#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__ hasDataAvailable(int, int);
int read(int, void *, size_t);
int /*<<< orphan*/ setNonBlocking(int, int);
int read_bytes(int socketfd, void *buf, size_t count) {
  setNonBlocking(socketfd, 1);
  while (count > 0) {
    int numread = read(socketfd, buf, count);
    if (numread == -1 && hasDataAvailable(socketfd, 1000000)) {
      continue;
    } else if (numread == -1) {
      return count;
    }
    count -= numread;
    buf += numread;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1050 <setNonBlocking@plt>
cmpq   $0x0,-0x18(%rbp)
jbe    11d4 <read_bytes+0xa4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1194 <read_bytes+0x64>
mov    -0x8(%rbp),%edi
mov    $0xf4240,%esi
call   1030 <hasDataAvailable@plt>
cmp    $0x0,%rax
je     1194 <read_bytes+0x64>
jmp    1150 <read_bytes+0x20>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11aa <read_bytes+0x7a>
mov    -0x18(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11db <read_bytes+0xab>
jmp    11af <read_bytes+0x7f>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1150 <read_bytes+0x20>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_bytes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
mov    $0x1,%esi
call   1050 <setNonBlocking@plt>
xor    %r14d,%r14d
jmp    1158 <read_bytes+0x28>
nop
cltq
sub    %rax,%rbx
add    %rax,%rbp
test   %rbx,%rbx
je     1185 <read_bytes+0x55>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
jne    1150 <read_bytes+0x20>
mov    %r15d,%edi
mov    $0xf4240,%esi
call   1030 <hasDataAvailable@plt>
test   %rax,%rax
jne    1158 <read_bytes+0x28>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_bytes(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i32, i32* %5
%10 = call i32 @setNonBlocking(i32 %9, i32 1)
br label %11
11: ; preds = %33, %25, %3
%12 = load i64, i64* %7
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %42
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %7
%18 = call i32 @read(i32 %15, i8* %16, i64 %17)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i32, i32* %5
%23 = call i64 @hasDataAvailable(i32 %22, i32 1000000)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
br label %11
26: ; preds = %21, %14
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i64, i64* %7
%31 = trunc i64 %30 to i32
store i32 %31, i32* %4
br label %43
32: ; preds = %26
br label %33
33: ; preds = %32
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = load i64, i64* %7
%37 = sub i64 %36, %35
store i64 %37, i64* %7
%38 = load i32, i32* %8
%39 = load i8*, i8** %6
%40 = sext i32 %38 to i64
%41 = getelementptr i8, i8* %39, i64 %40
store i8* %41, i8** %6
br label %11
42: ; preds = %11
store i32 0, i32* %4
br label %43
43: ; preds = %42, %29
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
define i32 @read_bytes(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @setNonBlocking(i32 %0, i32 1)
%5 = icmp eq i64 %2, 0
br i1 %5, label %24, label %6
6: ; preds = %3, %20
%7 = phi i64 [ %22, %20 ], [ %2, %3 ]
%8 = phi i8* [ %21, %20 ], [ %1, %3 ]
%9 = tail call i32 @read(i32 %0, i8* %8, i64 %7)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = tail call i64 @hasDataAvailable(i32 %0, i32 1000000)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = trunc i64 %7 to i32
br label %24
16: ; preds = %6
%17 = sext i32 %9 to i64
%18 = sub i64 %7, %17
%19 = getelementptr i8, i8* %8, i64 %17
br label %20
20: ; preds = %11, %16
%21 = phi i8* [ %19, %16 ], [ %8, %11 ]
%22 = phi i64 [ %18, %16 ], [ %7, %11 ]
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %6
24: ; preds = %20, %3, %14
%25 = phi i32 [ %15, %14 ], [ 0, %3 ], [ 0, %20 ]
ret i32 %25
}