#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 */
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ sock_check_opened(int);
int sock_data_read(int socket, char *buf, int nlen) {
  int n, num_bytes;
  if ((socket < 0) || (buf == NULL))
    return -1;
  if (!sock_check_opened(socket))
    return -1;
  num_bytes = 0;
  while (num_bytes < nlen) {
    n = read(socket, &buf[num_bytes], nlen - num_bytes);
    if (n < 0) {
      if (errno == EAGAIN) {
        fd_set rset;
        struct timeval timeout;
        FD_ZERO(&rset);
        FD_SET(socket, &rset);
        timeout.tv_sec = 30;
        timeout.tv_usec = 0;
        if (select(socket + 1, &rset, NULL, NULL, &timeout) <= 0) {
          printf("network: timeout on read\n");
          return 0;
        }
        continue;
      }
      printf("network: read error %d\n", errno);
      return 0;
    }
    num_bytes += n;
    if (!n)
      break;
  }
  return num_bytes;
}


x86 assembly (clang, -O0):
<sock_data_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1187 <sock_data_read+0x27>
cmpq   $0x0,-0x10(%rbp)
jne    1193 <sock_data_read+0x33>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <sock_data_read+0x151>
mov    -0x8(%rbp),%edi
call   1070 <sock_check_opened@plt>
cmp    $0x0,%eax
jne    11b0 <sock_data_read+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <sock_data_read+0x151>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12ab <sock_data_read+0x14b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    128e <sock_data_read+0x12e>
mov    0x2df0(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
jne    126a <sock_data_read+0x10a>
lea    -0x20(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <FD_SET@plt>
movl   $0x1e,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
cmp    $0x0,%rax
jg     1265 <sock_data_read+0x105>
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b1 <sock_data_read+0x151>
jmp    11b7 <sock_data_read+0x57>
mov    0x2d6f(%rip),%rax
mov    (%rax),%rsi
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b1 <sock_data_read+0x151>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    12a6 <sock_data_read+0x146>
jmp    12ab <sock_data_read+0x14b>
jmp    11b7 <sock_data_read+0x57>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_data_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0xffffffff,%r12d
test   %edi,%edi
js     1281 <sock_data_read+0x111>
mov    %rsi,%rbp
test   %rsi,%rsi
je     1281 <sock_data_read+0x111>
mov    %edx,%r15d
mov    %edi,%r13d
call   1080 <sock_check_opened@plt>
test   %eax,%eax
je     1281 <sock_data_read+0x111>
lea    0x1(%r13),%eax
mov    %eax,0x4(%rsp)
xor    %r12d,%r12d
mov    %r15d,(%rsp)
mov    %rbp,0x8(%rsp)
mov    0x4(%rsp),%ebx
jmp    11e4 <sock_data_read+0x74>
cs nopw 0x0(%rax,%rax,1)
nop
add    %eax,%r12d
test   %eax,%eax
mov    (%rsp),%r15d
mov    0x8(%rsp),%rbp
je     1281 <sock_data_read+0x111>
cmp    %r15d,%r12d
jge    1281 <sock_data_read+0x111>
movslq %r12d,%r14
add    %rbp,%r14
sub    %r12d,%r15d
mov    %r13d,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1050 <read@plt>
test   %eax,%eax
jns    11d0 <sock_data_read+0x60>
mov    0x2dd1(%rip),%rax
mov    (%rax),%rsi
mov    0x2db7(%rip),%rax
cmp    (%rax),%rsi
jne    1270 <sock_data_read+0x100>
lea    0x14(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
movl   $0x1e,0x18(%rsp)
movq   $0x0,0x20(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x18(%rsp),%r8
call   1070 <select@plt>
test   %rax,%rax
jg     11f6 <sock_data_read+0x86>
lea    0xdb2(%rip),%rdi
call   1030 <puts@plt>
xor    %r12d,%r12d
jmp    1281 <sock_data_read+0x111>
lea    0xd89(%rip),%rdi
xor    %r12d,%r12d
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EAGAIN = global i64 0
@.str = [26 x i8] c"network: timeout on read\0A\00"
@.str.1 = [24 x i8] c"network: read error %d\0A\00"
define i32 @sock_data_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
%10 = alloca i32
%11 = alloca %struct.timeval
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %14, %3
store i32 -1, i32* %4
br label %70
18: ; preds = %14
%19 = load i32, i32* %5
%20 = call i32 @sock_check_opened(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %18
store i32 -1, i32* %4
br label %70
23: ; preds = %18
store i32 0, i32* %9
br label %24
24: ; preds = %67, %56, %23
%25 = load i32, i32* %9
%26 = load i32, i32* %7
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %68
28: ; preds = %24
%29 = load i32, i32* %5
%30 = load i8*, i8** %6
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i32, i32* %7
%35 = load i32, i32* %9
%36 = sub nsw i32 %34, %35
%37 = call i32 @read(i32 %29, i8* %33, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %60
40: ; preds = %28
%41 = load i64, i64* @errno
%42 = load i64, i64* @EAGAIN
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %57
44: ; preds = %40
%45 = call i32 @FD_ZERO(i32* %10)
%46 = load i32, i32* %5
%47 = call i32 @FD_SET(i32 %46, i32* %10)
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 30, i32* %48
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %49
%50 = load i32, i32* %5
%51 = add nsw i32 %50, 1
%52 = call i64 @select(i32 %51, i32* %10, i32* null, i32* null, %struct.timeval* %11)
%53 = icmp sle i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %44
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %70
56: ; preds = %44
br label %24
57: ; preds = %40
%58 = load i64, i64* @errno
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %58)
store i32 0, i32* %4
br label %70
60: ; preds = %28
%61 = load i32, i32* %8
%62 = load i32, i32* %9
%63 = add nsw i32 %62, %61
store i32 %63, i32* %9
%64 = load i32, i32* %8
%65 = icmp ne i32 %64, 0
br i1 %65, label %67, label %66
66: ; preds = %60
br label %68
67: ; preds = %60
br label %24
68: ; preds = %66, %24
%69 = load i32, i32* %9
store i32 %69, i32* %4
br label %70
70: ; preds = %68, %57, %54, %22, %17
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@errno = global i64 0
@EAGAIN = global i64 0
@.str.1 = [24 x i8] c"network: read error %d\0A\00"
@str = [25 x i8] c"network: timeout on read\00"
define i32 @sock_data_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = icmp slt i32 %0, 0
%7 = icmp eq i8* %1, null
%8 = or i1 %6, %7
br i1 %8, label %47, label %9
9: ; preds = %3
%10 = tail call i32 @sock_check_opened(i32 %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %47, label %12
12: ; preds = %9
%13 = bitcast i32* %4 to i8*
%14 = bitcast %struct.timeval* %5 to i8*
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
%17 = add nuw nsw i32 %0, 1
br label %18
18: ; preds = %12, %43
%19 = phi i32 [ 0, %12 ], [ %45, %43 ]
%20 = icmp slt i32 %19, %2
br i1 %20, label %21, label %47
21: ; preds = %18
%22 = sext i32 %19 to i64
%23 = getelementptr inbounds i8, i8* %1, i64 %22
%24 = sub nsw i32 %2, %19
%25 = call i32 @read(i32 %0, i8* %23, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %30, label %43
27: ; preds = %34
%28 = call i32 @read(i32 %0, i8* %23, i32 %24)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %43
30: ; preds = %21, %27
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %41
34: ; preds = %30
%35 = call i32 @FD_ZERO(i32* %4)
%36 = call i32 @FD_SET(i32 %0, i32* %4)
store i32 30, i32* %15
store i64 0, i64* %16
%37 = call i64 @select(i32 %17, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%38 = icmp slt i64 %37, 1
br i1 %38, label %39, label %27
39: ; preds = %34
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %47
41: ; preds = %30
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %31)
br label %47
43: ; preds = %27, %21
%44 = phi i32 [ %25, %21 ], [ %28, %27 ]
%45 = add nsw i32 %44, %19
%46 = icmp eq i32 %44, 0
br i1 %46, label %47, label %18
47: ; preds = %18, %43, %39, %9, %3, %41
%48 = phi i32 [ 0, %39 ], [ 0, %41 ], [ -1, %3 ], [ -1, %9 ], [ %19, %18 ], [ %45, %43 ]
ret i32 %48
}