#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(void *);
void *malloc(int);
int read(int, void *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, void *, int);
int copy_fd(int in, int out) {
  int bs = 4096;
  void *buf = malloc(bs);
  if (!buf)
    exit(EXIT_FAILURE);
  while (1) {
    int now, wr;
    now = read(in, buf, bs);
    if (now < 0) {
      fprintf(stderr, "read failed with %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
    if (!now)
      break;
    wr = write(out, buf, now);
    if (wr < 0) {
      fprintf(stderr, "write failed with %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
    if (wr != now) {
      fprintf(stderr, "short write\n");
      exit(EXIT_FAILURE);
    }
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<copy_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x1000,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11aa <copy_fd+0x3a>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11af <copy_fd+0x3f>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1207 <copy_fd+0x97>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1216 <copy_fd+0xa6>
jmp    12a4 <copy_fd+0x134>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    126e <copy_fd+0xfe>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     129f <copy_fd+0x12f>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    11af <copy_fd+0x3f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_fd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
mov    $0x1000,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1245 <copy_fd+0xd5>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %eax,%eax
js     11fb <copy_fd+0x8b>
je     11e6 <copy_fd+0x76>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
js     121b <copy_fd+0xab>
cmp    %ebp,%eax
je     11a0 <copy_fd+0x30>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1245 <copy_fd+0xd5>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dde(%rip),%rax
mov    (%rax),%ebx
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde7(%rip),%rsi
jmp    1239 <copy_fd+0xc9>
mov    0x2dbe(%rip),%rax
mov    (%rax),%ebx
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xddc(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"read failed with %s\0A\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"write failed with %s\0A\00"
@.str.2 = [13 x i8] c"short write\0A\00"
define i32 @copy_fd(i32 %0, i32 %1) {
%3 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %2, %30
%9 = tail call i32 @read(i32 %0, i8* %3, i32 4096)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %8
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %8
%19 = icmp eq i32 %9, 0
br i1 %19, label %37, label %20
20: ; preds = %18
%21 = tail call i32 @write(i32 %1, i8* %3, i32 %9)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %20
%31 = icmp eq i32 %21, %9
br i1 %31, label %8, label %32
32: ; preds = %30
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %18
%38 = tail call i32 @free(i8* %3)
ret i32 0
}