#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ warnx(char *, scalar_t__);
int /*<<< orphan*/ write_exactly(int, char *, int);
void read_through(int src, int dst) {
  char buf[1024];
  while (src) {
    int rc = read(src, buf, 1023);
    if (rc == 0)
      break;
    if (rc < 0 && errno == EINTR)
      continue;
    for (; !(rc > 0); exit(EXIT_FAILURE))
      warnx("read_through() %d", errno);
    write_exactly(dst, buf, rc);
  }
}


x86 assembly (clang, -O0):
<read_through>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     120f <read_through+0xcf>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jne    1187 <read_through+0x47>
jmp    120f <read_through+0xcf>
cmpl   $0x0,-0x414(%rbp)
jge    11b3 <read_through+0x73>
mov    0x2e45(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
jne    11b3 <read_through+0x73>
jmp    1151 <read_through+0x11>
jmp    11b8 <read_through+0x78>
cmpl   $0x0,-0x414(%rbp)
setg   %al
xor    $0xff,%al
test   $0x1,%al
jne    11d1 <read_through+0x91>
jmp    11f5 <read_through+0xb5>
mov    0x2e08(%rip),%rax
mov    (%rax),%rsi
lea    0xe1e(%rip),%rdi
call   1030 <warnx@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1050 <write_exactly@plt>
jmp    1151 <read_through+0x11>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_through>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
test   %edi,%edi
je     11c7 <read_through+0x87>
mov    %esi,%r14d
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11c7 <read_through+0x87>
mov    0x2e70(%rip),%r15
mov    0x2e59(%rip),%r12
mov    %rsp,%rbx
jmp    11a0 <read_through+0x60>
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write_exactly@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11c7 <read_through+0x87>
test   %eax,%eax
jns    1180 <read_through+0x40>
mov    (%r15),%rsi
cmp    (%r12),%rsi
je     118d <read_through+0x4d>
lea    0xe4c(%rip),%rdi
call   1030 <warnx@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [18 x i8] c"read_through() %d\00"
@EXIT_FAILURE = global i32 0
define void @read_through(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
br label %7
7: ; preds = %36, %24, %2
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %41
10: ; preds = %7
%11 = load i32, i32* %3
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%13 = call i32 @read(i32 %11, i8* %12, i32 1023)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %10
br label %41
17: ; preds = %10
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %7
25: ; preds = %20, %17
br label %26
26: ; preds = %25
%27 = load i32, i32* %6
%28 = icmp sgt i32 %27, 0
%29 = xor i1 %28, true
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = call i32 @warnx(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %31)
br label %33
33: ; preds = %30
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %26
%37 = load i32, i32* %4
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%39 = load i32, i32* %6
%40 = call i32 @write_exactly(i32 %37, i8* %38, i32 %39)
br label %7
41: ; preds = %16, %7
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [18 x i8] c"read_through() %d\00"
@EXIT_FAILURE = global i32 0
define void @read_through(i32 %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 0
br i1 %5, label %25, label %6
6: ; preds = %2
%7 = call i32 @read(i32 %0, i8* %4, i32 1023)
%8 = icmp eq i32 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %6, %22
%10 = phi i32 [ %23, %22 ], [ %7, %6 ]
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %22, label %16
16: ; preds = %12
%17 = call i32 @warnx(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i64 %13)
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %9
%21 = call i32 @write_exactly(i32 %1, i8* %4, i32 %10)
br label %22
22: ; preds = %12, %20
%23 = call i32 @read(i32 %0, i8* %4, i32 1023)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %9
25: ; preds = %22, %6, %2
ret void
}