#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 copyFileFd(int, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ logMessage(int, char *, char *, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int copyFile(char *source, char *dest) {
  int infd = -1;
  int rc;
  infd = open(source, O_RDONLY);
  if (infd < 0) {
    logMessage(40, "failed to open %s: %s", source, strerror(errno));
    return 1;
  }
  rc = copyFileFd(infd, dest);
  close(infd);
  return rc;
}


x86 assembly (clang, -O0):
<copyFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11bf <copyFile+0x6f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
mov    $0x28,%edi
lea    0xe52(%rip),%rsi
call   1030 <logMessage@plt>
movl   $0x1,-0x4(%rbp)
jmp    11dc <copyFile+0x8c>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <copyFileFd@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyFile>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     1185 <copyFile+0x35>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
call   1040 <copyFileFd@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
jmp    11ae <copyFile+0x5e>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe66(%rip),%rsi
mov    $0x28,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1030 <logMessage@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [22 x i8] c"failed to open %s: %s\00"
@errno = global i32 0
define i32 @copyFile(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 -1, i32* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = load i32, i32* @errno
%16 = call i32 @strerror(i32 %15)
%17 = call i32 @logMessage(i32 40, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %16)
store i32 1, i32* %3
br label %25
18: ; preds = %2
%19 = load i32, i32* %6
%20 = load i8*, i8** %5
%21 = call i32 @copyFileFd(i32 %19, i8* %20)
store i32 %21, i32* %7
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %7
store i32 %24, i32* %3
br label %25
25: ; preds = %18, %13
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [22 x i8] c"failed to open %s: %s\00"
@errno = global i32 0
define i32 @copyFile(i8* %0, i8* %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i32 @strerror(i32 %7)
%9 = tail call i32 @logMessage(i32 40, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %8)
br label %13
10: ; preds = %2
%11 = tail call i32 @copyFileFd(i32 %4, i8* %1)
%12 = tail call i32 @close(i32 %4)
br label %13
13: ; preds = %10, %6
%14 = phi i32 [ 1, %6 ], [ %11, %10 ]
ret i32 %14
}