#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
void read_image(int image[827][943], char file_name[], int *p_h, int *p_w,
                int *p_levels) {
  FILE *fin;
  int i, j, h, w, levels;
  char buffer[80];
  fin = fopen("logo.pgm", "r");
  if (fin == NULL) {
    printf("file open error\n");
    exit(-1);
  }
  fscanf(fin, "%s", buffer);
  if (strcmp(buffer, "P2")) {
    printf("the image format must be P2\n");
    exit(-1);
  }
  fgets(buffer, 80, fin);
  fgets(buffer, 80, fin);
  fscanf(fin, "%d%d", &w, &h);
  fscanf(fin, "%d", &levels);
  printf("image reading ... h = %d w = %d\n", h, w);
  for (i = 0; i < h; i++)
    for (j = 0; j < w; j++)
      fscanf(fin, "%d", &image[i][j]);
  fclose(fin);
  *p_h = h;
  *p_w = w;
  *p_levels = levels;
  return;
}


x86 assembly (clang, -O0):
<read_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c9 <read_image+0x59>
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rdi
lea    -0xa0(%rbp),%rdx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1217 <read_image+0xa7>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x50,%esi
call   1060 <fgets@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x50,%esi
call   1060 <fgets@plt>
mov    -0x30(%rbp),%rdi
lea    0xdf3(%rip),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x3c(%rbp),%rcx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x30(%rbp),%rdi
lea    0xddb(%rip),%rsi
lea    -0x44(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x3c(%rbp),%esi
mov    -0x40(%rbp),%edx
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jge    12fb <read_image+0x18b>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x40(%rbp),%eax
jge    12e8 <read_image+0x178>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rdx
movslq -0x34(%rbp),%rax
imul   $0xebc,%rax,%rax
add    %rax,%rdx
movslq -0x38(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdx
lea    0xd6e(%rip),%rsi
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    129f <read_image+0x12f>
jmp    12ed <read_image+0x17d>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    128c <read_image+0x11c>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x3c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x44(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %r8,%r15
mov    %rcx,%rbp
mov    %rdx,0x28(%rsp)
mov    %rdi,%r14
lea    0xe5a(%rip),%rdi
lea    0xeaf(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1303 <read_image+0x183>
mov    %rax,%rbx
mov    %rbp,0x18(%rsp)
mov    %r15,0x20(%rsp)
lea    0xe3a(%rip),%rsi
lea    0x30(%rsp),%rbp
mov    %rax,%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <fscanf@plt>
lea    0xe63(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    130c <read_image+0x18c>
lea    0x30(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x50,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
mov    %rbp,%rdi
mov    $0x50,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
lea    0xde7(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x10(%rsp),%rcx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <fscanf@plt>
lea    0xdce(%rip),%rsi
lea    0x14(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <fscanf@plt>
mov    0x10(%rsp),%esi
mov    0xc(%rsp),%edx
lea    0xdb3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%ecx
test   %ecx,%ecx
jle    12c8 <read_image+0x148>
mov    0xc(%rsp),%eax
lea    0xd96(%rip),%rbp
xor    %r15d,%r15d
jmp    1293 <read_image+0x113>
nopl   (%rax)
add    $0x1,%r15
movslq %ecx,%rdx
add    $0xebc,%r14
cmp    %rdx,%r15
jge    12c8 <read_image+0x148>
test   %eax,%eax
jle    1280 <read_image+0x100>
mov    %r14,%r13
xor    %r12d,%r12d
nopl   (%rax)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <fscanf@plt>
add    $0x1,%r12
movslq 0xc(%rsp),%rax
add    $0x4,%r13
cmp    %rax,%r12
jl     12a0 <read_image+0x120>
mov    0x10(%rsp),%ecx
jmp    1280 <read_image+0x100>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x10(%rsp),%eax
mov    0x28(%rsp),%rcx
mov    %eax,(%rcx)
mov    0xc(%rsp),%eax
mov    0x18(%rsp),%rcx
mov    %eax,(%rcx)
mov    0x14(%rsp),%eax
mov    0x20(%rsp),%rcx
mov    %eax,(%rcx)
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd44(%rip),%rdi
jmp    1313 <read_image+0x193>
lea    0xd1f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"logo.pgm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [17 x i8] c"file open error\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [3 x i8] c"P2\00"
@.str.5 = [29 x i8] c"the image format must be P2\0A\00"
@.str.6 = [5 x i8] c"%d%d\00"
@.str.7 = [3 x i8] c"%d\00"
@.str.8 = [33 x i8] c"image reading ... h = %d w = %d\0A\00"
define void @read_image([943 x i32]* %0, i8* %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca [943 x i32]*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca [80 x i8]
store [943 x i32]* %0, [943 x i32]** %6
store i8* %1, i8** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i32* %4, i32** %10
%18 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %11
%19 = load i32*, i32** %11
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %5
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %5
%25 = load i32*, i32** %11
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %17, i64 0, i64 0
%27 = call i32 (i32*, i8*, ...) @fscanf(i32* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %17, i64 0, i64 0
%29 = call i64 @strcmp(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %24
%35 = getelementptr inbounds [80 x i8], [80 x i8]* %17, i64 0, i64 0
%36 = load i32*, i32** %11
%37 = call i32 @fgets(i8* %35, i32 80, i32* %36)
%38 = getelementptr inbounds [80 x i8], [80 x i8]* %17, i64 0, i64 0
%39 = load i32*, i32** %11
%40 = call i32 @fgets(i8* %38, i32 80, i32* %39)
%41 = load i32*, i32** %11
%42 = call i32 (i32*, i8*, ...) @fscanf(i32* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32* %15, i32* %14)
%43 = load i32*, i32** %11
%44 = call i32 (i32*, i8*, ...) @fscanf(i32* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32* %16)
%45 = load i32, i32* %14
%46 = load i32, i32* %15
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %45, i32 %46)
store i32 0, i32* %12
br label %48
48: ; preds = %71, %34
%49 = load i32, i32* %12
%50 = load i32, i32* %14
%51 = icmp slt i32 %49, %50
br i1 %51, label %52, label %74
52: ; preds = %48
store i32 0, i32* %13
br label %53
53: ; preds = %67, %52
%54 = load i32, i32* %13
%55 = load i32, i32* %15
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %70
57: ; preds = %53
%58 = load i32*, i32** %11
%59 = load [943 x i32]*, [943 x i32]** %6
%60 = load i32, i32* %12
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [943 x i32], [943 x i32]* %59, i64 %61
%63 = load i32, i32* %13
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [943 x i32], [943 x i32]* %62, i64 0, i64 %64
%66 = call i32 (i32*, i8*, ...) @fscanf(i32* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32* %65)
br label %67
67: ; preds = %57
%68 = load i32, i32* %13
%69 = add nsw i32 %68, 1
store i32 %69, i32* %13
br label %53
70: ; preds = %53
br label %71
71: ; preds = %70
%72 = load i32, i32* %12
%73 = add nsw i32 %72, 1
store i32 %73, i32* %12
br label %48
74: ; preds = %48
%75 = load i32*, i32** %11
%76 = call i32 @fclose(i32* %75)
%77 = load i32, i32* %14
%78 = load i32*, i32** %8
store i32 %77, i32* %78
%79 = load i32, i32* %15
%80 = load i32*, i32** %9
store i32 %79, i32* %80
%81 = load i32, i32* %16
%82 = load i32*, i32** %10
store i32 %81, i32* %82
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"logo.pgm\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [3 x i8] c"P2\00"
@.str.6 = [5 x i8] c"%d%d\00"
@.str.7 = [3 x i8] c"%d\00"
@.str.8 = [33 x i8] c"image reading ... h = %d w = %d\0A\00"
@str = [28 x i8] c"the image format must be P2\00"
@str.9 = [16 x i8] c"file open error\00"
define void @read_image([943 x i32]* %0, i8* nocapture readnone %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3, i32* nocapture writeonly %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [80 x i8]
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = bitcast i32* %8 to i8*
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%14 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %5
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %5
%20 = call i32 (i32*, i8*, ...) @fscanf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%21 = call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %19
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %19
%27 = call i32 @fgets(i8* %13, i32 80, i32* %14)
%28 = call i32 @fgets(i8* %13, i32 80, i32* %14)
%29 = call i32 (i32*, i8*, ...) @fscanf(i32* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i32* %7, i32* %6)
%30 = call i32 (i32*, i8*, ...) @fscanf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32* %8)
%31 = load i32, i32* %6
%32 = load i32, i32* %7
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.8, i64 0, i64 0), i32 %31, i32 %32)
%34 = load i32, i32* %6
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %59
36: ; preds = %26
%37 = load i32, i32* %7
br label %38
38: ; preds = %36, %53
%39 = phi i32 [ %34, %36 ], [ %54, %53 ]
%40 = phi i32 [ %37, %36 ], [ %55, %53 ]
%41 = phi i64 [ 0, %36 ], [ %56, %53 ]
%42 = icmp sgt i32 %40, 0
br i1 %42, label %43, label %53
43: ; preds = %38, %43
%44 = phi i64 [ %47, %43 ], [ 0, %38 ]
%45 = getelementptr inbounds [943 x i32], [943 x i32]* %0, i64 %41, i64 %44
%46 = call i32 (i32*, i8*, ...) @fscanf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i32* %45)
%47 = add nuw nsw i64 %44, 1
%48 = load i32, i32* %7
%49 = sext i32 %48 to i64
%50 = icmp slt i64 %47, %49
br i1 %50, label %43, label %51
51: ; preds = %43
%52 = load i32, i32* %6
br label %53
53: ; preds = %51, %38
%54 = phi i32 [ %52, %51 ], [ %39, %38 ]
%55 = phi i32 [ %48, %51 ], [ %40, %38 ]
%56 = add nuw nsw i64 %41, 1
%57 = sext i32 %54 to i64
%58 = icmp slt i64 %56, %57
br i1 %58, label %38, label %59
59: ; preds = %53, %26
%60 = call i32 @fclose(i32* %14)
%61 = load i32, i32* %6
store i32 %61, i32* %2
%62 = load i32, i32* %7
store i32 %62, i32* %3
%63 = load i32, i32* %8
store i32 %63, i32* %4
ret void
}