#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 BUFSIZ;
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fatal(int, char *, int, scalar_t__);
int read(int, unsigned char *, int);
int write_fill(int, unsigned char *, int);
int read_write(int infd, int outfd) {
  unsigned char buf[BUFSIZ];
  int rval;
  if ((rval = read(infd, buf, BUFSIZ)) == 0) {
    return 0;
  }
  if (rval < 0 && errno == EINTR)
    return 1;
  if (rval < 0)
    fatal(3, "Failed to read on fd %d: %d", infd, errno);
  if (write_fill(outfd, buf, rval) != rval)
    fatal(4, "Failed to write on fd %d: %d", outfd, errno);
  return 1;
}


x86 assembly (clang, -O0):
<read_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x30(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jne    119a <read_write+0x6a>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    123d <read_write+0x10d>
cmpl   $0x0,-0x24(%rbp)
jge    11d1 <read_write+0xa1>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
jne    11d1 <read_write+0xa1>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    123d <read_write+0x10d>
cmpl   $0x0,-0x24(%rbp)
jge    11f9 <read_write+0xc9>
mov    -0x8(%rbp),%edx
mov    0x2df3(%rip),%rax
mov    (%rax),%rcx
mov    $0x3,%edi
lea    0xe0c(%rip),%rsi
call   1050 <fatal@plt>
mov    -0x30(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x24(%rbp),%edx
call   1040 <write_fill@plt>
cmp    -0x24(%rbp),%eax
je     122f <read_write+0xff>
mov    -0xc(%rbp),%edx
mov    0x2dbd(%rip),%rax
mov    (%rax),%rcx
mov    $0x4,%edi
lea    0xdf2(%rip),%rsi
call   1050 <fatal@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_write>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e95(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1030 <read@plt>
test   %eax,%eax
je     11db <read_write+0xab>
mov    %eax,%r12d
jns    11a2 <read_write+0x72>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rcx
mov    0x2e4d(%rip),%rax
mov    $0x1,%r13d
cmp    (%rax),%rcx
je     11de <read_write+0xae>
lea    0xe6b(%rip),%rsi
mov    $0x3,%edi
mov    %r15d,%edx
call   1050 <fatal@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <write_fill@plt>
mov    $0x1,%r13d
cmp    %r12d,%eax
je     11de <read_write+0xae>
mov    0x2e16(%rip),%rax
mov    (%rax),%rcx
lea    0xe50(%rip),%rsi
mov    $0x4,%edi
mov    %r14d,%edx
call   1050 <fatal@plt>
jmp    11de <read_write+0xae>
xor    %r13d,%r13d
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [28 x i8] c"Failed to read on fd %d: %d\00"
@.str.1 = [29 x i8] c"Failed to write on fd %d: %d\00"
define i32 @read_write(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i32, i32* %4
%15 = load i32, i32* @BUFSIZ
%16 = call i32 @read(i32 %14, i8* %13, i32 %15)
store i32 %16, i32* %8
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 0, i32* %3
store i32 1, i32* %9
br label %45
19: ; preds = %2
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 1, i32* %3
store i32 1, i32* %9
br label %45
27: ; preds = %22, %19
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* %4
%32 = load i64, i64* @errno
%33 = call i32 @fatal(i32 3, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %31, i64 %32)
br label %34
34: ; preds = %30, %27
%35 = load i32, i32* %5
%36 = load i32, i32* %8
%37 = call i32 @write_fill(i32 %35, i8* %13, i32 %36)
%38 = load i32, i32* %8
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %44
40: ; preds = %34
%41 = load i32, i32* %5
%42 = load i64, i64* @errno
%43 = call i32 @fatal(i32 4, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %41, i64 %42)
br label %44
44: ; preds = %40, %34
store i32 1, i32* %3
store i32 1, i32* %9
br label %45
45: ; preds = %44, %26, %18
%46 = load i8*, i8** %6
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [28 x i8] c"Failed to read on fd %d: %d\00"
@.str.1 = [29 x i8] c"Failed to write on fd %d: %d\00"
define i32 @read_write(i32 %0, i32 %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @read(i32 %0, i8* %5, i32 %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %2
%9 = icmp slt i32 %6, 0
br i1 %9, label %10, label %16
10: ; preds = %8
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %22, label %14
14: ; preds = %10
%15 = tail call i32 @fatal(i32 3, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %0, i64 %11)
br label %16
16: ; preds = %8, %14
%17 = call i32 @write_fill(i32 %1, i8* %5, i32 %6)
%18 = icmp eq i32 %17, %6
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i64, i64* @errno
%21 = call i32 @fatal(i32 4, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %1, i64 %20)
br label %22
22: ; preds = %16, %19, %10, %2
%23 = phi i32 [ 0, %2 ], [ 1, %10 ], [ 1, %19 ], [ 1, %16 ]
ret i32 %23
}