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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
void *malloc(int);
int /*<<< orphan*/ perror(char *);
int sread(int, void *, unsigned int);
int swrite(int, void *, unsigned int);
int transfer_file_copy(int out_fd, int in_fd, off_t size_remain) {
  void *buf = malloc(4096);
  if (!buf) {
    perror("malloc error");
    exit(1);
  }
  while (size_remain > 0) {
    unsigned int send_this_time;
    if (size_remain > 4096) {
      send_this_time = 4096;
    } else {
      send_this_time = (unsigned int)size_remain;
    }
    if (sread(in_fd, buf, send_this_time) == -1)
      return -1;
    if (swrite(out_fd, buf, send_this_time) == -1)
      return -1;
    size_remain -= send_this_time;
  }
  return 0;
}


x86 assembly (clang, -O0):
<transfer_file_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    $0x1000,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1190 <transfer_file_copy+0x40>
lea    0xe7f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    1195 <transfer_file_copy+0x45>
cmpl   $0x0,-0x10(%rbp)
jle    1216 <transfer_file_copy+0xc6>
cmpl   $0x1000,-0x10(%rbp)
jle    11b8 <transfer_file_copy+0x68>
movl   $0x1000,-0x1c(%rbp)
jmp    11be <transfer_file_copy+0x6e>
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <sread@plt>
cmp    $0xffffffff,%eax
jne    11e2 <transfer_file_copy+0x92>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <transfer_file_copy+0xcd>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <swrite@plt>
cmp    $0xffffffff,%eax
jne    1206 <transfer_file_copy+0xb6>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <transfer_file_copy+0xcd>
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
jmp    1195 <transfer_file_copy+0x45>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_file_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r15d
mov    %edi,%r14d
mov    $0x1000,%edi
call   1040 <malloc@plt>
test   %rax,%rax
je     11d8 <transfer_file_copy+0x88>
mov    %rax,%r12
mov    $0xffffffff,%r13d
jmp    1187 <transfer_file_copy+0x37>
nopl   (%rax)
sub    %ecx,%ebx
cmp    $0xffffffff,%eax
je     11c6 <transfer_file_copy+0x76>
test   %ebx,%ebx
jle    11c3 <transfer_file_copy+0x73>
cmp    $0x1000,%ebx
mov    $0x1000,%ebp
cmovb  %ebx,%ebp
mov    %r15d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <sread@plt>
cmp    $0xffffffff,%eax
je     11c6 <transfer_file_copy+0x76>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1060 <swrite@plt>
xor    %ecx,%ecx
cmp    $0xffffffff,%eax
je     1180 <transfer_file_copy+0x30>
mov    %ebp,%ecx
jmp    1180 <transfer_file_copy+0x30>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe21(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"malloc error\00"
define i32 @transfer_file_copy(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = call i8* @malloc(i32 4096)
store i8* %10, i8** %8
%11 = load i8*, i8** %8
%12 = icmp ne i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %3
br label %17
17: ; preds = %40, %16
%18 = load i32, i32* %7
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %44
20: ; preds = %17
%21 = load i32, i32* %7
%22 = icmp sgt i32 %21, 4096
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 4096, i32* %9
br label %26
24: ; preds = %20
%25 = load i32, i32* %7
store i32 %25, i32* %9
br label %26
26: ; preds = %24, %23
%27 = load i32, i32* %6
%28 = load i8*, i8** %8
%29 = load i32, i32* %9
%30 = call i32 @sread(i32 %27, i8* %28, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %26
store i32 -1, i32* %4
br label %45
33: ; preds = %26
%34 = load i32, i32* %5
%35 = load i8*, i8** %8
%36 = load i32, i32* %9
%37 = call i32 @swrite(i32 %34, i8* %35, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 -1, i32* %4
br label %45
40: ; preds = %33
%41 = load i32, i32* %9
%42 = load i32, i32* %7
%43 = sub i32 %42, %41
store i32 %43, i32* %7
br label %17
44: ; preds = %17
store i32 0, i32* %4
br label %45
45: ; preds = %44, %39, %32
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@.str = [13 x i8] c"malloc error\00"
define i32 @transfer_file_copy(i32 %0, i32 %1, i32 %2) {
%4 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3, %17
%10 = phi i32 [ %21, %17 ], [ %2, %3 ]
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = icmp ult i32 %10, 4096
%14 = select i1 %13, i32 %10, i32 4096
%15 = tail call i32 @sread(i32 %1, i8* %4, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = tail call i32 @swrite(i32 %0, i8* %4, i32 %14)
%19 = icmp eq i32 %18, -1
%20 = select i1 %19, i32 0, i32 %14
%21 = sub nsw i32 %10, %20
br i1 %19, label %22, label %9
22: ; preds = %12, %9, %17
%23 = phi i32 [ -1, %17 ], [ 0, %9 ], [ -1, %12 ]
ret i32 %23
}