#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int CheckMagicBytes(int);
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ RecoverBackup(int);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "please use: recover backup.bin\n");
    return 1;
  }
  int backupfd;
  if ((backupfd = open(argv[1], O_RDONLY)) == -1) {
    perror("backup opening error: ");
    return 2;
  }
  struct stat backupinode;
  if (fstat(backupfd, &backupinode) == -1) {
    perror("backup stat error: ");
    return 3;
  }
  if (S_ISREG(backupinode.st_mode)) {
    if (CheckMagicBytes(backupfd) == 1) {
      RecoverBackup(backupfd);
      close(backupfd);
    } else {
      fprintf(stderr, "given backup archive is not compatible\n");
      return 5;
    }
  } else {
    fprintf(stderr, "we require a file to recover\n");
    return 4;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c1 <main+0x41>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe50(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a2 <main+0x122>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11fb <main+0x7b>
lea    0xe36(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12a2 <main+0x122>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1228 <main+0xa8>
lea    0xe20(%rip),%rdi
call   1080 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    12a2 <main+0x122>
mov    -0x18(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%rax
je     1286 <main+0x106>
mov    -0x14(%rbp),%edi
call   1050 <CheckMagicBytes@plt>
cmp    $0x1,%eax
jne    1260 <main+0xe0>
mov    -0x14(%rbp),%edi
call   1090 <RecoverBackup@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
jmp    1281 <main+0x101>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x5,-0x4(%rbp)
jmp    12a2 <main+0x122>
jmp    12a2 <main+0x122>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x4,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    11e8 <main+0x68>
mov    0x8(%rsi),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1240 <main+0xc0>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
je     1253 <main+0xd3>
mov    0x8(%rsp),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
je     1204 <main+0x84>
mov    %ebx,%edi
call   1050 <CheckMagicBytes@plt>
cmp    $0x1,%eax
jne    1220 <main+0xa0>
mov    %ebx,%edi
call   1090 <RecoverBackup@plt>
mov    %ebx,%edi
call   1030 <close@plt>
xor    %eax,%eax
jmp    123a <main+0xba>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%eax
jmp    123a <main+0xba>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x4,%eax
jmp    123a <main+0xba>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x5,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
jmp    123a <main+0xba>
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x3,%eax
jmp    123a <main+0xba>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"please use: recover backup.bin\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"backup opening error: \00"
@.str.2 = [20 x i8] c"backup stat error: \00"
@.str.3 = [40 x i8] c"given backup archive is not compatible\0A\00"
@.str.4 = [30 x i8] c"we require a file to recover\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %49
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17)
store i32 %18, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %49
22: ; preds = %13
%23 = load i32, i32* %6
%24 = call i32 @fstat(i32 %23, %struct.stat* %7)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %3
br label %49
28: ; preds = %22
%29 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @S_ISREG(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %46
33: ; preds = %28
%34 = load i32, i32* %6
%35 = call i32 @CheckMagicBytes(i32 %34)
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %42
37: ; preds = %33
%38 = load i32, i32* %6
%39 = call i32 @RecoverBackup(i32 %38)
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
br label %45
42: ; preds = %33
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
store i32 5, i32* %3
br label %49
45: ; preds = %37
br label %49
46: ; preds = %28
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
store i32 4, i32* %3
br label %49
49: ; preds = %10, %20, %26, %42, %46, %45
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"please use: recover backup.bin\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"backup opening error: \00"
@.str.2 = [20 x i8] c"backup stat error: \00"
@.str.3 = [40 x i8] c"given backup archive is not compatible\0A\00"
@.str.4 = [30 x i8] c"we require a file to recover\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp eq i32 %0, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %41
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %41
16: ; preds = %8
%17 = bitcast %struct.stat* %3 to i8*
%18 = call i32 @fstat(i32 %12, %struct.stat* %3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %39
22: ; preds = %16
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%24 = load i32, i32* %23
%25 = tail call i64 @S_ISREG(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %22
%28 = tail call i32 @CheckMagicBytes(i32 %12)
%29 = icmp eq i32 %28, 1
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = tail call i32 @RecoverBackup(i32 %12)
%32 = tail call i32 @close(i32 %12)
br label %39
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0))
br label %39
36: ; preds = %22
%37 = load i32, i32* @stderr
%38 = tail call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %30, %36, %33, %20
%40 = phi i32 [ 3, %20 ], [ 0, %30 ], [ 5, %33 ], [ 4, %36 ]
br label %41
41: ; preds = %14, %39, %5
%42 = phi i32 [ 1, %5 ], [ 2, %14 ], [ %40, %39 ]
ret i32 %42
}