#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 BUFSIZ;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ serial_write(int, int /*<<< orphan*/ *, scalar_t__);
int /*<<< orphan*/ stderr;
void copy(int in, int out) {
  uint8_t buf[BUFSIZ];
  ssize_t n;
  while (1) {
    n = read(in, buf, BUFSIZ);
    if (n < 0) {
      perror("read");
      exit(1);
    }
    if (n == 0) {
      fprintf(stderr, "EOF\n");
      exit(1);
    }
    serial_write(out, buf, n);
    fprintf(stderr, "Wrote %zd\n", n);
  }
}


x86 assembly (clang, -O0):
<copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
jmp    1190 <copy+0x40>
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
call   1030 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11ca <copy+0x7a>
lea    0xe45(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11f6 <copy+0xa6>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rdx
call   1070 <serial_write@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    118b <copy+0x3b>

x86 assembly (clang, -O2):
<copy>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%r13
lea    0xf(,%rdx,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %r13,%rsi
call   1030 <read@plt>
test   %rax,%rax
js     11e5 <copy+0x95>
mov    %rax,%rbx
lea    0xe71(%rip),%r12
nopl   0x0(%rax)
test   %rbx,%rbx
je     11fb <copy+0xab>
mov    %r14d,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1070 <serial_write@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %r13,%rsi
call   1030 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11a0 <copy+0x50>
lea    0xe14(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xdfa(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.1 = [5 x i8] c"EOF\0A\00"
@.str.2 = [11 x i8] c"Wrote %zd\0A\00"
define void @copy(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%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** %5
%11 = alloca i32, i64 %9
store i64 %9, i64* %6
br label %12
12: ; preds = %29, %2
br label %13
13: ; preds = %12
%14 = load i32, i32* %3
%15 = load i32, i32* @BUFSIZ
%16 = call i64 @read(i32 %14, i32* %11, i32 %15)
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %13
%23 = load i64, i64* %7
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = load i32, i32* %4
%31 = load i64, i64* %7
%32 = call i32 @serial_write(i32 %30, i32* %11, i64 %31)
%33 = load i32, i32* @stderr
%34 = load i64, i64* %7
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %34)
br label %12
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.1 = [5 x i8] c"EOF\0A\00"
@.str.2 = [11 x i8] c"Wrote %zd\0A\00"
define void @copy(i32 %0, i32 %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i32, i64 %4
%6 = call i64 @read(i32 %0, i32* %5, i32 %3)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %18, %2
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %2, %18
%12 = phi i64 [ %23, %18 ], [ %6, %2 ]
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = call i32 @serial_write(i32 %1, i32* %5, i64 %12)
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i64 %12)
%22 = load i32, i32* @BUFSIZ
%23 = call i64 @read(i32 %0, i32* %5, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %8, label %11
}