#include <metal_stdlib>

using namespace metal;
float4 tint_textureSampleBaseClampToEdge(texture2d<float, access::sample> t, sampler s, float2 coord) {
  float2 const dims = float2(uint2(t.get_width(0), t.get_height(0)));
  float2 const half_texel = (float2(0.5f) / dims);
  float2 const clamped = clamp(coord, half_texel, (1.0f - half_texel));
  return t.sample(s, clamped, level(0.0f));
}

void textureSampleBaseClampToEdge_9ca02c(texture2d<float, access::sample> tint_symbol_1, sampler tint_symbol_2, device float4* const tint_symbol_3) {
  float4 res = tint_textureSampleBaseClampToEdge(tint_symbol_1, tint_symbol_2, float2(1.0f));
  *(tint_symbol_3) = res;
}

struct tint_symbol {
  float4 value [[position]];
};

float4 vertex_main_inner(texture2d<float, access::sample> tint_symbol_4, sampler tint_symbol_5, device float4* const tint_symbol_6) {
  textureSampleBaseClampToEdge_9ca02c(tint_symbol_4, tint_symbol_5, tint_symbol_6);
  return float4(0.0f);
}

vertex tint_symbol vertex_main(texture2d<float, access::sample> tint_symbol_7 [[texture(0)]], sampler tint_symbol_8 [[sampler(0)]], device float4* tint_symbol_9 [[buffer(0)]]) {
  float4 const inner_result = vertex_main_inner(tint_symbol_7, tint_symbol_8, tint_symbol_9);
  tint_symbol wrapper_result = {};
  wrapper_result.value = inner_result;
  return wrapper_result;
}

fragment void fragment_main(texture2d<float, access::sample> tint_symbol_10 [[texture(0)]], sampler tint_symbol_11 [[sampler(0)]], device float4* tint_symbol_12 [[buffer(0)]]) {
  textureSampleBaseClampToEdge_9ca02c(tint_symbol_10, tint_symbol_11, tint_symbol_12);
  return;
}

kernel void compute_main(texture2d<float, access::sample> tint_symbol_13 [[texture(0)]], sampler tint_symbol_14 [[sampler(0)]], device float4* tint_symbol_15 [[buffer(0)]]) {
  textureSampleBaseClampToEdge_9ca02c(tint_symbol_13, tint_symbol_14, tint_symbol_15);
  return;
}

