#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*/ fprintf(int /*<<< orphan*/ *, char *, int, unsigned char);
int /*<<< orphan*/ fread(unsigned char **, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ system(char *);
int main() {
  unsigned char image[256][256];
  unsigned char histgram[256];
  FILE *fp;
  fp = fopen("girldark.dat", "rb");
  fread(image, sizeof(unsigned char), 256 * 256, fp);
  fclose(fp);
  for (int i = 0; i < 256; i++) {
    for (int j = 0; j < 256; j++) {
      histgram[image[i][j]]++;
    }
  }
  fp = fopen("plot.dat", "wb");
  for (int i = 0; i < 256; i++) {
    fprintf(fp, "%3d %3d\n", i, histgram[i]);
  }
  fclose(fp);
  system("zsh ex2-1.sh");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10130,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe97(%rip),%rdi
lea    0xe9d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10118(%rbp)
lea    -0x10010(%rbp),%rdi
mov    -0x10118(%rbp),%rcx
mov    $0x1,%esi
mov    $0x10000,%edx
call   1030 <fread@plt>
mov    -0x10118(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x1011c(%rbp)
cmpl   $0x100,-0x1011c(%rbp)
jge    1237 <main+0xe7>
movl   $0x0,-0x10120(%rbp)
cmpl   $0x100,-0x10120(%rbp)
jge    121e <main+0xce>
movslq -0x1011c(%rbp),%rcx
lea    -0x10010(%rbp),%rax
shl    $0x8,%rcx
add    %rcx,%rax
movslq -0x10120(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    -0x10110(%rbp,%rax,1),%cl
add    $0x1,%cl
mov    %cl,-0x10110(%rbp,%rax,1)
mov    -0x10120(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10120(%rbp)
jmp    11c9 <main+0x79>
jmp    1223 <main+0xd3>
mov    -0x1011c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1011c(%rbp)
jmp    11af <main+0x5f>
lea    0xdd2(%rip),%rdi
lea    0xdd4(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10118(%rbp)
movl   $0x0,-0x10124(%rbp)
cmpl   $0x100,-0x10124(%rbp)
jge    12a7 <main+0x157>
mov    -0x10118(%rbp),%rdi
mov    -0x10124(%rbp),%edx
movslq -0x10124(%rbp),%rax
lea    0xd96(%rip),%rsi
movzbl -0x10110(%rbp,%rax,1),%ecx
ff
call   1060 <fprintf@plt>
mov    -0x10124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10124(%rbp)
jmp    125b <main+0x10b>
mov    -0x10118(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd6b(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
add    $0x10130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10100,%rsp
lea    0xe9d(%rip),%rdi
lea    0xea3(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0x100(%rsp),%rdi
mov    $0x1,%esi
mov    $0x10000,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0x101(%rsp),%rax
xor    %ecx,%ecx
xchg   %ax,%ax
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl -0x1(%rax,%rdx,1),%esi
addb   $0x1,(%rsp,%rsi,1)
movzbl (%rax,%rdx,1),%esi
addb   $0x1,(%rsp,%rsi,1)
add    $0x2,%rdx
cmp    $0x100,%rdx
jne    11b0 <main+0x60>
add    $0x1,%rcx
add    $0x100,%rax
cmp    $0x100,%rcx
jne    11a0 <main+0x50>
lea    0xe28(%rip),%rdi
lea    0xe2a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%r14
xor    %ebx,%ebx
lea    0xe1c(%rip),%r15
movzbl (%rsp,%rbx,1),%ecx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1060 <fprintf@plt>
add    $0x1,%rbx
cmp    $0x100,%rbx
jne    1200 <main+0xb0>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xdf8(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
add    $0x10100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [13 x i8] c"girldark.dat\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [9 x i8] c"plot.dat\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [9 x i8] c"%3d %3d\0A\00"
@.str.5 = [13 x i8] c"zsh ex2-1.sh\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [256 x [256 x i8]]
%3 = alloca [256 x i8]
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
%9 = getelementptr inbounds [256 x [256 x i8]], [256 x [256 x i8]]* %2, i64 0, i64 0
%10 = bitcast [256 x i8]* %9 to i8**
%11 = load i32*, i32** %4
%12 = call i32 @fread(i8** %10, i32 1, i32 65536, i32* %11)
%13 = load i32*, i32** %4
%14 = call i32 @fclose(i32* %13)
store i32 0, i32* %5
br label %15
15: ; preds = %38, %0
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 256
br i1 %17, label %18, label %41
18: ; preds = %15
store i32 0, i32* %6
br label %19
19: ; preds = %34, %18
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 256
br i1 %21, label %22, label %37
22: ; preds = %19
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [256 x [256 x i8]], [256 x [256 x i8]]* %2, i64 0, i64 %24
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %25, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = zext i8 %29 to i64
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %30
%32 = load i8, i8* %31
%33 = add i8 %32, 1
store i8 %33, i8* %31
br label %34
34: ; preds = %22
%35 = load i32, i32* %6
%36 = add nsw i32 %35, 1
store i32 %36, i32* %6
br label %19
37: ; preds = %19
br label %38
38: ; preds = %37
%39 = load i32, i32* %5
%40 = add nsw i32 %39, 1
store i32 %40, i32* %5
br label %15
41: ; preds = %15
%42 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %42, i32** %4
store i32 0, i32* %7
br label %43
43: ; preds = %54, %41
%44 = load i32, i32* %7
%45 = icmp slt i32 %44, 256
br i1 %45, label %46, label %57
46: ; preds = %43
%47 = load i32*, i32** %4
%48 = load i32, i32* %7
%49 = load i32, i32* %7
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = call i32 @fprintf(i32* %47, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %48, i8 zeroext %52)
br label %54
54: ; preds = %46
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %43
57: ; preds = %43
%58 = load i32*, i32** %4
%59 = call i32 @fclose(i32* %58)
%60 = call i32 @system(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"girldark.dat\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [9 x i8] c"plot.dat\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [9 x i8] c"%3d %3d\0A\00"
@.str.5 = [13 x i8] c"zsh ex2-1.sh\00"
define i32 @main() {
%1 = alloca [256 x [256 x i8]]
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x [256 x i8]], [256 x [256 x i8]]* %1, i64 0, i64 0, i64 0
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = bitcast [256 x [256 x i8]]* %1 to i8**
%7 = call i32 @fread(i8** %6, i32 1, i32 65536, i32* %5)
%8 = tail call i32 @fclose(i32* %5)
br label %9
9: ; preds = %0, %13
%10 = phi i64 [ 0, %0 ], [ %14, %13 ]
br label %16
11: ; preds = %13
%12 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %36
13: ; preds = %16
%14 = add nuw nsw i64 %10, 1
%15 = icmp eq i64 %14, 256
br i1 %15, label %11, label %9
16: ; preds = %16, %9
%17 = phi i64 [ 0, %9 ], [ %31, %16 ]
%18 = getelementptr inbounds [256 x [256 x i8]], [256 x [256 x i8]]* %1, i64 0, i64 %10, i64 %17
%19 = load i8, i8* %18
%20 = zext i8 %19 to i64
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = add i8 %22, 1
store i8 %23, i8* %21
%24 = or i64 %17, 1
%25 = getelementptr inbounds [256 x [256 x i8]], [256 x [256 x i8]]* %1, i64 0, i64 %10, i64 %24
%26 = load i8, i8* %25
%27 = zext i8 %26 to i64
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = add i8 %29, 1
store i8 %30, i8* %28
%31 = add nuw nsw i64 %17, 2
%32 = icmp eq i64 %31, 256
br i1 %32, label %13, label %16
33: ; preds = %36
%34 = tail call i32 @fclose(i32* %12)
%35 = tail call i32 @system(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
ret i32 0
36: ; preds = %11, %36
%37 = phi i64 [ 0, %11 ], [ %42, %36 ]
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = trunc i64 %37 to i32
%41 = tail call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %40, i8 zeroext %39)
%42 = add nuw nsw i64 %37, 1
%43 = icmp eq i64 %42, 256
br i1 %43, label %33, label %36
}