#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 */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int checki(int /*<<< orphan*/, char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dump_file(int, float *, size_t);
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ open(char const *const, int, int);
int /*<<< orphan*/ slurp_path(char const *const, unsigned char **, size_t *,
                              size_t *);
int strtol(char const *const, int /*<<< orphan*/ *, int /*<<< orphan*/);
size_t sum(float *, int);
float *xmalloc(size_t);
int main(int argc, const char *const *argv) {
  if (argc != 4)
    errx(1, "Arguments <in> <out> <order>");
  int order = strtol(argv[3], NULL, 0);
  if (order < 1 || order > 20)
    errx(1, "Order should be between 1 and 20.");
  size_t stride = 1 << order;
  unsigned char *buffer = NULL;
  size_t bytes = 0;
  size_t size = 0;
  slurp_path(argv[1], &buffer, &bytes, &size);
  float *input = (float *)buffer;
  size_t count = bytes / sizeof(float) / stride;
  float *output = xmalloc(count * sizeof(float));
  for (size_t i = 0; i != count; ++i)
    output[i] = sum(input + i * stride, order) / stride;
  int out =
      checki(open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666), "open output");
  dump_file(out, output, count * sizeof(float));
  checki(close(out), "close output");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11c1 <main+0x31>
mov    $0x1,%edi
lea    0xe44(%rip),%rsi
call   1030 <errx@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jl     11eb <main+0x5b>
cmpl   $0x14,-0x14(%rbp)
jle    11fc <main+0x6c>
mov    $0x1,%edi
lea    0xe26(%rip),%rsi
call   1030 <errx@plt>
mov    -0x14(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
cltq
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10b0 <slurp_path@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
shr    $0x2,%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
shl    $0x2,%rdi
call   1060 <xmalloc@plt>
mov    %rax,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x48(%rbp),%rax
je     1301 <main+0x171>
mov    -0x40(%rbp),%rax
mov    -0x58(%rbp),%rcx
mov    -0x20(%rbp),%rdx
imul   %rdx,%rcx
lea    (%rax,%rcx,4),%rdi
mov    -0x14(%rbp),%esi
call   1090 <sum@plt>
mov    -0x20(%rbp),%rcx
xor    %edx,%edx
div    %rcx
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x64(%rbp)
test   %rax,%rax
movss  %xmm0,-0x60(%rbp)
js     12de <main+0x14e>
movss  -0x64(%rbp),%xmm0
movss  %xmm0,-0x60(%rbp)
movss  -0x60(%rbp),%xmm0
mov    -0x50(%rbp),%rax
mov    -0x58(%rbp),%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    1272 <main+0xe2>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2cb8(%rip),%rax
mov    (%rax),%esi
mov    0x2cdf(%rip),%rax
or     (%rax),%esi
mov    0x2cce(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
mov    %eax,%edi
lea    0xd08(%rip),%rsi
call   1040 <checki@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdx
shl    $0x2,%rdx
call   1050 <dump_file@plt>
mov    -0x5c(%rbp),%edi
call   1070 <close@plt>
mov    %eax,%edi
lea    0xce7(%rip),%rsi
call   1040 <checki@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
cmp    $0x4,%edi
je     11b7 <main+0x27>
lea    0xe53(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
mov    0x18(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtol@plt>
mov    %eax,%ebp
add    $0xffffffeb,%eax
cmp    $0xffffffeb,%eax
ja     11df <main+0x4f>
lea    0xe48(%rip),%rsi
mov    $0x1,%edi
call   1030 <errx@plt>
mov    $0x1,%r14d
mov    %ebp,0xc(%rsp)
mov    %ebp,%ecx
shl    %cl,%r14d
movslq %r14d,%r15
movq   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x30(%rsp)
mov    %rbx,0x28(%rsp)
mov    0x8(%rbx),%rdi
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0x30(%rsp),%rcx
call   10b0 <slurp_path@plt>
mov    0x18(%rsp),%rbx
mov    0x10(%rsp),%rbp
shr    $0x2,%rbp
mov    %rbp,%rax
or     %r15,%rax
shr    $0x20,%rax
mov    %r14d,0x8(%rsp)
je     1255 <main+0xc5>
mov    %rbp,%rax
xor    %edx,%edx
div    %r15
mov    %rax,%r14
jmp    125f <main+0xcf>
mov    %ebp,%eax
xor    %edx,%edx
div    %r14d
mov    %eax,%r14d
lea    0x0(,%r14,4),%rdi
mov    %rdi,0x20(%rsp)
call   1060 <xmalloc@plt>
mov    %rax,%r13
cmp    %r15,%rbp
jae    12ec <main+0x15c>
mov    0x2d48(%rip),%rax
mov    0x2d71(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x28(%rsp),%rax
mov    0x10(%rax),%rdi
mov    0x2d55(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   10a0 <open@plt>
lea    0xd91(%rip),%rsi
mov    %eax,%edi
call   1040 <checki@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r13,%rsi
mov    0x20(%rsp),%rdx
call   1050 <dump_file@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xd77(%rip),%rsi
mov    %eax,%edi
call   1040 <checki@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x0(,%r15,4),%rbp
xor    %r12d,%r12d
jmp    131f <main+0x18f>
nopl   0x0(%rax)
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
movss  %xmm0,0x0(%r13,%r12,4)
add    $0x1,%r12
add    %rbp,%rbx
cmp    %r14,%r12
je     1279 <main+0xe9>
mov    %rbx,%rdi
mov    0xc(%rsp),%esi
call   1090 <sum@plt>
mov    %rax,%rcx
or     %r15,%rcx
shr    $0x20,%rcx
je     1350 <main+0x1c0>
xor    %edx,%edx
div    %r15
test   %rax,%rax
jns    1300 <main+0x170>
jmp    135b <main+0x1cb>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %edx,%edx
divl   0x8(%rsp)
test   %rax,%rax
jns    1300 <main+0x170>
mov    %rax,%rcx
shr    %rcx
and    $0x1,%eax
or     %rcx,%rax
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  %xmm0,%xmm0
jmp    1308 <main+0x178>

LLVM IR (-O0):
@.str = [29 x i8] c"Arguments <in> <out> <order>\00"
@.str.1 = [34 x i8] c"Order should be between 1 and 20.\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [12 x i8] c"open output\00"
@.str.3 = [13 x i8] c"close output\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca float*
%12 = alloca i64
%13 = alloca float*
%14 = alloca i64
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 4
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @errx(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 3
%23 = load i8*, i8** %22
%24 = call i32 @strtol(i8* %23, i32* null, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 1
br i1 %26, label %30, label %27
27: ; preds = %20
%28 = load i32, i32* %6
%29 = icmp sgt i32 %28, 20
br i1 %29, label %30, label %32
30: ; preds = %27, %20
%31 = call i32 @errx(i32 1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = load i32, i32* %6
%34 = shl i32 1, %33
%35 = sext i32 %34 to i64
store i64 %35, i64* %7
store i8* null, i8** %8
store i64 0, i64* %9
store i64 0, i64* %10
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @slurp_path(i8* %38, i8** %8, i64* %9, i64* %10)
%40 = load i8*, i8** %8
%41 = bitcast i8* %40 to float*
store float* %41, float** %11
%42 = load i64, i64* %9
%43 = udiv i64 %42, 4
%44 = load i64, i64* %7
%45 = udiv i64 %43, %44
store i64 %45, i64* %12
%46 = load i64, i64* %12
%47 = mul i64 %46, 4
%48 = call float* @xmalloc(i64 %47)
store float* %48, float** %13
store i64 0, i64* %14
br label %49
49: ; preds = %67, %32
%50 = load i64, i64* %14
%51 = load i64, i64* %12
%52 = icmp ne i64 %50, %51
br i1 %52, label %53, label %70
53: ; preds = %49
%54 = load float*, float** %11
%55 = load i64, i64* %14
%56 = load i64, i64* %7
%57 = mul i64 %55, %56
%58 = getelementptr inbounds float, float* %54, i64 %57
%59 = load i32, i32* %6
%60 = call i64 @sum(float* %58, i32 %59)
%61 = load i64, i64* %7
%62 = udiv i64 %60, %61
%63 = uitofp i64 %62 to float
%64 = load float*, float** %13
%65 = load i64, i64* %14
%66 = getelementptr inbounds float, float* %64, i64 %65
store float %63, float* %66
br label %67
67: ; preds = %53
%68 = load i64, i64* %14
%69 = add i64 %68, 1
store i64 %69, i64* %14
br label %49
70: ; preds = %49
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i64 2
%73 = load i8*, i8** %72
%74 = load i32, i32* @O_WRONLY
%75 = load i32, i32* @O_CREAT
%76 = or i32 %74, %75
%77 = load i32, i32* @O_TRUNC
%78 = or i32 %76, %77
%79 = call i32 @open(i8* %73, i32 %78, i32 438)
%80 = call i32 @checki(i32 %79, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 %80, i32* %15
%81 = load i32, i32* %15
%82 = load float*, float** %13
%83 = load i64, i64* %12
%84 = mul i64 %83, 4
%85 = call i32 @dump_file(i32 %81, float* %82, i64 %84)
%86 = load i32, i32* %15
%87 = call i32 @close(i32 %86)
%88 = call i32 @checki(i32 %87, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [29 x i8] c"Arguments <in> <out> <order>\00"
@.str.1 = [34 x i8] c"Order should be between 1 and 20.\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [12 x i8] c"open output\00"
@.str.3 = [13 x i8] c"close output\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = icmp eq i32 %0, 4
br i1 %6, label %9, label %7
7: ; preds = %2
%8 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = getelementptr inbounds i8*, i8** %1, i64 3
%11 = load i8*, i8** %10
%12 = tail call i32 @strtol(i8* nocapture %11, i32* null, i32 0)
%13 = add i32 %12, -21
%14 = icmp ult i32 %13, -20
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %9, %15
%18 = shl nuw i32 1, %12
%19 = sext i32 %18 to i64
%20 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%21 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%22 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @slurp_path(i8* %24, i8** %3, i64* %4, i64* %5)
%26 = bitcast i8** %3 to float**
%27 = load float*, float** %26
%28 = load i64, i64* %4
%29 = lshr i64 %28, 2
%30 = udiv i64 %29, %19
%31 = shl nuw i64 %30, 2
%32 = call float* @xmalloc(i64 %31)
%33 = icmp ult i64 %29, %19
br i1 %33, label %34, label %47
34: ; preds = %47, %17
%35 = getelementptr inbounds i8*, i8** %1, i64 2
%36 = load i8*, i8** %35
%37 = load i32, i32* @O_WRONLY
%38 = load i32, i32* @O_CREAT
%39 = or i32 %38, %37
%40 = load i32, i32* @O_TRUNC
%41 = or i32 %39, %40
%42 = call i32 @open(i8* %36, i32 %41, i32 438)
%43 = call i32 @checki(i32 %42, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @dump_file(i32 %43, float* %32, i64 %31)
%45 = call i32 @close(i32 %43)
%46 = call i32 @checki(i32 %45, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
47: ; preds = %17, %47
%48 = phi i64 [ %55, %47 ], [ 0, %17 ]
%49 = mul i64 %48, %19
%50 = getelementptr inbounds float, float* %27, i64 %49
%51 = call i64 @sum(float* %50, i32 %12)
%52 = udiv i64 %51, %19
%53 = uitofp i64 %52 to float
%54 = getelementptr inbounds float, float* %32, i64 %48
store float %53, float* %54
%55 = add i64 %48, 1
%56 = icmp eq i64 %55, %30
br i1 %56, label %34, label %47
}