#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;
scalar_t__ EPIPE;
scalar_t__ errno;
int read(int, char *, int);
int network_read(int fd, char *data, int length) {
  int total = 0;
  int piece;
  while (total != length) {
    piece = read(fd, &data[total], (length - total));
    if (piece > 0) {
      total += piece;
    } else {
      if (piece == -1 && errno == EINTR) {
        continue;
      } else if (piece == 0) {
        errno = EPIPE;
        break;
      } else {
        break;
      }
    }
  }
  if (total == length) {
    return 1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<network_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     11bf <network_read+0xaf>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x18(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1169 <network_read+0x59>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11ba <network_read+0xaa>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1192 <network_read+0x82>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rax
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
jne    1192 <network_read+0x82>
jmp    1129 <network_read+0x19>
cmpl   $0x0,-0x1c(%rbp)
jne    11b5 <network_read+0xa5>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rcx
mov    0x2e2b(%rip),%rax
mov    %rcx,(%rax)
jmp    11bf <network_read+0xaf>
jmp    11bf <network_read+0xaf>
jmp    1129 <network_read+0x19>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    11d7 <network_read+0xc7>
movl   $0x1,-0x4(%rbp)
jmp    11de <network_read+0xce>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<network_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
je     119a <network_read+0x8a>
mov    %edx,%r15d
mov    %edi,%r12d
xor    %ecx,%ecx
mov    0x2ea7(%rip),%r13
mov    0x2e98(%rip),%r14
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rsi,0x10(%rsp)
mov    %ecx,0xc(%rsp)
cs nopw 0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
mov    %eax,%ecx
test   %eax,%eax
jg     1180 <network_read+0x70>
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11a1 <network_read+0x91>
mov    0x0(%r13),%rcx
cmp    (%r14),%rcx
je     1150 <network_read+0x40>
jmp    11b3 <network_read+0xa3>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0xc(%rsp),%eax
add    %ecx,%eax
mov    %eax,%ecx
cltq
mov    0x10(%rsp),%rsi
lea    (%rsi,%rax,1),%rbx
mov    %r15d,%ebp
sub    %eax,%ebp
jne    1142 <network_read+0x32>
mov    $0x1,%eax
jmp    11b3 <network_read+0xa3>
test   %ecx,%ecx
jne    11b3 <network_read+0xa3>
mov    0x2e34(%rip),%rcx
mov    (%rcx),%rcx
mov    %rcx,0x0(%r13)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EPIPE = global i64 0
define i32 @network_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %10
10: ; preds = %44, %37, %3
%11 = load i32, i32* %8
%12 = load i32, i32* %7
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %45
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = sub nsw i32 %20, %21
%23 = call i32 @read(i32 %15, i8* %19, i32 %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %14
%27 = load i32, i32* %9
%28 = load i32, i32* %8
%29 = add nsw i32 %28, %27
store i32 %29, i32* %8
br label %44
30: ; preds = %14
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %33
br label %10
38: ; preds = %33, %30
%39 = load i32, i32* %9
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = load i64, i64* @EPIPE
store i64 %42, i64* @errno
br label %45
43: ; preds = %38
br label %45
44: ; preds = %26
br label %10
45: ; preds = %43, %41, %10
%46 = load i32, i32* %8
%47 = load i32, i32* %7
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 1, i32* %4
br label %51
50: ; preds = %45
store i32 0, i32* %4
br label %51
51: ; preds = %50, %49
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EPIPE = global i64 0
define i32 @network_read(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %25, label %5
5: ; preds = %3, %12
%6 = phi i32 [ %17, %12 ], [ %2, %3 ]
%7 = phi i8* [ %16, %12 ], [ %1, %3 ]
%8 = phi i32 [ %13, %12 ], [ 0, %3 ]
br label %9
9: ; preds = %5, %19
%10 = tail call i32 @read(i32 %0, i8* %7, i32 %6)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = add nsw i32 %10, %8
%14 = icmp eq i32 %13, %2
%15 = sext i32 %13 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = sub nsw i32 %2, %13
br i1 %14, label %25, label %5
18: ; preds = %9
switch i32 %10, label %25 [
i32 -1, label %19
i32 0, label %23
]
19: ; preds = %18
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %9, label %25
23: ; preds = %18
%24 = load i64, i64* @EPIPE
store i64 %24, i64* @errno
br label %25
25: ; preds = %12, %18, %19, %3, %23
%26 = phi i32 [ 0, %23 ], [ 1, %3 ], [ 0, %19 ], [ 0, %18 ], [ 1, %12 ]
ret i32 %26
}