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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
scalar_t__ close(int);
int /*<<< orphan*/ isp_dbgfail(char *, ...);
int open(char *, int /*<<< orphan*/);
int util_read(int, char *, int);
int util_write(int, char *, int);
int _copyfile(char *path, int nfd) {
  char buf[8192];
  int fd;
  int res = 0;
  int n;
  if ((fd = open(path, O_RDONLY)) < 0) {
    isp_dbgfail("_copyfile: open O_RDONLY %s: %m", path);
    res = 21;
    goto done;
  }
  do {
    if ((n = util_read(fd, buf, sizeof(buf))) < 0) {
      isp_dbgfail("_copyfile: read %s: %m", path);
      res = 21;
      goto done;
    }
    if (n > 0 && (n = util_write(nfd, buf, n)) <= 0) {
      isp_dbgfail("_copyfile: write: %m");
      res = 21;
      goto done;
    }
  } while (n > 0);
done:
  if (fd >= 0 && close(fd) < 0) {
    isp_dbgfail("_copyfile: close %s: %m", path);
    res = 21;
  }
  return res;
}


x86 assembly (clang, -O0):
<_copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x2018(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x2014(%rbp)
cmp    $0x0,%eax
jge    11ae <_copyfile+0x5e>
mov    -0x8(%rbp),%rsi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1050 <isp_dbgfail@plt>
movl   $0x15,-0x2018(%rbp)
jmp    125f <_copyfile+0x10f>
jmp    11b3 <_copyfile+0x63>
mov    -0x2014(%rbp),%edi
lea    -0x2010(%rbp),%rsi
mov    $0x2000,%edx
call   1060 <util_read@plt>
mov    %eax,-0x201c(%rbp)
cmp    $0x0,%eax
jge    11fa <_copyfile+0xaa>
mov    -0x8(%rbp),%rsi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1050 <isp_dbgfail@plt>
movl   $0x15,-0x2018(%rbp)
jmp    125f <_copyfile+0x10f>
cmpl   $0x0,-0x201c(%rbp)
jle    1248 <_copyfile+0xf8>
mov    -0xc(%rbp),%edi
lea    -0x2010(%rbp),%rsi
mov    -0x201c(%rbp),%edx
call   1040 <util_write@plt>
mov    %eax,-0x201c(%rbp)
cmp    $0x0,%eax
jg     1248 <_copyfile+0xf8>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1050 <isp_dbgfail@plt>
movl   $0x15,-0x2018(%rbp)
jmp    125f <_copyfile+0x10f>
jmp    124d <_copyfile+0xfd>
cmpl   $0x0,-0x201c(%rbp)
jg     11b3 <_copyfile+0x63>
jmp    125f <_copyfile+0x10f>
cmpl   $0x0,-0x2014(%rbp)
jl     129d <_copyfile+0x14d>
mov    -0x2014(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    129d <_copyfile+0x14d>
mov    -0x8(%rbp),%rsi
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1050 <isp_dbgfail@plt>
movl   $0x15,-0x2018(%rbp)
mov    -0x2018(%rbp),%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_copyfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11b6 <_copyfile+0x66>
mov    %eax,%r15d
mov    %rsp,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x2000,%edx
call   1060 <util_read@plt>
test   %eax,%eax
js     11bf <_copyfile+0x6f>
je     11dc <_copyfile+0x8c>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <util_write@plt>
test   %eax,%eax
jg     1180 <_copyfile+0x30>
lea    0xe8a(%rip),%rdi
xor    %eax,%eax
call   1050 <isp_dbgfail@plt>
jmp    11d0 <_copyfile+0x80>
lea    0xe43(%rip),%rdi
jmp    11f7 <_copyfile+0xa7>
lea    0xe5a(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <isp_dbgfail@plt>
mov    $0x15,%ebx
test   %r15d,%r15d
jns    11e3 <_copyfile+0x93>
jmp    1206 <_copyfile+0xb6>
xor    %ebx,%ebx
test   %r15d,%r15d
js     1206 <_copyfile+0xb6>
mov    %r15d,%edi
call   1030 <close@plt>
test   %rax,%rax
jns    1206 <_copyfile+0xb6>
lea    0xe55(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <isp_dbgfail@plt>
mov    $0x15,%ebx
mov    %ebx,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [32 x i8] c"_copyfile: open O_RDONLY %s: %m\00"
@.str.1 = [23 x i8] c"_copyfile: read %s: %m\00"
@.str.2 = [21 x i8] c"_copyfile: write: %m\00"
@.str.3 = [24 x i8] c"_copyfile: close %s: %m\00"
define i32 @_copyfile(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [8192 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %7
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i8*, i8** %3
%15 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 21, i32* %7
br label %41
16: ; preds = %2
br label %17
17: ; preds = %37, %16
%18 = load i32, i32* %6
%19 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%20 = call i32 @util_read(i32 %18, i8* %19, i32 8192)
store i32 %20, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i8*, i8** %3
%24 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 21, i32* %7
br label %41
25: ; preds = %17
%26 = load i32, i32* %8
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = load i32, i32* %4
%30 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%31 = load i32, i32* %8
%32 = call i32 @util_write(i32 %29, i8* %30, i32 %31)
store i32 %32, i32* %8
%33 = icmp sle i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 21, i32* %7
br label %41
36: ; preds = %28, %25
br label %37
37: ; preds = %36
%38 = load i32, i32* %8
%39 = icmp sgt i32 %38, 0
br i1 %39, label %17, label %40
40: ; preds = %37
br label %41
41: ; preds = %40, %34, %22, %13
%42 = load i32, i32* %6
%43 = icmp sge i32 %42, 0
br i1 %43, label %44, label %51
44: ; preds = %41
%45 = load i32, i32* %6
%46 = call i64 @close(i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i8*, i8** %3
%50 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %49)
store i32 21, i32* %7
br label %51
51: ; preds = %48, %44, %41
%52 = load i32, i32* %7
ret i32 %52
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [32 x i8] c"_copyfile: open O_RDONLY %s: %m\00"
@.str.1 = [23 x i8] c"_copyfile: read %s: %m\00"
@.str.2 = [21 x i8] c"_copyfile: write: %m\00"
@.str.3 = [24 x i8] c"_copyfile: close %s: %m\00"
define i32 @_copyfile(i8* %0, i32 %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %30
10: ; preds = %2, %17
%11 = call i32 @util_read(i32 %6, i8* %4, i32 8192)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
15: ; preds = %10
%16 = icmp eq i32 %11, 0
br i1 %16, label %22, label %17
17: ; preds = %15
%18 = call i32 @util_write(i32 %1, i8* %4, i32 %11)
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %10
20: ; preds = %17
%21 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %15, %20, %13
%23 = phi i32 [ 21, %13 ], [ 21, %20 ], [ 0, %15 ]
%24 = icmp sgt i32 %6, -1
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = call i64 @close(i32 %6)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 (i8*, ...) @isp_dbgfail(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %8, %28, %25, %22
%31 = phi i32 [ 21, %28 ], [ %23, %25 ], [ %23, %22 ], [ 21, %8 ]
ret i32 %31
}