#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int bench(int inputN) {
  int w, h, bit_num = 0;
  char byte_acc = 0;
  int i, iter = 50;
  double x, y, limit = 2.0;
  double Zr, Zi, Cr, Ci, Tr, Ti;
  w = h = inputN;
  char filename[64];
  sprintf(filename, "mandelbrot-%d.txt", w);
  FILE *outStream = fopen(filename, "w+");
  printf("P4\n%d %d\n", w, h);
  for (y = 0; y < h; ++y) {
    for (x = 0; x < w; ++x) {
      Zr = Zi = Tr = Ti = 0.0;
      Cr = (2.0 * x / w - 1.5);
      Ci = (2.0 * y / h - 1.0);
      for (i = 0; i < iter && (Tr + Ti <= limit * limit); ++i) {
        Zi = 2.0 * Zr * Zi + Ci;
        Zr = Tr - Ti + Cr;
        Tr = Zr * Zr;
        Ti = Zi * Zi;
      }
      byte_acc <<= 1;
      if (Tr + Ti <= limit * limit)
        byte_acc |= 0x01;
      ++bit_num;
      if (bit_num == 8) {
        putc(byte_acc, outStream);
        byte_acc = 0;
        bit_num = 0;
      } else if (x == w - 1) {
        byte_acc <<= (8 - w % 8);
        putc(byte_acc, outStream);
        byte_acc = 0;
        bit_num = 0;
      }
    }
  }
  fclose(outStream);
  return 0;
}


x86 assembly (clang, -O0):
<bench>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
movl   $0x32,-0x1c(%rbp)
movsd  0xe88(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    %eax,-0x8(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe81(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0xb0(%rbp),%rdi
lea    0xe80(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0xb8(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xe6a(%rip),%rdi
call   1040 <printf@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm1
cvtsi2sdl -0xc(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    141c <bench+0x2cc>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm1
cvtsi2sdl -0x8(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1400 <bench+0x2b0>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x68(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x60(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x48(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  0xdd8(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
cvtsi2sdl -0x8(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xdd2(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  0xdb1(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
cvtsi2sdl -0xc(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xda3(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x1c(%rbp),%ecx
mov    %al,-0xb9(%rbp)
jge    12aa <bench+0x15a>
movsd  -0x60(%rbp),%xmm1
addsd  -0x68(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
mulsd  -0x38(%rbp),%xmm0
ucomisd %xmm1,%xmm0
setae  %al
mov    %al,-0xb9(%rbp)
mov    -0xb9(%rbp),%al
test   $0x1,%al
jne    12bd <bench+0x16d>
jmp    1321 <bench+0x1d1>
movsd  0xd3b(%rip),%xmm0
mulsd  -0x40(%rbp),%xmm0
movsd  -0x48(%rbp),%xmm2
movsd  -0x58(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x60(%rbp),%xmm0
subsd  -0x68(%rbp),%xmm0
addsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x40(%rbp),%xmm0
mulsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x48(%rbp),%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1275 <bench+0x125>
movsbl -0x11(%rbp),%eax
shl    $0x1,%eax
mov    %al,-0x11(%rbp)
movsd  -0x60(%rbp),%xmm1
addsd  -0x68(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
mulsd  -0x38(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jb     1353 <bench+0x203>
movsbl -0x11(%rbp),%eax
or     $0x1,%eax
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x8,-0x10(%rbp)
jne    1388 <bench+0x238>
mov    -0x11(%rbp),%al
mov    -0xb8(%rbp),%rsi
movsbl %al,%edi
call   1050 <putc@plt>
movb   $0x0,-0x11(%rbp)
movl   $0x0,-0x10(%rbp)
jmp    13e4 <bench+0x294>
movsd  -0x28(%rbp),%xmm0
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
cvtsi2sd %eax,%xmm1
ucomisd %xmm1,%xmm0
jne    13df <bench+0x28f>
jp     13df <bench+0x28f>
mov    -0x8(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    $0x8,%ecx
sub    %edx,%ecx
movsbl -0x11(%rbp),%eax
shl    %cl,%eax
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
mov    -0xb8(%rbp),%rsi
movsbl %al,%edi
call   1050 <putc@plt>
movb   $0x0,-0x11(%rbp)
movl   $0x0,-0x10(%rbp)
jmp    13e4 <bench+0x294>
jmp    13e9 <bench+0x299>
movsd  0xc17(%rip),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    11ec <bench+0x9c>
jmp    1405 <bench+0x2b5>
movsd  0xbfb(%rip),%xmm0
addsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    11d0 <bench+0x80>
mov    -0xb8(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bench>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %edi,%r14d
lea    0xebe(%rip),%rsi
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14d,%edx
call   1070 <sprintf@plt>
lea    0xeb9(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
lea    0xeaa(%rip),%rdi
mov    %r14d,%esi
mov    %r14d,%edx
call   1040 <printf@plt>
test   %r14d,%r14d
jle    131b <bench+0x1cb>
cvtsi2sd %r14d,%xmm8
lea    -0x1(%r14),%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x18(%rsp)
and    $0x7,%r14d
mov    $0x8,%ebp
sub    %r14d,%ebp
xorpd  %xmm0,%xmm0
xor    %ecx,%ecx
movsd  0xe44(%rip),%xmm7
xor    %eax,%eax
movsd  %xmm8,0x20(%rsp)
jmp    11f9 <bench+0xa9>
nopw   0x0(%rax,%rax,1)
movsd  0x10(%rsp),%xmm0
addsd  0xe2a(%rip),%xmm0
ucomisd %xmm0,%xmm8
jbe    131b <bench+0x1cb>
movsd  %xmm0,0x10(%rsp)
movapd %xmm0,%xmm6
addsd  %xmm0,%xmm6
divsd  %xmm8,%xmm6
addsd  0xdec(%rip),%xmm6
xorpd  %xmm9,%xmm9
movsd  %xmm6,0x28(%rsp)
jmp    1276 <bench+0x126>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsd  %xmm9,0x8(%rsp)
movsbl %al,%edi
mov    %rbx,%rsi
call   1050 <putc@plt>
xor    %eax,%eax
xor    %ecx,%ecx
movsd  0x20(%rsp),%xmm8
movsd  0xdbb(%rip),%xmm7
movsd  0x28(%rsp),%xmm6
movsd  0x8(%rsp),%xmm9
addsd  0xdad(%rip),%xmm9
ucomisd %xmm9,%xmm8
jbe    11e0 <bench+0x90>
movapd %xmm9,%xmm0
addsd  %xmm9,%xmm0
divsd  %xmm8,%xmm0
addsd  0xd7b(%rip),%xmm0
xor    %edx,%edx
xorpd  %xmm3,%xmm3
xorpd  %xmm2,%xmm2
xorpd  %xmm1,%xmm1
xorpd  %xmm4,%xmm4
nop
subsd  %xmm3,%xmm2
addsd  %xmm4,%xmm4
mulsd  %xmm4,%xmm1
movapd %xmm2,%xmm4
addsd  %xmm0,%xmm4
addsd  %xmm6,%xmm1
movapd %xmm4,%xmm2
mulsd  %xmm4,%xmm2
movapd %xmm1,%xmm3
mulsd  %xmm1,%xmm3
movapd %xmm2,%xmm5
addsd  %xmm3,%xmm5
cmp    $0x30,%edx
ja     12de <bench+0x18e>
add    $0x1,%edx
ucomisd %xmm5,%xmm7
jae    12a0 <bench+0x150>
ucomisd %xmm5,%xmm7
setae  %dl
add    %al,%al
or     %dl,%al
add    $0x1,%ecx
cmp    $0x8,%ecx
je     1230 <bench+0xe0>
ucomisd 0x18(%rsp),%xmm9
jne    1262 <bench+0x112>
jp     1262 <bench+0x112>
movsd  %xmm9,0x8(%rsp)
movzbl %al,%eax
mov    %ebp,%ecx
shl    %cl,%eax
jmp    1237 <bench+0xe7>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"mandelbrot-%d.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [10 x i8] c"P4\0A%d %d\0A\00"
define i32 @bench(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca double
%17 = alloca double
%18 = alloca [64 x i8]
%19 = alloca i32*
store i32 %0, i32* %2
store i32 0, i32* %5
store i8 0, i8* %6
store i32 50, i32* %8
store double 2.000000e+00, double* %11
%20 = load i32, i32* %2
store i32 %20, i32* %4
store i32 %20, i32* %3
%21 = getelementptr inbounds [64 x i8], [64 x i8]* %18, i64 0, i64 0
%22 = load i32, i32* %3
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %18, i64 0, i64 0
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %19
%26 = load i32, i32* %3
%27 = load i32, i32* %4
%28 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %27)
store double 0.000000e+00, double* %10
br label %29
29: ; preds = %136, %1
%30 = load double, double* %10
%31 = load i32, i32* %4
%32 = sitofp i32 %31 to double
%33 = fcmp olt double %30, %32
br i1 %33, label %34, label %139
34: ; preds = %29
store double 0.000000e+00, double* %9
br label %35
35: ; preds = %132, %34
%36 = load double, double* %9
%37 = load i32, i32* %3
%38 = sitofp i32 %37 to double
%39 = fcmp olt double %36, %38
br i1 %39, label %40, label %135
40: ; preds = %35
store double 0.000000e+00, double* %17
store double 0.000000e+00, double* %16
store double 0.000000e+00, double* %13
store double 0.000000e+00, double* %12
%41 = load double, double* %9
%42 = fmul double 2.000000e+00, %41
%43 = load i32, i32* %3
%44 = sitofp i32 %43 to double
%45 = fdiv double %42, %44
%46 = fsub double %45, 1.500000e+00
store double %46, double* %14
%47 = load double, double* %10
%48 = fmul double 2.000000e+00, %47
%49 = load i32, i32* %4
%50 = sitofp i32 %49 to double
%51 = fdiv double %48, %50
%52 = fsub double %51, 1.000000e+00
store double %52, double* %15
store i32 0, i32* %7
br label %53
53: ; preds = %84, %40
%54 = load i32, i32* %7
%55 = load i32, i32* %8
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %65
57: ; preds = %53
%58 = load double, double* %16
%59 = load double, double* %17
%60 = fadd double %58, %59
%61 = load double, double* %11
%62 = load double, double* %11
%63 = fmul double %61, %62
%64 = fcmp ole double %60, %63
br label %65
65: ; preds = %57, %53
%66 = phi i1 [ false, %53 ], [ %64, %57 ]
br i1 %66, label %67, label %87
67: ; preds = %65
%68 = load double, double* %12
%69 = fmul double 2.000000e+00, %68
%70 = load double, double* %13
%71 = load double, double* %15
%72 = call double @llvm.fmuladd.f64(double %69, double %70, double %71)
store double %72, double* %13
%73 = load double, double* %16
%74 = load double, double* %17
%75 = fsub double %73, %74
%76 = load double, double* %14
%77 = fadd double %75, %76
store double %77, double* %12
%78 = load double, double* %12
%79 = load double, double* %12
%80 = fmul double %78, %79
store double %80, double* %16
%81 = load double, double* %13
%82 = load double, double* %13
%83 = fmul double %81, %82
store double %83, double* %17
br label %84
84: ; preds = %67
%85 = load i32, i32* %7
%86 = add nsw i32 %85, 1
store i32 %86, i32* %7
br label %53
87: ; preds = %65
%88 = load i8, i8* %6
%89 = sext i8 %88 to i32
%90 = shl i32 %89, 1
%91 = trunc i32 %90 to i8
store i8 %91, i8* %6
%92 = load double, double* %16
%93 = load double, double* %17
%94 = fadd double %92, %93
%95 = load double, double* %11
%96 = load double, double* %11
%97 = fmul double %95, %96
%98 = fcmp ole double %94, %97
br i1 %98, label %99, label %104
99: ; preds = %87
%100 = load i8, i8* %6
%101 = sext i8 %100 to i32
%102 = or i32 %101, 1
%103 = trunc i32 %102 to i8
store i8 %103, i8* %6
br label %104
104: ; preds = %99, %87
%105 = load i32, i32* %5
%106 = add nsw i32 %105, 1
store i32 %106, i32* %5
%107 = load i32, i32* %5
%108 = icmp eq i32 %107, 8
br i1 %108, label %109, label %113
109: ; preds = %104
%110 = load i8, i8* %6
%111 = load i32*, i32** %19
%112 = call i32 @putc(i8 signext %110, i32* %111)
store i8 0, i8* %6
store i32 0, i32* %5
br label %131
113: ; preds = %104
%114 = load double, double* %9
%115 = load i32, i32* %3
%116 = sub nsw i32 %115, 1
%117 = sitofp i32 %116 to double
%118 = fcmp oeq double %114, %117
br i1 %118, label %119, label %130
119: ; preds = %113
%120 = load i32, i32* %3
%121 = srem i32 %120, 8
%122 = sub nsw i32 8, %121
%123 = load i8, i8* %6
%124 = sext i8 %123 to i32
%125 = shl i32 %124, %122
%126 = trunc i32 %125 to i8
store i8 %126, i8* %6
%127 = load i8, i8* %6
%128 = load i32*, i32** %19
%129 = call i32 @putc(i8 signext %127, i32* %128)
store i8 0, i8* %6
store i32 0, i32* %5
br label %130
130: ; preds = %119, %113
br label %131
131: ; preds = %130, %109
br label %132
132: ; preds = %131
%133 = load double, double* %9
%134 = fadd double %133, 1.000000e+00
store double %134, double* %9
br label %35
135: ; preds = %35
br label %136
136: ; preds = %135
%137 = load double, double* %10
%138 = fadd double %137, 1.000000e+00
store double %138, double* %10
br label %29
139: ; preds = %29
%140 = load i32*, i32** %19
%141 = call i32 @fclose(i32* %140)
ret i32 0
}

LLVM IR (-O2):
@.str = [18 x i8] c"mandelbrot-%d.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [10 x i8] c"P4\0A%d %d\0A\00"
define i32 @bench(i32 %0) {
%2 = alloca [64 x i8]
%3 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%4 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %0)
%7 = sitofp i32 %0 to double
%8 = icmp sgt i32 %0, 0
br i1 %8, label %9, label %69
9: ; preds = %1
%10 = add nsw i32 %0, -1
%11 = sitofp i32 %10 to double
%12 = and i32 %0, 7
%13 = sub nuw nsw i32 8, %12
br label %14
14: ; preds = %66, %9
%15 = phi i32 [ 0, %9 ], [ %63, %66 ]
%16 = phi i8 [ 0, %9 ], [ %62, %66 ]
%17 = phi double [ 0.000000e+00, %9 ], [ %67, %66 ]
%18 = fmul double %17, 2.000000e+00
%19 = fdiv double %18, %7
%20 = fadd double %19, -1.000000e+00
br label %21
21: ; preds = %14, %61
%22 = phi i32 [ %15, %14 ], [ %63, %61 ]
%23 = phi i8 [ %16, %14 ], [ %62, %61 ]
%24 = phi double [ 0.000000e+00, %14 ], [ %64, %61 ]
%25 = fmul double %24, 2.000000e+00
%26 = fdiv double %25, %7
%27 = fadd double %26, -1.500000e+00
br label %28
28: ; preds = %21, %28
%29 = phi double [ 0.000000e+00, %21 ], [ %39, %28 ]
%30 = phi double [ 0.000000e+00, %21 ], [ %38, %28 ]
%31 = phi i32 [ 0, %21 ], [ %40, %28 ]
%32 = phi double [ 0.000000e+00, %21 ], [ %35, %28 ]
%33 = phi double [ 0.000000e+00, %21 ], [ %37, %28 ]
%34 = fmul double %33, 2.000000e+00
%35 = tail call double @llvm.fmuladd.f64(double %34, double %32, double %20)
%36 = fsub double %30, %29
%37 = fadd double %27, %36
%38 = fmul double %37, %37
%39 = fmul double %35, %35
%40 = add nuw nsw i32 %31, 1
%41 = icmp ugt i32 %31, 48
%42 = fadd double %38, %39
%43 = fcmp ugt double %42, 4.000000e+00
%44 = select i1 %41, i1 true, i1 %43
br i1 %44, label %45, label %28
45: ; preds = %28
%46 = shl i8 %23, 1
%47 = xor i1 %43, true
%48 = zext i1 %47 to i8
%49 = or i8 %46, %48
%50 = add nsw i32 %22, 1
%51 = icmp eq i32 %50, 8
br i1 %51, label %58, label %52
52: ; preds = %45
%53 = fcmp oeq double %24, %11
br i1 %53, label %54, label %61
54: ; preds = %52
%55 = zext i8 %49 to i32
%56 = shl nuw nsw i32 %55, %13
%57 = trunc i32 %56 to i8
br label %58
58: ; preds = %45, %54
%59 = phi i8 [ %57, %54 ], [ %49, %45 ]
%60 = tail call i32 @putc(i8 signext %59, i32* %5)
br label %61
61: ; preds = %58, %52
%62 = phi i8 [ %49, %52 ], [ 0, %58 ]
%63 = phi i32 [ %50, %52 ], [ 0, %58 ]
%64 = fadd double %24, 1.000000e+00
%65 = fcmp olt double %64, %7
br i1 %65, label %21, label %66
66: ; preds = %61
%67 = fadd double %17, 1.000000e+00
%68 = fcmp olt double %67, %7
br i1 %68, label %14, label %69
69: ; preds = %66, %1
%70 = tail call i32 @fclose(i32* %5)
ret i32 0
}