int main(int argc, char *argv[]) {
  int i;
  if (argc != 2) {
    printf("Usage: histogram input.pgm\n");
    return -1;
  }
  unsigned char *image;
  int width;
  int height;
  int grayMax;
  readPGM(&image, argv[1], &width, &height, &grayMax);
  printf("Image File Name: %s\n", argv[1]);
  printf("Image Width: %d\n", width);
  printf("Image Height: %d\n", height);
  unsigned int *histogram =
      (unsigned int *)malloc((256) * sizeof(unsigned int));
  memset(histogram, 0, (256) * sizeof(unsigned char));
  histogram_calculation(image, width, height, histogram);
  printf("\n");
  printf("index - occurance\n");
  printf("=================\n");
  for (i = 0; i < (256); i++) {
    printf(" %3d  %8d\n", i, histogram[i]);
  }
  free(image);
  free(histogram);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     119a <main+0x3a>
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <main+0x145>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdi
lea    -0x24(%rbp),%rdx
lea    -0x28(%rbp),%rcx
lea    -0x2c(%rbp),%r8
mov    $0x0,%al
call   1040 <readPGM@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%esi
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x400,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x0,%al
call   1070 <histogram_calculation@plt>
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x100,-0x14(%rbp)
jge    128c <main+0x12c>
mov    -0x14(%rbp),%esi
mov    -0x38(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1255 <main+0xf5>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jne    12c3 <main+0x153>
mov    %rsi,%rbx
mov    0x8(%rsi),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x1c(%rsp),%r8
xor    %eax,%eax
call   1060 <readPGM@plt>
mov    0x8(%rbx),%rsi
lea    0xe51(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0xc(%rsp),%esi
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x8(%rsp),%esi
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x400,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
xorps  %xmm0,%xmm0
movups %xmm0,(%rax)
movups %xmm0,0x10(%rax)
movups %xmm0,0x20(%rax)
movups %xmm0,0x30(%rax)
movups %xmm0,0x40(%rax)
movups %xmm0,0x50(%rax)
movups %xmm0,0x60(%rax)
movups %xmm0,0x70(%rax)
movups %xmm0,0x80(%rax)
movups %xmm0,0x90(%rax)
movups %xmm0,0xa0(%rax)
movups %xmm0,0xb0(%rax)
movups %xmm0,0xc0(%rax)
movups %xmm0,0xd0(%rax)
movups %xmm0,0xe0(%rax)
movups %xmm0,0xf0(%rax)
mov    0x10(%rsp),%rdi
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <histogram_calculation@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
lea    0xdda(%rip),%rdi
call   1050 <puts@plt>
lea    0xde0(%rip),%rdi
call   1050 <puts@plt>
lea    0xdb7(%rip),%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r15,%rbx,4),%edx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbx
cmp    $0x100,%rbx
jne    1290 <main+0x120>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    12d4 <main+0x164>
lea    0xd9d(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Usage: histogram input.pgm\0A\00"
@.str.1 = [21 x i8] c"Image File Name: %s\0A\00"
@.str.2 = [17 x i8] c"Image Width: %d\0A\00"
@.str.3 = [18 x i8] c"Image Height: %d\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [19 x i8] c"index - occurance\0A\00"
@.str.6 = [19 x i8] c"=================\0A\00"
@.str.7 = [11 x i8] c" %3d %8d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %59
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 (i8**, i8*, i32*, i32*, i32*, ...) bitcast (i32 (...)* @readPGM to i32 (i8**, i8*, i32*, i32*, i32*, ...)*)(i8** %7, i8* %19, i32* %8, i32* %9, i32* %10)
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i32, i32* %8
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %9
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = call i8* @malloc(i64 1024)
%30 = bitcast i8* %29 to i32*
store i32* %30, i32** %11
%31 = load i32*, i32** %11
%32 = bitcast i32* %31 to i8*
%33 = load i8*, i8** %7
%34 = load i32, i32* %8
%35 = load i32, i32* %9
%36 = load i32*, i32** %11
%37 = call i32 (i8*, i32, i32, i32*, ...) bitcast (i32 (...)* @histogram_calculation to i32 (i8*, i32, i32, i32*, ...)*)(i8* %33, i32 %34, i32 %35, i32* %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %6
br label %41
41: ; preds = %52, %16
%42 = load i32, i32* %6
%43 = icmp slt i32 %42, 256
br i1 %43, label %44, label %55
44: ; preds = %41
%45 = load i32, i32* %6
%46 = load i32*, i32** %11
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = load i32, i32* %49
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 %45, i32 %50)
br label %52
52: ; preds = %44
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
br label %41
55: ; preds = %41
%56 = load i8*, i8** %7
%57 = load i32*, i32** %11
%58 = bitcast i32* %57 to i8*
store i32 0, i32* %3
br label %59
59: ; preds = %55, %14
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str.1 = [21 x i8] c"Image File Name: %s\0A\00"
@.str.2 = [17 x i8] c"Image Width: %d\0A\00"
@.str.3 = [18 x i8] c"Image Height: %d\0A\00"
@.str.7 = [11 x i8] c" %3d %8d\0A\00"
@str = [18 x i8] c"index - occurance\00"
@str.8 = [18 x i8] c"=================\00"
@str.9 = [27 x i8] c"Usage: histogram input.pgm\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = icmp eq i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.9, i64 0, i64 0))
br label %43
10: ; preds = %2
%11 = bitcast i8** %3 to i8*
%12 = bitcast i32* %4 to i8*
%13 = bitcast i32* %5 to i8*
%14 = bitcast i32* %6 to i8*
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = call i32 (i8**, i8*, i32*, i32*, i32*, ...) bitcast (i32 (...)* @readPGM to i32 (i8**, i8*, i32*, i32*, i32*, ...)*)(i8** %3, i8* %16, i32* %4, i32* %5, i32* %6)
%18 = load i8*, i8** %15
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = load i32, i32* %4
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %5
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
%25 = bitcast i8* %24 to i32*
%26 = load i8*, i8** %3
%27 = load i32, i32* %4
%28 = load i32, i32* %5
%29 = call i32 (i8*, i32, i32, i32*, ...) bitcast (i32 (...)* @histogram_calculation to i32 (i8*, i32, i32, i32*, ...)*)(i8* %26, i32 %27, i32 %28, i32* %25)
%30 = call i32 @putchar(i32 10)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
br label %33
33: ; preds = %10, %33
%34 = phi i64 [ 0, %10 ], [ %39, %33 ]
%35 = getelementptr inbounds i32, i32* %25, i64 %34
%36 = load i32, i32* %35
%37 = trunc i64 %34 to i32
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i32 %37, i32 %36)
%39 = add nuw nsw i64 %34, 1
%40 = icmp eq i64 %39, 256
br i1 %40, label %41, label %33
41: ; preds = %33
%42 = load i8*, i8** %3
br label %43
43: ; preds = %41, %8
%44 = phi i32 [ -1, %8 ], [ 0, %41 ]
ret i32 %44
}