#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 atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char **, char *);
int /*<<< orphan*/ fread(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 5) {
    puts("Usage: ./read_bin max_stage row col sleep_mode");
    puts("Sleep mode: 1(ON) or 0(OFF). Indicates whether the program sleeps "
         "after printing out each state");
    return -1;
  }
  int counter;
  int row = atoi(argv[3]);
  int col = atoi(argv[2]);
  int max_cell = row * col;
  int max_stage = atoi(argv[1]);
  int sleep_mode = atoi(argv[4]);
  FILE *fptr;
  int i, j;
  for (j = 0; j < max_stage; j++) {
    char filename[50];
    sprintf(filename, "stage%d", j);
    fptr = fopen(&filename, "rb");
    printf("Stage %d\n", j);
    for (counter = 0; counter < max_cell; counter++) {
      fread(&i, sizeof(int), 1, fptr);
      if (i == 0)
        printf("-");
      else
        printf("O");
      if (counter % col == (col - 1))
        puts("");
    }
    if (sleep_mode == 1)
      sleep(1);
    puts("");
    fclose(fptr);
  }
  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   $0x5,-0x8(%rbp)
jge    11c4 <main+0x44>
lea    0xe59(%rip),%rdi
call   1030 <puts@plt>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <main+0x1a0>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
imul   -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1319 <main+0x199>
lea    -0x70(%rbp),%rdi
mov    -0x38(%rbp),%edx
lea    0xe60(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x70(%rbp),%rdi
lea    0xe58(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    12e2 <main+0x162>
mov    -0x30(%rbp),%rcx
lea    -0x34(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
cmpl   $0x0,-0x34(%rbp)
jne    12a0 <main+0x120>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12ae <main+0x12e>
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
cltd
idivl  -0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cmp    %eax,%edx
jne    12cf <main+0x14f>
lea    0xdd9(%rip),%rdi
call   1030 <puts@plt>
jmp    12d4 <main+0x154>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1260 <main+0xe0>
cmpl   $0x1,-0x28(%rbp)
jne    12f6 <main+0x176>
mov    $0x1,%edi
call   10a0 <sleep@plt>
lea    0xda6(%rip),%rdi
call   1030 <puts@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1215 <main+0x95>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%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    $0x48,%rsp
cmp    $0x4,%edi
jg     11c5 <main+0x35>
lea    0xe56(%rip),%rdi
call   1040 <puts@plt>
lea    0xe79(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
jmp    12fb <main+0x16b>
mov    %rsi,%r14
mov    0x18(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%r12d
mov    0x10(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,%r15d
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    0x20(%r14),%rdi
call   1090 <atoi@plt>
mov    %eax,(%rsp)
mov    %ebp,0x4(%rsp)
test   %ebp,%ebp
jle    12f9 <main+0x169>
imul   %r15d,%r12d
mov    %r15d,%ebp
add    $0xffffffff,%ebp
xor    %ebx,%ebx
jmp    1243 <main+0xb3>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    0x8(%rsp),%ebx
add    $0x1,%ebx
cmp    0x4(%rsp),%ebx
je     12f9 <main+0x169>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
lea    0xe3d(%rip),%rsi
mov    %ebx,%edx
call   10a0 <sprintf@plt>
mov    %r14,%rdi
lea    0xe34(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
lea    0xe28(%rip),%rdi
mov    %ebx,0x8(%rsp)
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %r12d,%r12d
mov    $0x2d,%ebx
jle    12e0 <main+0x150>
xor    %r13d,%r13d
jmp    1299 <main+0x109>
xchg   %ax,%ax
add    $0x1,%r13d
cmp    %r13d,%r12d
je     12e0 <main+0x150>
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
cmpl   $0x0,0xc(%rsp)
mov    $0x4f,%edi
cmove  %ebx,%edi
call   1030 <putchar@plt>
mov    %r13d,%eax
cltd
idiv   %r15d
cmp    %ebp,%edx
jne    1290 <main+0x100>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1290 <main+0x100>
nopl   0x0(%rax)
cmpl   $0x1,(%rsp)
jne    1220 <main+0x90>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    1220 <main+0x90>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [47 x i8] c"Usage: ./read_bin max_stage row col sleep_mode\00"
@.str.1 = [96 x i8] c"Sleep mode: 1(ON) or 0(OFF). Indicates whether the program sleeps after printing out each state\00"
@.str.2 = [8 x i8] c"stage%d\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [10 x i8] c"Stage %d\0A\00"
@.str.5 = [2 x i8] c"-\00"
@.str.6 = [2 x i8] c"O\00"
@.str.7 = [1 x i8] zeroinitializer
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca [50 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 5
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @puts(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @puts(i8* getelementptr inbounds ([96 x i8], [96 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %92
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 3
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %7
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %8
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = mul nsw i32 %30, %31
store i32 %32, i32* %9
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
store i32 %36, i32* %10
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 4
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
store i32 %40, i32* %11
store i32 0, i32* %14
br label %41
41: ; preds = %88, %21
%42 = load i32, i32* %14
%43 = load i32, i32* %10
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %91
45: ; preds = %41
%46 = getelementptr inbounds [50 x i8], [50 x i8]* %15, i64 0, i64 0
%47 = load i32, i32* %14
%48 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = bitcast [50 x i8]* %15 to i8**
%50 = call i32* @fopen(i8** %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %50, i32** %12
%51 = load i32, i32* %14
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %51)
store i32 0, i32* %6
br label %53
53: ; preds = %76, %45
%54 = load i32, i32* %6
%55 = load i32, i32* %9
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %79
57: ; preds = %53
%58 = load i32*, i32** %12
%59 = call i32 @fread(i32* %13, i32 4, i32 1, i32* %58)
%60 = load i32, i32* %13
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %66
64: ; preds = %57
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %66
66: ; preds = %64, %62
%67 = load i32, i32* %6
%68 = load i32, i32* %8
%69 = srem i32 %67, %68
%70 = load i32, i32* %8
%71 = sub nsw i32 %70, 1
%72 = icmp eq i32 %69, %71
br i1 %72, label %73, label %75
73: ; preds = %66
%74 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0))
br label %75
75: ; preds = %73, %66
br label %76
76: ; preds = %75
%77 = load i32, i32* %6
%78 = add nsw i32 %77, 1
store i32 %78, i32* %6
br label %53
79: ; preds = %53
%80 = load i32, i32* %11
%81 = icmp eq i32 %80, 1
br i1 %81, label %82, label %84
82: ; preds = %79
%83 = call i32 @sleep(i32 1)
br label %84
84: ; preds = %82, %79
%85 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0))
%86 = load i32*, i32** %12
%87 = call i32 @fclose(i32* %86)
br label %88
88: ; preds = %84
%89 = load i32, i32* %14
%90 = add nsw i32 %89, 1
store i32 %90, i32* %14
br label %41
91: ; preds = %41
store i32 0, i32* %3
br label %92
92: ; preds = %91, %18
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
@.str = [47 x i8] c"Usage: ./read_bin max_stage row col sleep_mode\00"
@.str.1 = [96 x i8] c"Sleep mode: 1(ON) or 0(OFF). Indicates whether the program sleeps after printing out each state\00"
@.str.2 = [8 x i8] c"stage%d\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [10 x i8] c"Stage %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [50 x i8]
%5 = icmp slt i32 %0, 5
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([96 x i8], [96 x i8]* @.str.1, i64 0, i64 0))
br label %59
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 3
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = mul i32 %15, %12
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = getelementptr inbounds i8*, i8** %1, i64 4
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = bitcast i32* %3 to i8*
%24 = icmp sgt i32 %19, 0
br i1 %24, label %25, label %58
25: ; preds = %9
%26 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%27 = bitcast [50 x i8]* %4 to i8**
%28 = icmp sgt i32 %16, 0
%29 = add nsw i32 %15, -1
%30 = icmp eq i32 %22, 1
br label %31
31: ; preds = %25, %53
%32 = phi i32 [ 0, %25 ], [ %56, %53 ]
%33 = call i32 @sprintf(i8* %26, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = call i32* @fopen(i8** %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %32)
br i1 %28, label %36, label %50
36: ; preds = %31, %47
%37 = phi i32 [ %48, %47 ], [ 0, %31 ]
%38 = call i32 @fread(i32* %3, i32 4, i32 1, i32* %34)
%39 = load i32, i32* %3
%40 = icmp eq i32 %39, 0
%41 = select i1 %40, i32 45, i32 79
%42 = tail call i32 @putchar(i32 %41)
%43 = srem i32 %37, %15
%44 = icmp eq i32 %43, %29
br i1 %44, label %45, label %47
45: ; preds = %36
%46 = tail call i32 @putchar(i32 10)
br label %47
47: ; preds = %36, %45
%48 = add nuw nsw i32 %37, 1
%49 = icmp eq i32 %48, %16
br i1 %49, label %50, label %36
50: ; preds = %47, %31
br i1 %30, label %51, label %53
51: ; preds = %50
%52 = tail call i32 @sleep(i32 1)
br label %53
53: ; preds = %51, %50
%54 = tail call i32 @putchar(i32 10)
%55 = tail call i32 @fclose(i32* %34)
%56 = add nuw nsw i32 %32, 1
%57 = icmp eq i32 %56, %19
br i1 %57, label %58, label %31
58: ; preds = %53, %9
br label %59
59: ; preds = %58, %6
%60 = phi i32 [ -1, %6 ], [ 0, %58 ]
ret i32 %60
}