#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 ssize_t;

/* Variables and functions */
int EXIT_SUCCESS;
int read(int, char *, int);
int write(int, char *, int);
int mycp_copy(int src, int dest) {
  ssize_t nread, nwrite;
  int buf_len = 1024;
  char buf[buf_len];
  while ((nread = read(src, buf, buf_len))) {
    if (nread == -1)
      return -1;
    nwrite = write(dest, buf, nread);
    if (nwrite == -1)
      return -1;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<mycp_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x400,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
je     11c7 <mycp_copy+0xa7>
cmpl   $0xffffffff,-0x10(%rbp)
jne    1193 <mycp_copy+0x73>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11da <mycp_copy+0xba>
mov    -0x38(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c2 <mycp_copy+0xa2>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    11da <mycp_copy+0xba>
jmp    115b <mycp_copy+0x3b>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mycp_copy>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     117a <mycp_copy+0x5a>
test   %eax,%eax
je     1171 <mycp_copy+0x51>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1140 <mycp_copy+0x20>
mov    $0xffffffff,%eax
jmp    117a <mycp_copy+0x5a>
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@EXIT_SUCCESS = global i32 0
define i32 @mycp_copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 1024, i32* %8
%12 = load i32, i32* %8
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %9
%15 = alloca i8, i64 %13
store i64 %13, i64* %10
br label %16
16: ; preds = %32, %2
%17 = load i32, i32* %4
%18 = load i32, i32* %8
%19 = call i32 @read(i32 %17, i8* %15, i32 %18)
store i32 %19, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %16
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 -1, i32* %3
store i32 1, i32* %11
br label %35
25: ; preds = %21
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = call i32 @write(i32 %26, i8* %15, i32 %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %25
store i32 -1, i32* %3
store i32 1, i32* %11
br label %35
32: ; preds = %25
br label %16
33: ; preds = %16
%34 = load i32, i32* @EXIT_SUCCESS
store i32 %34, i32* %3
store i32 1, i32* %11
br label %35
35: ; preds = %33, %31, %24
%36 = load i8*, i8** %9
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@EXIT_SUCCESS = global i32 0
define i32 @mycp_copy(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
br label %5
5: ; preds = %7, %2
%6 = call i32 @read(i32 %0, i8* %4, i32 1024)
switch i32 %6, label %7 [
i32 0, label %10
i32 -1, label %12
]
7: ; preds = %5
%8 = call i32 @write(i32 %1, i8* %4, i32 %6)
%9 = icmp eq i32 %8, -1
br i1 %9, label %12, label %5
10: ; preds = %5
%11 = load i32, i32* @EXIT_SUCCESS
br label %12
12: ; preds = %7, %5, %10
%13 = phi i32 [ %11, %10 ], [ -1, %7 ], [ %6, %5 ]
ret i32 %13
}