#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 */
#define EAGAIN 129
#define EINTR 128
int errno;
int read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int read_socket(int fd, char *readBuffer, int toReadLength) {
  int bytesRead = 0;
  int aux = 0;
  if ((fd == -1) || (readBuffer == NULL) || (toReadLength < 1))
    return -1;
  while (bytesRead < toReadLength) {
    aux = read(fd, readBuffer + bytesRead, toReadLength - bytesRead);
    if (aux > 0) {
      bytesRead = bytesRead + aux;
    } else {
      if (aux == 0)
        return bytesRead;
      if (aux == -1) {
        switch (errno) {
        case EINTR:
        case EAGAIN:
          usleep(100);
          break;
        default:
          return -1;
        }
      }
    }
  }
  return bytesRead;
}


x86 assembly (clang, -O0):
<read_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     115f <read_socket+0x3f>
cmpq   $0x0,-0x10(%rbp)
je     115f <read_socket+0x3f>
cmpl   $0x1,-0x14(%rbp)
jge    116b <read_socket+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <read_socket+0xf9>
jmp    1170 <read_socket+0x50>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1213 <read_socket+0xf3>
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    11b0 <read_socket+0x90>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    120e <read_socket+0xee>
cmpl   $0x0,-0x1c(%rbp)
jne    11c5 <read_socket+0xa5>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1219 <read_socket+0xf9>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1209 <read_socket+0xe9>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     11f8 <read_socket+0xd8>
jmp    11e9 <read_socket+0xc9>
mov    $0x64,%edi
call   1040 <usleep@plt>
jmp    1204 <read_socket+0xe4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <read_socket+0xf9>
jmp    1209 <read_socket+0xe9>
jmp    120e <read_socket+0xee>
jmp    1170 <read_socket+0x50>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0xffffffff,%edi
je     1192 <read_socket+0x72>
mov    %rsi,%r14
test   %rsi,%rsi
je     1192 <read_socket+0x72>
mov    %edx,%ebx
test   %edx,%edx
jle    1192 <read_socket+0x72>
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2e99(%rip),%r12
jmp    1156 <read_socket+0x36>
nopl   0x0(%rax)
add    %eax,%ebp
cmp    %ebx,%ebp
jge    1197 <read_socket+0x77>
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1030 <read@plt>
test   %eax,%eax
jg     1150 <read_socket+0x30>
cmp    $0xffffffff,%eax
je     1178 <read_socket+0x58>
test   %eax,%eax
jne    1152 <read_socket+0x32>
jmp    1197 <read_socket+0x77>
mov    (%r12),%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    1192 <read_socket+0x72>
mov    $0x64,%edi
call   1040 <usleep@plt>
jmp    1152 <read_socket+0x32>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @read_socket(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
store i32 0, i32* %9
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %18, label %12
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %19
18: ; preds = %15, %12, %3
store i32 -1, i32* %4
br label %58
19: ; preds = %15
br label %20
20: ; preds = %55, %19
%21 = load i32, i32* %8
%22 = load i32, i32* %7
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %56
24: ; preds = %20
%25 = load i32, i32* %5
%26 = load i8*, i8** %6
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i32, i32* %7
%31 = load i32, i32* %8
%32 = sub nsw i32 %30, %31
%33 = call i32 @read(i32 %25, i8* %29, i32 %32)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %24
%37 = load i32, i32* %8
%38 = load i32, i32* %9
%39 = add nsw i32 %37, %38
store i32 %39, i32* %8
br label %55
40: ; preds = %24
%41 = load i32, i32* %9
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = load i32, i32* %8
store i32 %44, i32* %4
br label %58
45: ; preds = %40
%46 = load i32, i32* %9
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %54
48: ; preds = %45
%49 = load i32, i32* @errno
switch i32 %49, label %52 [
i32 128, label %50
i32 129, label %50
]
50: ; preds = %48, %48
%51 = call i32 @usleep(i32 100)
br label %53
52: ; preds = %48
store i32 -1, i32* %4
br label %58
53: ; preds = %50
br label %54
54: ; preds = %53, %45
br label %55
55: ; preds = %54, %36
br label %20
56: ; preds = %20
%57 = load i32, i32* %8
store i32 %57, i32* %4
br label %58
58: ; preds = %56, %52, %43, %18
%59 = load i32, i32* %4
ret i32 %59
}

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