#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int write(int, char *, int);
void do_copy(int input, int output) {
  char buf[1024];
  int n;
  while ((n = read(input, buf, sizeof(buf))) > 0) {
    char *p = buf;
    while (n > 0) {
      int m = write(output, buf, n);
      if (m <= 0) {
        perror("do_copy: write");
      }
      p += m;
      n -= m;
    }
  }
  if (n < 0) {
    perror("do_copy: read");
  }
}


x86 assembly (clang, -O0):
<do_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jle    11eb <do_copy+0xbb>
lea    -0x410(%rbp),%rax
mov    %rax,-0x420(%rbp)
cmpl   $0x0,-0x414(%rbp)
jle    11e6 <do_copy+0xb6>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jg     11b3 <do_copy+0x83>
lea    0xe52(%rip),%rdi
call   1050 <perror@plt>
mov    -0x424(%rbp),%ecx
mov    -0x420(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x420(%rbp)
mov    -0x424(%rbp),%ecx
mov    -0x414(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x414(%rbp)
jmp    1172 <do_copy+0x42>
jmp    1141 <do_copy+0x11>
cmpl   $0x0,-0x414(%rbp)
jge    1204 <do_copy+0xd4>
lea    0xe10(%rip),%rdi
call   1050 <perror@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_copy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %esi,%r13d
mov    %edi,%r14d
mov    %rsp,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    11a9 <do_copy+0x79>
mov    %rsp,%r15
lea    0xe9c(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13d,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    119f <do_copy+0x6f>
sub    %ebx,%ebp
jg     1170 <do_copy+0x40>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1170 <do_copy+0x40>
jmp    11a9 <do_copy+0x79>
mov    %r12,%rdi
call   1050 <perror@plt>
jmp    1183 <do_copy+0x53>
test   %ebp,%ebp
js     11bf <do_copy+0x8f>
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe49(%rip),%rdi
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <perror@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"do_copy: write\00"
@.str.1 = [14 x i8] c"do_copy: read\00"
define void @do_copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
br label %9
9: ; preds = %36, %2
%10 = load i32, i32* %3
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 1024)
store i32 %12, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %9
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
store i8* %15, i8** %7
br label %16
16: ; preds = %28, %14
%17 = load i32, i32* %6
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %36
19: ; preds = %16
%20 = load i32, i32* %4
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%22 = load i32, i32* %6
%23 = call i32 @write(i32 %20, i8* %21, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp sle i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %19
%29 = load i32, i32* %8
%30 = load i8*, i8** %7
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8* %32, i8** %7
%33 = load i32, i32* %8
%34 = load i32, i32* %6
%35 = sub nsw i32 %34, %33
store i32 %35, i32* %6
br label %16
36: ; preds = %16
br label %9
37: ; preds = %9
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"do_copy: write\00"
@.str.1 = [14 x i8] c"do_copy: read\00"
define void @do_copy(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 1024)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %10, label %21
7: ; preds = %16
%8 = call i32 @read(i32 %0, i8* %4, i32 1024)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %19, label %21
10: ; preds = %2, %19
%11 = phi i32 [ %20, %19 ], [ %5, %2 ]
%12 = call i32 @write(i32 %1, i8* %4, i32 %11)
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = sub nsw i32 %11, %12
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %7
19: ; preds = %16, %7
%20 = phi i32 [ %17, %16 ], [ %8, %7 ]
br label %10
21: ; preds = %7, %2
%22 = phi i32 [ %5, %2 ], [ %8, %7 ]
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
ret void
}