#include <metal_stdlib>

using namespace metal;

template<typename T, size_t N>
struct tint_array {
    const constant T& operator[](size_t i) const constant { return elements[i]; }
    device T& operator[](size_t i) device { return elements[i]; }
    const device T& operator[](size_t i) const device { return elements[i]; }
    thread T& operator[](size_t i) thread { return elements[i]; }
    const thread T& operator[](size_t i) const thread { return elements[i]; }
    threadgroup T& operator[](size_t i) threadgroup { return elements[i]; }
    const threadgroup T& operator[](size_t i) const threadgroup { return elements[i]; }
    T elements[N];
};

struct tint_symbol_5 {
  tint_array<float4x3, 4> w;
};

struct tint_packed_vec3_f32_array_element {
  /* 0x0000 */ packed_float3 elements;
  /* 0x000c */ tint_array<int8_t, 4> tint_pad;
};

float4x3 tint_unpack_vec3_in_composite(tint_array<tint_packed_vec3_f32_array_element, 4> in) {
  float4x3 result = float4x3(0.0f);
  for(uint i = 0u; (i < 4u); i = (i + 1u)) {
    result[i] = float3(in[i].elements);
  }
  return result;
}

tint_array<float4x3, 4> tint_unpack_vec3_in_composite_1(tint_array<tint_array<tint_packed_vec3_f32_array_element, 4>, 4> in) {
  tint_array<float4x3, 4> result = {};
  for(uint i = 0u; (i < 4u); i = (i + 1u)) {
    result[i] = tint_unpack_vec3_in_composite(in[i]);
  }
  return result;
}

void f_inner(uint local_invocation_index, threadgroup tint_array<float4x3, 4>* const tint_symbol, const constant tint_array<tint_array<tint_packed_vec3_f32_array_element, 4>, 4>* const tint_symbol_1) {
  for(uint idx = local_invocation_index; (idx < 4u); idx = (idx + 1u)) {
    uint const i = idx;
    (*(tint_symbol))[i] = float4x3(float3(0.0f), float3(0.0f), float3(0.0f), float3(0.0f));
  }
  threadgroup_barrier(mem_flags::mem_threadgroup);
  *(tint_symbol) = tint_unpack_vec3_in_composite_1(*(tint_symbol_1));
  (*(tint_symbol))[1] = tint_unpack_vec3_in_composite((*(tint_symbol_1))[2]);
  (*(tint_symbol))[1][0] = float3((*(tint_symbol_1))[0][1].elements).zxy;
  (*(tint_symbol))[1][0][0] = (*(tint_symbol_1))[0][1].elements[0];
}

kernel void f(const constant tint_array<tint_array<tint_packed_vec3_f32_array_element, 4>, 4>* tint_symbol_4 [[buffer(0)]], threadgroup tint_symbol_5* tint_symbol_3 [[threadgroup(0)]], uint local_invocation_index [[thread_index_in_threadgroup]]) {
  threadgroup tint_array<float4x3, 4>* const tint_symbol_2 = &((*(tint_symbol_3)).w);
  f_inner(local_invocation_index, tint_symbol_2, tint_symbol_4);
  return;
}

