#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ trm_get(char *);
int /*<<< orphan*/ trm_str(char *);
int advrestore(char *hdr, int hlen, char *save, int slen) {
  char fname[50], hbuf[50], *p;
  int fd;
  if (hlen > 50)
    error("save file header buffer too small");
  trm_str("File name? ");
  trm_get(fname);
  strcat(fname, ".sav");
  if ((fd = open(fname, O_RDONLY)) == 0)
    return (0);
  if (read(fd, hbuf, hlen) != hlen) {
    close(fd);
    return (0);
  }
  for (p = hbuf; hlen--;)
    if (*hdr++ != *p++) {
      trm_str("This save file does not match the adventure!\n");
      close(fd);
      return (0);
    }
  if (read(fd, save, slen) != slen) {
    close(fd);
    return (0);
  }
  close(fd);
  return (1);
}


x86 assembly (clang, -O0):
<advrestore>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpl   $0x32,-0x14(%rbp)
jle    119f <advrestore+0x2f>
lea    0xe66(%rip),%rdi
call   1060 <error@plt>
lea    0xe7c(%rip),%rdi
call   1090 <trm_str@plt>
lea    -0x60(%rbp),%rdi
call   1030 <trm_get@plt>
lea    -0x60(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x60(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0xac(%rbp)
cmp    $0x0,%eax
jne    11f1 <advrestore+0x81>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <advrestore+0x176>
mov    -0xac(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <read@plt>
cmp    -0x14(%rbp),%eax
je     1226 <advrestore+0xb6>
mov    -0xac(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <advrestore+0x176>
lea    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     12a2 <advrestore+0x132>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
mov    -0xa8(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0xa8(%rbp)
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     129d <advrestore+0x12d>
lea    0xdb2(%rip),%rdi
call   1090 <trm_str@plt>
mov    -0xac(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <advrestore+0x176>
jmp    1234 <advrestore+0xc4>
mov    -0xac(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <read@plt>
cmp    -0x24(%rbp),%eax
je     12d4 <advrestore+0x164>
mov    -0xac(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <advrestore+0x176>
mov    -0xac(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<advrestore>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %ecx,%r15d
mov    %rdx,0x8(%rsp)
mov    %esi,%r14d
mov    %rdi,%rbx
cmp    $0x33,%esi
jl     11a0 <advrestore+0x30>
lea    0xe65(%rip),%rdi
call   1060 <error@plt>
lea    0xe7b(%rip),%rdi
call   1090 <trm_str@plt>
lea    0x50(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <trm_get@plt>
lea    0xe6e(%rip),%rsi
mov    %rbp,%rdi
call   1080 <strcat@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1070 <open@plt>
xor    %r13d,%r13d
test   %eax,%eax
je     1250 <advrestore+0xe0>
mov    %eax,%r12d
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    %r14d,%edx
call   1050 <read@plt>
cmp    %r14d,%eax
jne    1248 <advrestore+0xd8>
mov    %r14d,%eax
xor    %ecx,%ecx
nopl   0x0(%rax)
cmp    %ecx,%eax
je     122e <advrestore+0xbe>
movzbl (%rbx,%rcx,1),%edx
lea    0x1(%rcx),%rsi
cmp    0x10(%rsp,%rcx,1),%dl
mov    %rsi,%rcx
je     1200 <advrestore+0x90>
lea    0xe17(%rip),%rdi
call   1090 <trm_str@plt>
mov    %r12d,%edi
call   1040 <close@plt>
xor    %r13d,%r13d
jmp    1250 <advrestore+0xe0>
mov    %r12d,%edi
mov    0x8(%rsp),%rsi
mov    %r15d,%edx
call   1050 <read@plt>
xor    %r13d,%r13d
cmp    %r15d,%eax
sete   %r13b
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r13d,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"save file header buffer too small\00"
@.str.1 = [12 x i8] c"File name? \00"
@.str.2 = [5 x i8] c".sav\00"
@O_RDONLY = global i32 0
@.str.3 = [46 x i8] c"This save file does not match the adventure!\0A\00"
define i32 @advrestore(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [50 x i8]
%11 = alloca [50 x i8]
%12 = alloca i8*
%13 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%14 = load i32, i32* %7
%15 = icmp sgt i32 %14, 50
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %4
%19 = call i32 @trm_str(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%21 = call i32 @trm_get(i8* %20)
%22 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%23 = call i32 @strcat(i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%24 = getelementptr inbounds [50 x i8], [50 x i8]* %10, i64 0, i64 0
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %13
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %18
store i32 0, i32* %5
br label %73
29: ; preds = %18
%30 = load i32, i32* %13
%31 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
%32 = load i32, i32* %7
%33 = call i32 @read(i32 %30, i8* %31, i32 %32)
%34 = load i32, i32* %7
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %29
%37 = load i32, i32* %13
%38 = call i32 @close(i32 %37)
store i32 0, i32* %5
br label %73
39: ; preds = %29
%40 = getelementptr inbounds [50 x i8], [50 x i8]* %11, i64 0, i64 0
store i8* %40, i8** %12
br label %41
41: ; preds = %59, %39
%42 = load i32, i32* %7
%43 = add nsw i32 %42, -1
store i32 %43, i32* %7
%44 = icmp ne i32 %42, 0
br i1 %44, label %45, label %60
45: ; preds = %41
%46 = load i8*, i8** %6
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %6
%48 = load i8, i8* %46
%49 = sext i8 %48 to i32
%50 = load i8*, i8** %12
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %12
%52 = load i8, i8* %50
%53 = sext i8 %52 to i32
%54 = icmp ne i32 %49, %53
br i1 %54, label %55, label %59
55: ; preds = %45
%56 = call i32 @trm_str(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
%57 = load i32, i32* %13
%58 = call i32 @close(i32 %57)
store i32 0, i32* %5
br label %73
59: ; preds = %45
br label %41
60: ; preds = %41
%61 = load i32, i32* %13
%62 = load i8*, i8** %8
%63 = load i32, i32* %9
%64 = call i32 @read(i32 %61, i8* %62, i32 %63)
%65 = load i32, i32* %9
%66 = icmp ne i32 %64, %65
br i1 %66, label %67, label %70
67: ; preds = %60
%68 = load i32, i32* %13
%69 = call i32 @close(i32 %68)
store i32 0, i32* %5
br label %73
70: ; preds = %60
%71 = load i32, i32* %13
%72 = call i32 @close(i32 %71)
store i32 1, i32* %5
br label %73
73: ; preds = %70, %67, %55, %36, %28
%74 = load i32, i32* %5
ret i32 %74
}

LLVM IR (-O2):
@.str = [34 x i8] c"save file header buffer too small\00"
@.str.1 = [12 x i8] c"File name? \00"
@.str.2 = [5 x i8] c".sav\00"
@O_RDONLY = global i32 0
@.str.3 = [46 x i8] c"This save file does not match the adventure!\0A\00"
define i32 @advrestore(i8* nocapture %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = alloca [50 x i8]
%6 = alloca [50 x i8]
%7 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%9 = icmp sgt i32 %1, 50
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %4
%13 = tail call i32 @trm_str(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @trm_get(i8* %7)
%15 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %7, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %44, label %19
19: ; preds = %12
%20 = call i32 @read(i32 %17, i8* %8, i32 %1)
%21 = icmp eq i32 %20, %1
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 @close(i32 %17)
br label %44
24: ; preds = %19, %29
%25 = phi i32 [ %30, %29 ], [ %1, %19 ]
%26 = phi i8* [ %31, %29 ], [ %0, %19 ]
%27 = phi i8* [ %33, %29 ], [ %8, %19 ]
%28 = icmp eq i32 %25, 0
br i1 %28, label %39, label %29
29: ; preds = %24
%30 = add nsw i32 %25, -1
%31 = getelementptr inbounds i8, i8* %26, i64 1
%32 = load i8, i8* %26
%33 = getelementptr inbounds i8, i8* %27, i64 1
%34 = load i8, i8* %27
%35 = icmp eq i8 %32, %34
br i1 %35, label %24, label %36
36: ; preds = %29
%37 = call i32 @trm_str(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @close(i32 %17)
br label %44
39: ; preds = %24
%40 = call i32 @read(i32 %17, i8* %2, i32 %3)
%41 = icmp eq i32 %40, %3
%42 = call i32 @close(i32 %17)
%43 = zext i1 %41 to i32
br label %44
44: ; preds = %39, %12, %36, %22
%45 = phi i32 [ 0, %22 ], [ 0, %36 ], [ 0, %12 ], [ %43, %39 ]
ret i32 %45
}