#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ fmaxf(float, int /*<<< orphan*/);
int /*<<< orphan*/ fminf(float, scalar_t__);
scalar_t__ linear2srgb(float);
scalar_t__ srgb2linear(float);
__attribute__((always_)) void
imReduceHalfBox3sRGB(unsigned char *dst, unsigned char *src, int bytesperpixel,
                     int bytesperline, float *dithersum) {
  int i, offset[4];
  float sum0, sum1, sum2;
  offset[0] = 0;
  offset[1] = bytesperpixel;
  offset[2] = bytesperline;
  offset[3] = bytesperline + bytesperpixel;
  sum0 = 0.0f;
  sum1 = 0.0f;
  sum2 = 0.0f;
  for (i = 0; i < 4; i++) {
    sum0 += srgb2linear((float)src[offset[i] + 0]);
    sum1 += srgb2linear((float)src[offset[i] + 1]);
    sum2 += srgb2linear((float)src[offset[i] + 2]);
  }
  dst[0] = (unsigned char)(fmaxf(
      0.0f, fminf(255.0f, linear2srgb(sum0 * 0.25f) + 0.5f)));
  dst[1] = (unsigned char)(fmaxf(
      0.0f, fminf(255.0f, linear2srgb(sum1 * 0.25f) + 0.5f)));
  dst[2] = (unsigned char)(fmaxf(
      0.0f, fminf(255.0f, linear2srgb(sum2 * 0.25f) + 0.5f)));
  return;
}


x86 assembly (clang, -O0):
<imReduceHalfBox3sRGB>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x44(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x48(%rbp)
xorps  %xmm0,%xmm0
movss  %xmm0,-0x4c(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x4,-0x24(%rbp)
jge    1232 <imReduceHalfBox3sRGB+0xf2>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
movslq -0x40(%rbp,%rcx,4),%rcx
movzbl (%rax,%rcx,1),%eax
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
cvtsi2ss %rax,%xmm1
movss  -0x44(%rbp),%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x44(%rbp)
mov    -0x10(%rbp),%rcx
movslq -0x24(%rbp),%rax
movslq -0x40(%rbp,%rax,4),%rax
movzbl 0x1(%rax,%rcx,1),%eax
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
cvtsi2ss %rax,%xmm1
movss  -0x48(%rbp),%xmm0
addss  %xmm1,%xmm0
movss  %xmm0,-0x48(%rbp)
mov    -0x10(%rbp),%rcx
movslq -0x24(%rbp),%rax
movslq -0x40(%rbp,%rax,4),%rax
movzbl 0x2(%rax,%rcx,1),%eax
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
cvtsi2ss %rax,%xmm0
addss  -0x4c(%rbp),%xmm0
movss  %xmm0,-0x4c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1195 <imReduceHalfBox3sRGB+0x55>
movss  0xdce(%rip),%xmm0
mulss  -0x44(%rbp),%xmm0
call   1030 <linear2srgb@plt>
cvtsi2ss %rax,%xmm0
movss  0xdb3(%rip),%xmm1
addss  %xmm1,%xmm0
cvttss2si %xmm0,%rdi
movss  0xd9e(%rip),%xmm0
call   1050 <fminf@plt>
mov    %eax,%edi
xorps  %xmm0,%xmm0
call   1060 <fmaxf@plt>
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
movss  0xd87(%rip),%xmm0
mulss  -0x48(%rbp),%xmm0
call   1030 <linear2srgb@plt>
cvtsi2ss %rax,%xmm0
movss  0xd6c(%rip),%xmm1
addss  %xmm1,%xmm0
cvttss2si %xmm0,%rdi
movss  0xd57(%rip),%xmm0
call   1050 <fminf@plt>
mov    %eax,%edi
xorps  %xmm0,%xmm0
call   1060 <fmaxf@plt>
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x1(%rax)
movss  0xd3f(%rip),%xmm0
mulss  -0x4c(%rbp),%xmm0
call   1030 <linear2srgb@plt>
cvtsi2ss %rax,%xmm0
movss  0xd24(%rip),%xmm1
addss  %xmm1,%xmm0
cvttss2si %xmm0,%rdi
movss  0xd0f(%rip),%xmm0
call   1050 <fminf@plt>
mov    %eax,%edi
xorps  %xmm0,%xmm0
call   1060 <fmaxf@plt>
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,0x2(%rax)
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<imReduceHalfBox3sRGB>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %rdi,%r14
lea    (%r15,%r12,1),%r13d
movzbl (%rsi),%eax
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movss  %xmm0,0x8(%rsp)
movzbl 0x1(%rbx),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movss  %xmm0,0x10(%rsp)
movzbl 0x2(%rbx),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movss  %xmm0,0xc(%rsp)
movslq %r12d,%rbp
movzbl (%rbx,%rbp,1),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0x8(%rsp),%xmm0
movss  %xmm0,0x8(%rsp)
movzbl 0x1(%rbx,%rbp,1),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0x10(%rsp),%xmm0
movss  %xmm0,0x10(%rsp)
movzbl 0x2(%rbx,%rbp,1),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0xc(%rsp),%xmm0
movss  %xmm0,0xc(%rsp)
movslq %r15d,%rbp
movzbl (%rbx,%rbp,1),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0x8(%rsp),%xmm0
movss  %xmm0,0x8(%rsp)
movzbl 0x1(%rbx,%rbp,1),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0x10(%rsp),%xmm0
movss  %xmm0,0x10(%rsp)
movzbl 0x2(%rbx,%rbp,1),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0xc(%rsp),%xmm0
movss  %xmm0,0xc(%rsp)
movslq %r13d,%rbp
movzbl (%rbx,%rbp,1),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0x8(%rsp),%xmm0
movss  %xmm0,0x14(%rsp)
movzbl 0x1(%rbx,%rbp,1),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0x10(%rsp),%xmm0
movss  %xmm0,0x8(%rsp)
movzbl 0x2(%rbx,%rbp,1),%eax
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
call   1040 <srgb2linear@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0xc(%rsp),%xmm0
movss  %xmm0,0xc(%rsp)
movss  0x14(%rsp),%xmm0
mulss  0xceb(%rip),%xmm0
call   1030 <linear2srgb@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0xcda(%rip),%xmm0
cvttss2si %xmm0,%rdi
movss  0xcd1(%rip),%xmm0
call   1050 <fminf@plt>
xorps  %xmm0,%xmm0
mov    %eax,%edi
call   1060 <fmaxf@plt>
mov    %al,(%r14)
movss  0x8(%rsp),%xmm0
mulss  0xca9(%rip),%xmm0
call   1030 <linear2srgb@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0xc98(%rip),%xmm0
cvttss2si %xmm0,%rdi
movss  0xc8f(%rip),%xmm0
call   1050 <fminf@plt>
xorps  %xmm0,%xmm0
mov    %eax,%edi
call   1060 <fmaxf@plt>
mov    %al,0x1(%r14)
movss  0xc(%rsp),%xmm0
mulss  0xc66(%rip),%xmm0
call   1030 <linear2srgb@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  0xc55(%rip),%xmm0
cvttss2si %xmm0,%rdi
movss  0xc4c(%rip),%xmm0
call   1050 <fminf@plt>
xorps  %xmm0,%xmm0
mov    %eax,%edi
call   1060 <fmaxf@plt>
mov    %al,0x2(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @imReduceHalfBox3sRGB(i8* %0, i8* %1, i32 %2, i32 %3, float* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca float*
%11 = alloca i32
%12 = alloca [4 x i32]
%13 = alloca float
%14 = alloca float
%15 = alloca float
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store float* %4, float** %10
%16 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 0
store i32 0, i32* %16
%17 = load i32, i32* %8
%18 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 1
store i32 %17, i32* %18
%19 = load i32, i32* %9
%20 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 2
store i32 %19, i32* %20
%21 = load i32, i32* %9
%22 = load i32, i32* %8
%23 = add nsw i32 %21, %22
%24 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 3
store i32 %23, i32* %24
store float 0.000000e+00, float* %13
store float 0.000000e+00, float* %14
store float 0.000000e+00, float* %15
store i32 0, i32* %11
br label %25
25: ; preds = %71, %5
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 4
br i1 %27, label %28, label %74
28: ; preds = %25
%29 = load i8*, i8** %7
%30 = load i32, i32* %11
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 %31
%33 = load i32, i32* %32
%34 = add nsw i32 %33, 0
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %29, i64 %35
%37 = load i8, i8* %36
%38 = uitofp i8 %37 to float
%39 = call i64 @srgb2linear(float %38)
%40 = sitofp i64 %39 to float
%41 = load float, float* %13
%42 = fadd float %41, %40
store float %42, float* %13
%43 = load i8*, i8** %7
%44 = load i32, i32* %11
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 %45
%47 = load i32, i32* %46
%48 = add nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %43, i64 %49
%51 = load i8, i8* %50
%52 = uitofp i8 %51 to float
%53 = call i64 @srgb2linear(float %52)
%54 = sitofp i64 %53 to float
%55 = load float, float* %14
%56 = fadd float %55, %54
store float %56, float* %14
%57 = load i8*, i8** %7
%58 = load i32, i32* %11
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 0, i64 %59
%61 = load i32, i32* %60
%62 = add nsw i32 %61, 2
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %57, i64 %63
%65 = load i8, i8* %64
%66 = uitofp i8 %65 to float
%67 = call i64 @srgb2linear(float %66)
%68 = sitofp i64 %67 to float
%69 = load float, float* %15
%70 = fadd float %69, %68
store float %70, float* %15
br label %71
71: ; preds = %28
%72 = load i32, i32* %11
%73 = add nsw i32 %72, 1
store i32 %73, i32* %11
br label %25
74: ; preds = %25
%75 = load float, float* %13
%76 = fmul float %75, 2.500000e-01
%77 = call i64 @linear2srgb(float %76)
%78 = sitofp i64 %77 to float
%79 = fadd float %78, 5.000000e-01
%80 = fptosi float %79 to i64
%81 = call i32 @fminf(float 2.550000e+02, i64 %80)
%82 = call i64 @fmaxf(float 0.000000e+00, i32 %81)
%83 = trunc i64 %82 to i8
%84 = load i8*, i8** %6
%85 = getelementptr inbounds i8, i8* %84, i64 0
store i8 %83, i8* %85
%86 = load float, float* %14
%87 = fmul float %86, 2.500000e-01
%88 = call i64 @linear2srgb(float %87)
%89 = sitofp i64 %88 to float
%90 = fadd float %89, 5.000000e-01
%91 = fptosi float %90 to i64
%92 = call i32 @fminf(float 2.550000e+02, i64 %91)
%93 = call i64 @fmaxf(float 0.000000e+00, i32 %92)
%94 = trunc i64 %93 to i8
%95 = load i8*, i8** %6
%96 = getelementptr inbounds i8, i8* %95, i64 1
store i8 %94, i8* %96
%97 = load float, float* %15
%98 = fmul float %97, 2.500000e-01
%99 = call i64 @linear2srgb(float %98)
%100 = sitofp i64 %99 to float
%101 = fadd float %100, 5.000000e-01
%102 = fptosi float %101 to i64
%103 = call i32 @fminf(float 2.550000e+02, i64 %102)
%104 = call i64 @fmaxf(float 0.000000e+00, i32 %103)
%105 = trunc i64 %104 to i8
%106 = load i8*, i8** %6
%107 = getelementptr inbounds i8, i8* %106, i64 2
store i8 %105, i8* %107
ret void
}

LLVM IR (-O2):
define void @imReduceHalfBox3sRGB(i8* nocapture writeonly %0, i8* nocapture %1, i32 %2, i32 %3, float* nocapture readnone %4) {
%6 = add nsw i32 %3, %2
%7 = load i8, i8* %1
%8 = uitofp i8 %7 to float
%9 = tail call i64 @srgb2linear(float %8)
%10 = sitofp i64 %9 to float
%11 = getelementptr inbounds i8, i8* %1, i64 1
%12 = load i8, i8* %11
%13 = uitofp i8 %12 to float
%14 = tail call i64 @srgb2linear(float %13)
%15 = sitofp i64 %14 to float
%16 = getelementptr inbounds i8, i8* %1, i64 2
%17 = load i8, i8* %16
%18 = uitofp i8 %17 to float
%19 = tail call i64 @srgb2linear(float %18)
%20 = sitofp i64 %19 to float
%21 = sext i32 %2 to i64
%22 = getelementptr inbounds i8, i8* %1, i64 %21
%23 = load i8, i8* %22
%24 = uitofp i8 %23 to float
%25 = tail call i64 @srgb2linear(float %24)
%26 = sitofp i64 %25 to float
%27 = fadd float %10, %26
%28 = add nsw i32 %2, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %1, i64 %29
%31 = load i8, i8* %30
%32 = uitofp i8 %31 to float
%33 = tail call i64 @srgb2linear(float %32)
%34 = sitofp i64 %33 to float
%35 = fadd float %15, %34
%36 = add nsw i32 %2, 2
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %1, i64 %37
%39 = load i8, i8* %38
%40 = uitofp i8 %39 to float
%41 = tail call i64 @srgb2linear(float %40)
%42 = sitofp i64 %41 to float
%43 = fadd float %20, %42
%44 = sext i32 %3 to i64
%45 = getelementptr inbounds i8, i8* %1, i64 %44
%46 = load i8, i8* %45
%47 = uitofp i8 %46 to float
%48 = tail call i64 @srgb2linear(float %47)
%49 = sitofp i64 %48 to float
%50 = fadd float %27, %49
%51 = add nsw i32 %3, 1
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %1, i64 %52
%54 = load i8, i8* %53
%55 = uitofp i8 %54 to float
%56 = tail call i64 @srgb2linear(float %55)
%57 = sitofp i64 %56 to float
%58 = fadd float %35, %57
%59 = add nsw i32 %3, 2
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %1, i64 %60
%62 = load i8, i8* %61
%63 = uitofp i8 %62 to float
%64 = tail call i64 @srgb2linear(float %63)
%65 = sitofp i64 %64 to float
%66 = fadd float %43, %65
%67 = sext i32 %6 to i64
%68 = getelementptr inbounds i8, i8* %1, i64 %67
%69 = load i8, i8* %68
%70 = uitofp i8 %69 to float
%71 = tail call i64 @srgb2linear(float %70)
%72 = sitofp i64 %71 to float
%73 = fadd float %50, %72
%74 = add nsw i32 %6, 1
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %1, i64 %75
%77 = load i8, i8* %76
%78 = uitofp i8 %77 to float
%79 = tail call i64 @srgb2linear(float %78)
%80 = sitofp i64 %79 to float
%81 = fadd float %58, %80
%82 = add nsw i32 %6, 2
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %1, i64 %83
%85 = load i8, i8* %84
%86 = uitofp i8 %85 to float
%87 = tail call i64 @srgb2linear(float %86)
%88 = sitofp i64 %87 to float
%89 = fadd float %66, %88
%90 = fmul float %73, 2.500000e-01
%91 = tail call i64 @linear2srgb(float %90)
%92 = sitofp i64 %91 to float
%93 = fadd float %92, 5.000000e-01
%94 = fptosi float %93 to i64
%95 = tail call i32 @fminf(float 2.550000e+02, i64 %94)
%96 = tail call i64 @fmaxf(float 0.000000e+00, i32 %95)
%97 = trunc i64 %96 to i8
store i8 %97, i8* %0
%98 = fmul float %81, 2.500000e-01
%99 = tail call i64 @linear2srgb(float %98)
%100 = sitofp i64 %99 to float
%101 = fadd float %100, 5.000000e-01
%102 = fptosi float %101 to i64
%103 = tail call i32 @fminf(float 2.550000e+02, i64 %102)
%104 = tail call i64 @fmaxf(float 0.000000e+00, i32 %103)
%105 = trunc i64 %104 to i8
%106 = getelementptr inbounds i8, i8* %0, i64 1
store i8 %105, i8* %106
%107 = fmul float %89, 2.500000e-01
%108 = tail call i64 @linear2srgb(float %107)
%109 = sitofp i64 %108 to float
%110 = fadd float %109, 5.000000e-01
%111 = fptosi float %110 to i64
%112 = tail call i32 @fminf(float 2.550000e+02, i64 %111)
%113 = tail call i64 @fmaxf(float 0.000000e+00, i32 %112)
%114 = trunc i64 %113 to i8
%115 = getelementptr inbounds i8, i8* %0, i64 2
store i8 %114, i8* %115
ret void
}