#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 read(int, char *, int);
int write(int, char *, int);
int gdesccopy(int source, int dest) {
  char buffer[1024];
  int result = 0;
  while ((result = read(source, buffer, 1024)) > 0) {
    if (write(dest, buffer, result) == -1)
      return -1;
  }
  if (result == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<gdesccopy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x414(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    118d <gdesccopy+0x6d>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1188 <gdesccopy+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <gdesccopy+0x8d>
jmp    113b <gdesccopy+0x1b>
cmpl   $0xffffffff,-0x414(%rbp)
jne    11a6 <gdesccopy+0x86>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <gdesccopy+0x8d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdesccopy>:
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>
mov    %eax,%edx
test   %eax,%eax
jle    116c <gdesccopy+0x4c>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1140 <gdesccopy+0x20>
mov    $0xffffffff,%eax
jmp    1176 <gdesccopy+0x56>
xor    %eax,%eax
cmp    $0xffffffff,%edx
sete   %al
neg    %eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @gdesccopy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 0, i32* %7
br label %8
8: ; preds = %20, %2
%9 = load i32, i32* %4
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 1024)
store i32 %11, i32* %7
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %8
%14 = load i32, i32* %5
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* %7
%17 = call i32 @write(i32 %14, i8* %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %13
store i32 -1, i32* %3
br label %26
20: ; preds = %13
br label %8
21: ; preds = %8
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %21
store i32 -1, i32* %3
br label %26
25: ; preds = %21
store i32 0, i32* %3
br label %26
26: ; preds = %25, %24, %19
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
define i32 @gdesccopy(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 = %8, %2
%6 = call i32 @read(i32 %0, i8* %4, i32 1024)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = call i32 @write(i32 %1, i8* %4, i32 %6)
%10 = icmp eq i32 %9, -1
br i1 %10, label %14, label %5
11: ; preds = %5
%12 = icmp eq i32 %6, -1
%13 = sext i1 %12 to i32
br label %14
14: ; preds = %8, %11
%15 = phi i32 [ %13, %11 ], [ -1, %8 ]
ret i32 %15
}