#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 st_size;
  int /*<<< orphan*/ st_gid;
  int /*<<< orphan*/ st_uid;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
int creat(char const *, int);
int /*<<< orphan*/ ensurePath(char *);
int /*<<< orphan*/ exit(int);
int fchmod(int, int /*<<< orphan*/);
int fchown(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ pathFromFile(char const *, char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char **, int);
int /*<<< orphan*/ write(int, char *, int);
void RecoverFile(int bfd, const char *fname, struct stat *inode) {
  char path[2048];
  pathFromFile(fname, &path);
  ensurePath(path);
  int cnt;
  char buf[2048];
  int fileEnd = inode->st_size;
  int targetfd;
  if ((targetfd = creat(fname, 0666)) == -1) {
    perror("Backuptarget: ");
    exit(2);
  }
  while (fileEnd > 0 &&
         (cnt = read(bfd, &buf, (((2048) < (fileEnd)) ? (2048) : (fileEnd)))) >
             0) {
    printf("%i - %i\n", fileEnd, cnt);
    write(targetfd, buf, cnt);
    fileEnd -= 2048;
  }
  if (fchmod(targetfd, inode->st_mode) == -1) {
    perror("fchmod");
  }
  if (fchown(targetfd, inode->st_uid, inode->st_gid) == -1) {
    perror("chown");
  }
  close(targetfd);
  printf("recovered file %s\n", fname);
}


x86 assembly (clang, -O0):
<RecoverFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x820(%rbp),%rsi
call   1070 <pathFromFile@plt>
lea    -0x820(%rbp),%rdi
call   1080 <ensurePath@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1034(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
call   10c0 <creat@plt>
mov    %eax,-0x1038(%rbp)
cmp    $0xffffffff,%eax
jne    1221 <RecoverFile+0x71>
lea    0xdee(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
jmp    1226 <RecoverFile+0x76>
xor    %eax,%eax
cmpl   $0x0,-0x1034(%rbp)
mov    %al,-0x1039(%rbp)
jle    12a9 <RecoverFile+0xf9>
mov    -0x4(%rbp),%eax
mov    %eax,-0x104c(%rbp)
lea    -0x1030(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    $0x800,%eax
cmp    -0x1034(%rbp),%eax
jge    1273 <RecoverFile+0xc3>
mov    $0x800,%eax
mov    %eax,-0x1050(%rbp)
jmp    127f <RecoverFile+0xcf>
mov    -0x1034(%rbp),%eax
mov    %eax,-0x1050(%rbp)
mov    -0x1048(%rbp),%rsi
mov    -0x104c(%rbp),%edi
mov    -0x1050(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x824(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x1039(%rbp)
mov    -0x1039(%rbp),%al
test   $0x1,%al
jne    12bc <RecoverFile+0x10c>
jmp    1304 <RecoverFile+0x154>
mov    -0x1034(%rbp),%esi
mov    -0x824(%rbp),%edx
lea    0xd40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1038(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    -0x824(%rbp),%edx
call   1030 <write@plt>
mov    -0x1034(%rbp),%eax
sub    $0x800,%eax
mov    %eax,-0x1034(%rbp)
jmp    1226 <RecoverFile+0x76>
mov    -0x1038(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%esi
call   1090 <fchmod@plt>
cmp    $0xffffffff,%eax
jne    132b <RecoverFile+0x17b>
lea    0xcf2(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1038(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edx
call   10a0 <fchown@plt>
cmp    $0xffffffff,%eax
jne    1359 <RecoverFile+0x1a9>
lea    0xccb(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x1038(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rsi
lea    0xcb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RecoverFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r12d
lea    0x810(%rsp),%r14
mov    %rsi,%rdi
mov    %r14,%rsi
call   1070 <pathFromFile@plt>
mov    %r14,%rdi
call   1080 <ensurePath@plt>
mov    %rbx,(%rsp)
mov    (%rbx),%ebx
mov    %rbp,0x8(%rsp)
mov    %rbp,%rdi
mov    $0x1b6,%esi
call   10c0 <creat@plt>
cmp    $0xffffffff,%eax
je     12e2 <RecoverFile+0x132>
mov    %eax,%r13d
test   %ebx,%ebx
jle    1272 <RecoverFile+0xc2>
lea    0x10(%rsp),%rbp
mov    %ebx,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
sub    $0x800,%r14d
mov    $0x800,%edx
cmovb  %ebx,%edx
mov    %r12d,%edi
mov    %rbp,%rsi
call   1060 <read@plt>
test   %eax,%eax
jle    1272 <RecoverFile+0xc2>
mov    %eax,%r15d
lea    0xdc7(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
lea    0x800(%r14),%eax
mov    %r14d,%ebx
cmp    $0x800,%eax
jg     1220 <RecoverFile+0x70>
mov    (%rsp),%rbx
mov    0xc(%rbx),%esi
mov    %r13d,%edi
call   1090 <fchmod@plt>
cmp    $0xffffffff,%eax
je     12c6 <RecoverFile+0x116>
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %r13d,%edi
call   10a0 <fchown@plt>
cmp    $0xffffffff,%eax
je     12d4 <RecoverFile+0x124>
mov    %r13d,%edi
call   1050 <close@plt>
lea    0xd7d(%rip),%rdi
mov    0x8(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4b(%rip),%rdi
call   10b0 <perror@plt>
jmp    1286 <RecoverFile+0xd6>
lea    0xd44(%rip),%rdi
call   10b0 <perror@plt>
jmp    1299 <RecoverFile+0xe9>
lea    0xd17(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32, i32 }
@.str = [15 x i8] c"Backuptarget: \00"
@.str.1 = [9 x i8] c"%i - %i\0A\00"
@.str.2 = [7 x i8] c"fchmod\00"
@.str.3 = [6 x i8] c"chown\00"
@.str.4 = [19 x i8] c"recovered file %s\0A\00"
define void @RecoverFile(i32 %0, i8* %1, %struct.stat* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat*
%7 = alloca [2048 x i8]
%8 = alloca i32
%9 = alloca [2048 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store %struct.stat* %2, %struct.stat** %6
%12 = load i8*, i8** %5
%13 = bitcast [2048 x i8]* %7 to i8**
%14 = call i32 @pathFromFile(i8* %12, i8** %13)
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%16 = call i32 @ensurePath(i8* %15)
%17 = load %struct.stat*, %struct.stat** %6
%18 = getelementptr inbounds %struct.stat, %struct.stat* %17, i32 0, i32 0
%19 = load i32, i32* %18
store i32 %19, i32* %10
%20 = load i8*, i8** %5
%21 = call i32 @creat(i8* %20, i32 438)
store i32 %21, i32* %11
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %3
%24 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 2)
unreachable
26: ; preds = %3
br label %27
27: ; preds = %44, %26
%28 = load i32, i32* %10
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %42
30: ; preds = %27
%31 = load i32, i32* %4
%32 = bitcast [2048 x i8]* %9 to i8**
%33 = load i32, i32* %10
%34 = icmp slt i32 2048, %33
br i1 %34, label %35, label %36
35: ; preds = %30
br label %38
36: ; preds = %30
%37 = load i32, i32* %10
br label %38
38: ; preds = %36, %35
%39 = phi i32 [ 2048, %35 ], [ %37, %36 ]
%40 = call i32 @read(i32 %31, i8** %32, i32 %39)
store i32 %40, i32* %8
%41 = icmp sgt i32 %40, 0
br label %42
42: ; preds = %38, %27
%43 = phi i1 [ false, %27 ], [ %41, %38 ]
br i1 %43, label %44, label %54
44: ; preds = %42
%45 = load i32, i32* %10
%46 = load i32, i32* %8
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %45, i32 %46)
%48 = load i32, i32* %11
%49 = getelementptr inbounds [2048 x i8], [2048 x i8]* %9, i64 0, i64 0
%50 = load i32, i32* %8
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
%52 = load i32, i32* %10
%53 = sub nsw i32 %52, 2048
store i32 %53, i32* %10
br label %27
54: ; preds = %42
%55 = load i32, i32* %11
%56 = load %struct.stat*, %struct.stat** %6
%57 = getelementptr inbounds %struct.stat, %struct.stat* %56, i32 0, i32 3
%58 = load i32, i32* %57
%59 = call i32 @fchmod(i32 %55, i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %54
%62 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %63
63: ; preds = %61, %54
%64 = load i32, i32* %11
%65 = load %struct.stat*, %struct.stat** %6
%66 = getelementptr inbounds %struct.stat, %struct.stat* %65, i32 0, i32 2
%67 = load i32, i32* %66
%68 = load %struct.stat*, %struct.stat** %6
%69 = getelementptr inbounds %struct.stat, %struct.stat* %68, i32 0, i32 1
%70 = load i32, i32* %69
%71 = call i32 @fchown(i32 %64, i32 %67, i32 %70)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %75
73: ; preds = %63
%74 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %75
75: ; preds = %73, %63
%76 = load i32, i32* %11
%77 = call i32 @close(i32 %76)
%78 = load i8*, i8** %5
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %78)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32, i32 }
@.str = [15 x i8] c"Backuptarget: \00"
@.str.1 = [9 x i8] c"%i - %i\0A\00"
@.str.2 = [7 x i8] c"fchmod\00"
@.str.3 = [6 x i8] c"chown\00"
@.str.4 = [19 x i8] c"recovered file %s\0A\00"
define void @RecoverFile(i32 %0, i8* %1, %struct.stat* nocapture %2) {
%4 = alloca [2048 x i8]
%5 = alloca [2048 x i8]
%6 = getelementptr inbounds [2048 x i8], [2048 x i8]* %4, i64 0, i64 0
%7 = bitcast [2048 x i8]* %4 to i8**
%8 = call i32 @pathFromFile(i8* %1, i8** %7)
%9 = call i32 @ensurePath(i8* %6)
%10 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @creat(i8* %1, i32 438)
%14 = icmp eq i32 %13, -1
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = bitcast [2048 x i8]* %5 to i8**
%17 = icmp sgt i32 %12, 0
br i1 %17, label %21, label %32
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 2)
unreachable
21: ; preds = %15, %27
%22 = phi i32 [ %30, %27 ], [ %12, %15 ]
%23 = icmp ult i32 %22, 2048
%24 = select i1 %23, i32 %22, i32 2048
%25 = call i32 @read(i32 %0, i8** %16, i32 %24)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %25)
%29 = call i32 @write(i32 %13, i8* %10, i32 %25)
%30 = add nsw i32 %22, -2048
%31 = icmp sgt i32 %22, 2048
br i1 %31, label %21, label %32
32: ; preds = %21, %27, %15
%33 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 3
%34 = load i32, i32* %33
%35 = call i32 @fchmod(i32 %13, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %32
%40 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 2
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%43 = load i32, i32* %42
%44 = call i32 @fchown(i32 %13, i32 %41, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %39
%49 = call i32 @close(i32 %13)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %1)
ret void
}