#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int open(char const *, int, ...);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ write(int, char *, scalar_t__);
int cp(const char *src, const char *dest) {
  int in_fd, out_fd;
  char buf[8192];
  ssize_t result;
  in_fd = open(src, O_RDONLY);
  if (in_fd < 0) {
    fprintf(stderr, "failed to open %s: %s\n", src, strerror(errno));
    return 0;
  }
  out_fd = open(dest, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
  if (out_fd < 0) {
    fprintf(stderr, "failed to open %s: %s\n", dest, strerror(errno));
    return 0;
  }
  while ((result = read(in_fd, buf, sizeof(buf)))) {
    assert(result > 0);
    assert(write(out_fd, buf, result) == result);
  }
  close(in_fd);
  close(out_fd);
  return 1;
}


x86 assembly (clang, -O0):
<cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f4 <cp+0x84>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2034(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x2030(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2034(%rbp),%edi
mov    -0x2030(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1d(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1305 <cp+0x195>
mov    -0x18(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
or     (%rax),%esi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
mov    0x2dbe(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1280 <cp+0x110>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2044(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x2040(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x2044(%rbp),%edi
mov    -0x2040(%rbp),%rdx
mov    %rax,%rcx
lea    0xd91(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1305 <cp+0x195>
jmp    1285 <cp+0x115>
mov    -0x1c(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
call   1050 <read@plt>
mov    %rax,-0x2028(%rbp)
cmp    $0x0,%rax
je     12ee <cp+0x17e>
cmpq   $0x0,-0x2028(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x20(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    -0x2028(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x2028(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
jmp    1285 <cp+0x115>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     125a <cp+0xea>
mov    %eax,%r12d
mov    0x2e4c(%rip),%rax
mov    0x2df5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dfa(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %r14d,%r14d
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     127f <cp+0x10f>
mov    %eax,%r13d
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0x2000,%edx
call   1050 <read@plt>
test   %rax,%rax
je     1242 <cp+0xd2>
mov    %rax,%rbp
mov    %rsp,%rbx
nopl   0x0(%rax)
xor    %edi,%edi
test   %rbp,%rbp
setg   %dil
call   1060 <assert@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
xor    %edi,%edi
cmp    %rbp,%rax
sete   %dil
call   1060 <assert@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1050 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1200 <cp+0x90>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r13d,%edi
call   1040 <close@plt>
mov    $0x1,%r14d
jmp    12aa <cp+0x13a>
mov    0x2d67(%rip),%rax
mov    (%rax),%ebp
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd88(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
jmp    12a2 <cp+0x132>
mov    0x2d42(%rip),%rax
mov    (%rax),%ebx
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd63(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    %r14d,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"failed to open %s: %s\0A\00"
@errno = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @cp(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [8192 x i8]
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 (i8*, i32, ...) @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %4
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %19)
store i32 0, i32* %3
br label %62
21: ; preds = %2
%22 = load i8*, i8** %5
%23 = load i32, i32* @O_CREAT
%24 = load i32, i32* @O_WRONLY
%25 = or i32 %23, %24
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %26, %27
%29 = call i32 (i8*, i32, ...) @open(i8* %22, i32 %25, i32 %28)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %21
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %5
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %34, i8* %36)
store i32 0, i32* %3
br label %62
38: ; preds = %21
br label %39
39: ; preds = %44, %38
%40 = load i32, i32* %6
%41 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%42 = call i64 @read(i32 %40, i8* %41, i32 8192)
store i64 %42, i64* %9
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %57
44: ; preds = %39
%45 = load i64, i64* %9
%46 = icmp sgt i64 %45, 0
%47 = zext i1 %46 to i32
%48 = call i32 @assert(i32 %47)
%49 = load i32, i32* %7
%50 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%51 = load i64, i64* %9
%52 = call i64 @write(i32 %49, i8* %50, i64 %51)
%53 = load i64, i64* %9
%54 = icmp eq i64 %52, %53
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
br label %39
57: ; preds = %39
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
%60 = load i32, i32* %7
%61 = call i32 @close(i32 %60)
store i32 1, i32* %3
br label %62
62: ; preds = %57, %32, %15
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"failed to open %s: %s\0A\00"
@errno = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @cp(i8* %0, i8* %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 (i8*, i32, ...) @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %11)
br label %44
13: ; preds = %2
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_WRONLY
%16 = or i32 %15, %14
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %18, %17
%20 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %16, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %13
%23 = call i64 @read(i32 %6, i8* %4, i32 8192)
%24 = icmp eq i64 %23, 0
br i1 %24, label %41, label %30
25: ; preds = %13
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %28)
br label %44
30: ; preds = %22, %30
%31 = phi i64 [ %39, %30 ], [ %23, %22 ]
%32 = icmp sgt i64 %31, 0
%33 = zext i1 %32 to i32
%34 = tail call i32 @assert(i32 %33)
%35 = call i64 @write(i32 %20, i8* %4, i64 %31)
%36 = icmp eq i64 %35, %31
%37 = zext i1 %36 to i32
%38 = tail call i32 @assert(i32 %37)
%39 = call i64 @read(i32 %6, i8* %4, i32 8192)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %30
41: ; preds = %30, %22
%42 = tail call i32 @close(i32 %6)
%43 = tail call i32 @close(i32 %20)
br label %44
44: ; preds = %41, %25, %8
%45 = phi i32 [ 0, %8 ], [ 0, %25 ], [ 1, %41 ]
ret i32 %45
}