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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int creat(char const *, int);
int errno;
int open(char const *, int /*<<< orphan*/);
int read(int, char *, int);
int write(int, char *, int);
int sp_rtrace_copy_file(const char *source, const char *dest) {
  int rc = 0;
  int fd_in = open(source, O_RDONLY);
  if (fd_in == -1)
    return -errno;
  int fd_out = creat(dest, 0644);
  if (fd_out == -1) {
    rc = -errno;
    close(fd_in);
    return rc;
  }
  char buffer[0x8000];
  int n_in;
  while ((n_in = read(fd_in, buffer, sizeof(buffer))) > 0) {
    int n_out = write(fd_out, buffer, n_in);
    if (n_in != n_out) {
      rc = -errno;
      break;
    }
  }
  close(fd_in);
  close(fd_out);
  return rc;
}


x86 assembly (clang, -O0):
<sp_rtrace_copy_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x8040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    119c <sp_rtrace_copy_file+0x4c>
xor    %eax,%eax
mov    0x2e46(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    125b <sp_rtrace_copy_file+0x10b>
mov    -0x18(%rbp),%rdi
mov    $0x1a4,%esi
call   1070 <creat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11d8 <sp_rtrace_copy_file+0x88>
xor    %eax,%eax
mov    0x2e18(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125b <sp_rtrace_copy_file+0x10b>
jmp    11dd <sp_rtrace_copy_file+0x8d>
mov    -0x20(%rbp),%edi
lea    -0x8030(%rbp),%rsi
mov    $0x8000,%edx
call   1050 <read@plt>
mov    %eax,-0x8034(%rbp)
cmp    $0x0,%eax
jle    1245 <sp_rtrace_copy_file+0xf5>
mov    -0x24(%rbp),%edi
lea    -0x8030(%rbp),%rsi
mov    -0x8034(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x8038(%rbp)
mov    -0x8034(%rbp),%eax
cmp    -0x8038(%rbp),%eax
je     1240 <sp_rtrace_copy_file+0xf0>
xor    %eax,%eax
mov    0x2da2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
jmp    1245 <sp_rtrace_copy_file+0xf5>
jmp    11dd <sp_rtrace_copy_file+0x8d>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x8040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sp_rtrace_copy_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x8000,%rsp
mov    %rsi,%rbx
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11c6 <sp_rtrace_copy_file+0x76>
mov    %eax,%r15d
mov    %rbx,%rdi
mov    $0x1a4,%esi
call   1070 <creat@plt>
cmp    $0xffffffff,%eax
je     11d5 <sp_rtrace_copy_file+0x85>
mov    %eax,%r12d
mov    %rsp,%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x8000,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11e7 <sp_rtrace_copy_file+0x97>
mov    %eax,%ebp
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %eax,%ebp
je     1190 <sp_rtrace_copy_file+0x40>
mov    0x2e1a(%rip),%rax
xor    %r14d,%r14d
sub    (%rax),%r14d
jmp    11ea <sp_rtrace_copy_file+0x9a>
mov    0x2e0b(%rip),%rax
xor    %r14d,%r14d
sub    (%rax),%r14d
jmp    11fa <sp_rtrace_copy_file+0xaa>
mov    0x2dfc(%rip),%rax
xor    %r14d,%r14d
sub    (%rax),%r14d
mov    %r15d,%edi
jmp    11f5 <sp_rtrace_copy_file+0xa5>
xor    %r14d,%r14d
mov    %r15d,%edi
call   1040 <close@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x8000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @sp_rtrace_copy_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [32768 x i8]
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
store i32 %19, i32* %3
br label %55
20: ; preds = %2
%21 = load i8*, i8** %5
%22 = call i32 @creat(i8* %21, i32 420)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
store i32 %27, i32* %6
%28 = load i32, i32* %7
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %6
store i32 %30, i32* %3
br label %55
31: ; preds = %20
br label %32
32: ; preds = %48, %31
%33 = load i32, i32* %7
%34 = getelementptr inbounds [32768 x i8], [32768 x i8]* %9, i64 0, i64 0
%35 = call i32 @read(i32 %33, i8* %34, i32 32768)
store i32 %35, i32* %10
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %32
%38 = load i32, i32* %8
%39 = getelementptr inbounds [32768 x i8], [32768 x i8]* %9, i64 0, i64 0
%40 = load i32, i32* %10
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
store i32 %41, i32* %11
%42 = load i32, i32* %10
%43 = load i32, i32* %11
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %37
%46 = load i32, i32* @errno
%47 = sub nsw i32 0, %46
store i32 %47, i32* %6
br label %49
48: ; preds = %37
br label %32
49: ; preds = %45, %32
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %8
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %6
store i32 %54, i32* %3
br label %55
55: ; preds = %49, %25, %17
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i32 0
define i32 @sp_rtrace_copy_file(i8* nocapture %0, i8* %1) {
%3 = alloca [32768 x i8]
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %32
10: ; preds = %2
%11 = tail call i32 @creat(i8* %1, i32 420)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
%16 = tail call i32 @close(i32 %5)
br label %32
17: ; preds = %10
%18 = getelementptr inbounds [32768 x i8], [32768 x i8]* %3, i64 0, i64 0
br label %19
19: ; preds = %22, %17
%20 = call i32 @read(i32 %5, i8* %18, i32 32768)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = call i32 @write(i32 %11, i8* %18, i32 %20)
%24 = icmp eq i32 %20, %23
br i1 %24, label %19, label %25
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
br label %28
28: ; preds = %19, %25
%29 = phi i32 [ %27, %25 ], [ 0, %19 ]
%30 = tail call i32 @close(i32 %5)
%31 = tail call i32 @close(i32 %11)
br label %32
32: ; preds = %13, %28, %7
%33 = phi i32 [ %9, %7 ], [ %15, %13 ], [ %29, %28 ]
ret i32 %33
}