#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*/ uint8_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ write(int, int /*<<< orphan*/ *, scalar_t__);
bool copyfile(int fd, int sockfd) {
  uint8_t buf[4096];
  ssize_t readc = 0, wrotec = 0, pos = 0;
  while ((readc = read(fd, buf, 4096)) > 0) {
    pos = 0;
    while (readc - pos > 0 &&
           ((wrotec = write(sockfd, &(buf[pos]), readc - pos)) > 0))
      pos += wrotec;
    if (wrotec < 0) {
      fprintf(stderr, "Error: %s\n", strerror(errno));
      return true;
    }
  }
  if (readc < 0) {
    fprintf(stderr, "Error: %s\n", strerror(errno));
    return true;
  };
  return false;
}


x86 assembly (clang, -O0):
<copyfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4040,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movq   $0x0,-0x4018(%rbp)
movq   $0x0,-0x4020(%rbp)
movq   $0x0,-0x4028(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x4010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,-0x4018(%rbp)
cmp    $0x0,%rax
jle    127f <copyfile+0x13f>
movq   $0x0,-0x4028(%rbp)
mov    -0x4018(%rbp),%rcx
sub    -0x4028(%rbp),%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x4029(%rbp)
jle    1201 <copyfile+0xc1>
mov    -0xc(%rbp),%edi
mov    -0x4028(%rbp),%rax
lea    -0x4010(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x4018(%rbp),%rdx
sub    -0x4028(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x4020(%rbp)
cmp    $0x0,%rax
setg   %al
mov    %al,-0x4029(%rbp)
mov    -0x4029(%rbp),%al
test   $0x1,%al
jne    1214 <copyfile+0xd4>
jmp    122e <copyfile+0xee>
mov    -0x4020(%rbp),%rax
add    -0x4028(%rbp),%rax
mov    %rax,-0x4028(%rbp)
jmp    11a2 <copyfile+0x62>
cmpq   $0x0,-0x4020(%rbp)
jge    127a <copyfile+0x13a>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4030(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x4030(%rbp),%edi
mov    %rax,%rdx
lea    0xd97(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <copyfile+0x192>
jmp    1172 <copyfile+0x32>
cmpq   $0x0,-0x4018(%rbp)
jge    12cb <copyfile+0x18b>
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4034(%rbp)
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x4034(%rbp),%edi
mov    %rax,%rdx
lea    0xd46(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <copyfile+0x192>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x4000,%rsp
mov    %esi,%r12d
mov    %edi,%r15d
mov    %rsp,%r14
jmp    1165 <copyfile+0x25>
nopw   0x0(%rax,%rax,1)
test   %rax,%rax
js     11b5 <copyfile+0x75>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
jle    11ae <copyfile+0x6e>
mov    %rbp,%rdx
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    (%rsp,%rbx,4),%rsi
mov    %r12d,%edi
call   1030 <write@plt>
test   %rax,%rax
jle    1160 <copyfile+0x20>
add    %rax,%rbx
mov    %rbp,%rdx
sub    %rbx,%rdx
jg     1190 <copyfile+0x50>
jmp    1160 <copyfile+0x20>
xor    %eax,%eax
test   %rbp,%rbp
jns    11e2 <copyfile+0xa2>
mov    0x2e24(%rip),%rax
mov    (%rax),%ebx
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe2d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x4000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
define i32 @copyfile(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4096 x i32]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i64 0, i64* %7
store i64 0, i64* %8
store i64 0, i64* %9
br label %10
10: ; preds = %44, %2
%11 = load i32, i32* %4
%12 = getelementptr inbounds [4096 x i32], [4096 x i32]* %6, i64 0, i64 0
%13 = call i64 @read(i32 %11, i32* %12, i32 4096)
store i64 %13, i64* %7
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %45
15: ; preds = %10
store i64 0, i64* %9
br label %16
16: ; preds = %32, %15
%17 = load i64, i64* %7
%18 = load i64, i64* %9
%19 = sub nsw i64 %17, %18
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %16
%22 = load i32, i32* %5
%23 = load i64, i64* %9
%24 = getelementptr inbounds [4096 x i32], [4096 x i32]* %6, i64 0, i64 %23
%25 = load i64, i64* %7
%26 = load i64, i64* %9
%27 = sub nsw i64 %25, %26
%28 = call i64 @write(i32 %22, i32* %24, i64 %27)
store i64 %28, i64* %8
%29 = icmp sgt i64 %28, 0
br label %30
30: ; preds = %21, %16
%31 = phi i1 [ false, %16 ], [ %29, %21 ]
br i1 %31, label %32, label %36
32: ; preds = %30
%33 = load i64, i64* %8
%34 = load i64, i64* %9
%35 = add nsw i64 %34, %33
store i64 %35, i64* %9
br label %16
36: ; preds = %30
%37 = load i64, i64* %8
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %42)
store i32 1, i32* %3
br label %54
44: ; preds = %36
br label %10
45: ; preds = %10
%46 = load i64, i64* %7
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %45
%49 = load i32, i32* @stderr
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %51)
store i32 1, i32* %3
br label %54
53: ; preds = %45
store i32 0, i32* %3
br label %54
54: ; preds = %53, %48, %39
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [11 x i8] c"Error: %s\0A\00"
@errno = global i32 0
define i32 @copyfile(i32 %0, i32 %1) {
%3 = alloca [4096 x i32]
%4 = bitcast [4096 x i32]* %3 to i8*
%5 = getelementptr inbounds [4096 x i32], [4096 x i32]* %3, i64 0, i64 0
br label %6
6: ; preds = %19, %2
%7 = call i64 @read(i32 %0, i32* %5, i32 4096)
%8 = icmp sgt i64 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %6, %15
%10 = phi i64 [ %17, %15 ], [ %7, %6 ]
%11 = phi i64 [ %16, %15 ], [ 0, %6 ]
%12 = getelementptr inbounds [4096 x i32], [4096 x i32]* %3, i64 0, i64 %11
%13 = call i64 @write(i32 %1, i32* %12, i64 %10)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = add nsw i64 %13, %11
%17 = sub nsw i64 %7, %16
%18 = icmp sgt i64 %17, 0
br i1 %18, label %9, label %19
19: ; preds = %9, %15
%20 = icmp slt i64 %13, 0
br i1 %20, label %23, label %6
21: ; preds = %6
%22 = icmp slt i64 %7, 0
br i1 %22, label %23, label %28
23: ; preds = %19, %21
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %23, %21
%29 = phi i32 [ 0, %21 ], [ 1, %23 ]
ret i32 %29
}