#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 EINTR;
int /*<<< orphan*/ debug(int, char *);
int read(int, char *, int);
int /*<<< orphan*/ socket_wait_read(int, int);
int socket_read(int socket, void *buffer, int size, int timeout) {
  int received;
  int readval;
  for (received = 0; received < size;) {
    if (!socket_wait_read(socket, timeout)) {
      debug(2, "Socket read timed out.");
      return 0;
    }
    readval = read(socket, ((char *)buffer) + received, size - received);
    if (readval == EINTR)
      continue;
    if (readval <= 0) {
      debug(2, "Read in socket read failed.");
      return 0;
    }
    received += readval;
  }
  return 1;
}


x86 assembly (clang, -O0):
<socket_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11f1 <socket_read+0xc1>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <socket_wait_read@plt>
cmp    $0x0,%eax
jne    1189 <socket_read+0x59>
mov    $0x2,%edi
lea    0xe88(%rip),%rsi
call   1040 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <socket_read+0xc8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%eax
jne    11bc <socket_read+0x8c>
jmp    114c <socket_read+0x1c>
cmpl   $0x0,-0x20(%rbp)
jg     11e3 <socket_read+0xb3>
mov    $0x2,%edi
lea    0xe45(%rip),%rsi
call   1040 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <socket_read+0xc8>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    114c <socket_read+0x1c>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0x1,%eax
test   %edx,%edx
jle    11c6 <socket_read+0x96>
mov    %ecx,%ebp
mov    %edi,%ebx
xor    %r15d,%r15d
mov    0x2e8b(%rip),%r14
mov    %edx,%r13d
mov    %rsi,%r12
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %ebp,%esi
call   1030 <socket_wait_read@plt>
test   %eax,%eax
je     11b3 <socket_read+0x83>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %r13d,%edx
call   1050 <read@plt>
cmp    (%r14),%eax
je     1170 <socket_read+0x40>
test   %eax,%eax
jle    11d5 <socket_read+0xa5>
add    %eax,%r15d
movslq %r15d,%rax
mov    0x10(%rsp),%rsi
lea    (%rsi,%rax,1),%r12
mov    0xc(%rsp),%r13d
sub    %eax,%r13d
jg     1170 <socket_read+0x40>
mov    $0x1,%eax
jmp    11c6 <socket_read+0x96>
lea    0xe46(%rip),%rsi
mov    $0x2,%edi
call   1040 <debug@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe3b(%rip),%rsi
jmp    11ba <socket_read+0x8a>

LLVM IR (-O0):
@.str = [23 x i8] c"Socket read timed out.\00"
@EINTR = global i32 0
@.str.1 = [28 x i8] c"Read in socket read failed.\00"
define i32 @socket_read(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
br label %12
12: ; preds = %42, %36, %4
%13 = load i32, i32* %10
%14 = load i32, i32* %8
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %46
16: ; preds = %12
%17 = load i32, i32* %6
%18 = load i32, i32* %9
%19 = call i32 @socket_wait_read(i32 %17, i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = call i32 @debug(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %47
23: ; preds = %16
%24 = load i32, i32* %6
%25 = load i8*, i8** %7
%26 = load i32, i32* %10
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i32, i32* %8
%30 = load i32, i32* %10
%31 = sub nsw i32 %29, %30
%32 = call i32 @read(i32 %24, i8* %28, i32 %31)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = load i32, i32* @EINTR
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %23
br label %12
37: ; preds = %23
%38 = load i32, i32* %11
%39 = icmp sle i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @debug(i32 2, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %47
42: ; preds = %37
%43 = load i32, i32* %11
%44 = load i32, i32* %10
%45 = add nsw i32 %44, %43
store i32 %45, i32* %10
br label %12
46: ; preds = %12
store i32 1, i32* %5
br label %47
47: ; preds = %46, %40, %21
%48 = load i32, i32* %5
ret i32 %48
}

LLVM IR (-O2):
@.str = [23 x i8] c"Socket read timed out.\00"
@EINTR = global i32 0
@.str.1 = [28 x i8] c"Read in socket read failed.\00"
define i32 @socket_read(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %11, label %28
6: ; preds = %22
%7 = icmp slt i32 %24, %2
%8 = sext i32 %24 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = sub nsw i32 %2, %24
br i1 %7, label %11, label %28
11: ; preds = %4, %6
%12 = phi i32 [ %10, %6 ], [ %2, %4 ]
%13 = phi i8* [ %9, %6 ], [ %1, %4 ]
%14 = phi i32 [ %24, %6 ], [ 0, %4 ]
br label %15
15: ; preds = %11, %18
%16 = tail call i32 @socket_wait_read(i32 %0, i32 %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = tail call i32 @read(i32 %0, i8* %13, i32 %12)
%20 = load i32, i32* @EINTR
%21 = icmp eq i32 %19, %20
br i1 %21, label %15, label %22
22: ; preds = %18
%23 = icmp slt i32 %19, 1
%24 = add nsw i32 %19, %14
br i1 %23, label %25, label %6
25: ; preds = %22, %15
%26 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), %15 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %22 ]
%27 = tail call i32 @debug(i32 2, i8* %26)
br label %28
28: ; preds = %6, %25, %4
%29 = phi i32 [ 1, %4 ], [ 0, %25 ], [ 1, %6 ]
ret i32 %29
}