#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 scalar_t__ ssize_t;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, char *, scalar_t__);
void do_cp(int src, int dest) {
  ssize_t count;
  char buf[BUFSIZ];
  while ((count = read(src, buf, BUFSIZ)) > 0) {
    count = write(dest, buf, count);
    if (count < 0) {
      fprintf(stderr, "cp: write error\n");
      exit(EXIT_FAILURE);
    }
  }
  if (count < 0) {
    fprintf(stderr, "cp: read error\n");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<do_cp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
call   1040 <read@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jle    11e4 <do_cp+0xa4>
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11df <do_cp+0x9f>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    117a <do_cp+0x3a>
cmpq   $0x0,-0x10(%rbp)
jge    1212 <do_cp+0xd2>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_cp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e80(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
call   1040 <read@plt>
test   %rax,%rax
jle    11a9 <do_cp+0x69>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     11bb <do_cp+0x7b>
mov    (%r12),%edx
mov    %r15d,%edi
mov    %rbx,%rsi
jmp    1180 <do_cp+0x40>
test   %rax,%rax
js     11de <do_cp+0x9e>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
jmp    11cb <do_cp+0x8b>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"cp: write error\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"cp: read error\0A\00"
define void @do_cp(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* @BUFSIZ
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %6
%11 = alloca i8, i64 %9
store i64 %9, i64* %7
br label %12
12: ; preds = %28, %2
%13 = load i32, i32* %3
%14 = load i32, i32* @BUFSIZ
%15 = call i64 @read(i32 %13, i8* %11, i32 %14)
store i64 %15, i64* %5
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %12
%18 = load i32, i32* %4
%19 = load i64, i64* %5
%20 = call i64 @write(i32 %18, i8* %11, i64 %19)
store i64 %20, i64* %5
%21 = load i64, i64* %5
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %17
br label %12
29: ; preds = %12
%30 = load i64, i64* %5
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %29
%38 = load i8*, i8** %6
ret void
}

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