#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 /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int timeoutread(int, int /*<<< orphan*/, char *, unsigned int);
void netread(char *buf, unsigned int len) {
  int r = 0;
  while (len > 0) {
    r = timeoutread(60, 0, buf, len);
    if (r == 0)
      exit(0);
    if (r < 0)
      err(-1, "could not read from the network");
    buf += r;
    len -= r;
  }
}


x86 assembly (clang, -O0):
<netread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jbe    11b3 <netread+0x83>
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    $0x3c,%edi
xor    %esi,%esi
call   1030 <timeoutread@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1177 <netread+0x47>
xor    %edi,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
jge    1192 <netread+0x62>
mov    $0xffffffff,%edi
lea    0xe73(%rip),%rsi
call   1040 <err@plt>
mov    -0x10(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1146 <netread+0x16>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
je     1184 <netread+0x54>
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0xeb8(%rip),%r14
jmp    115b <netread+0x2b>
nopw   0x0(%rax,%rax,1)
movslq %ebp,%rax
add    %rax,%rbx
sub    %ebp,%r15d
je     1184 <netread+0x54>
mov    $0x3c,%edi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1030 <timeoutread@plt>
test   %eax,%eax
je     118f <netread+0x5f>
mov    %eax,%ebp
jns    1150 <netread+0x20>
mov    $0xffffffff,%edi
mov    %r14,%rsi
call   1040 <err@plt>
jmp    1150 <netread+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"could not read from the network\00"
define void @netread(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %22, %2
%7 = load i32, i32* %4
%8 = icmp ugt i32 %7, 0
br i1 %8, label %9, label %30
9: ; preds = %6
%10 = load i8*, i8** %3
%11 = load i32, i32* %4
%12 = call i32 @timeoutread(i32 60, i32 0, i8* %10, i32 %11)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %9
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @err(i32 -1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* %5
%24 = load i8*, i8** %3
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8* %26, i8** %3
%27 = load i32, i32* %5
%28 = load i32, i32* %4
%29 = sub i32 %28, %27
store i32 %29, i32* %4
br label %6
30: ; preds = %6
ret void
}

LLVM IR (-O2):
@.str = [32 x i8] c"could not read from the network\00"
define void @netread(i8* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %20, label %4
4: ; preds = %2, %15
%5 = phi i32 [ %18, %15 ], [ %1, %2 ]
%6 = phi i8* [ %17, %15 ], [ %0, %2 ]
%7 = tail call i32 @timeoutread(i32 60, i32 0, i8* %6, i32 %5)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %4
%12 = icmp slt i32 %7, 0
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i32 @err(i32 -1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %11
%16 = sext i32 %7 to i64
%17 = getelementptr inbounds i8, i8* %6, i64 %16
%18 = sub i32 %5, %7
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %4
20: ; preds = %15, %2
ret void
}