#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef unsigned int uint32_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ errno;
scalar_t__ read(int, int /*<<< orphan*/ *, unsigned int);
int ReadData(int socket_fd, uint8_t *buffer, uint32_t length) {
  ssize_t numread;
  unsigned total = 0;
  do {
    numread = read(socket_fd, buffer + total, length - total);
    if (!numread)
      return 0;
    else if (numread > 0)
      total += numread;
    else if (errno != EINTR && errno != EAGAIN)
      return -1;
  } while (total < length);
  if (total < length)
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<ReadData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%eax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x24(%rbp),%edx
call   1030 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1160 <ReadData+0x50>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <ReadData+0xe7>
cmpq   $0x0,-0x20(%rbp)
jle    117d <ReadData+0x6d>
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    11c2 <ReadData+0xb2>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%rax
je     11bd <ReadData+0xad>
mov    0x2e42(%rip),%rax
mov    (%rax),%rax
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%rax
je     11bd <ReadData+0xad>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f7 <ReadData+0xe7>
jmp    11c2 <ReadData+0xb2>
jmp    11c7 <ReadData+0xb7>
jmp    11cc <ReadData+0xbc>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jb     1129 <ReadData+0x19>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11f0 <ReadData+0xe0>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <ReadData+0xe7>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadData>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2eb7(%rip),%r12
mov    0x2ea8(%rip),%r13
xor    %r14d,%r14d
jmp    1148 <ReadData+0x38>
cs nopw 0x0(%rax,%rax,1)
nop
add    %eax,%r14d
cmp    %ebp,%r14d
jae    1183 <ReadData+0x73>
mov    %r14d,%eax
lea    (%r15,%rax,4),%rsi
mov    %ebp,%edx
sub    %r14d,%edx
mov    %ebx,%edi
call   1030 <read@plt>
test   %rax,%rax
je     117f <ReadData+0x6f>
jg     1140 <ReadData+0x30>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1143 <ReadData+0x33>
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%rax
je     1143 <ReadData+0x33>
mov    $0xffffffff,%eax
jmp    1188 <ReadData+0x78>
xor    %eax,%eax
jmp    1188 <ReadData+0x78>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @ReadData(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %9
br label %10
10: ; preds = %44, %3
%11 = load i32, i32* %5
%12 = load i32*, i32** %6
%13 = load i32, i32* %9
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %12, i64 %14
%16 = load i32, i32* %7
%17 = load i32, i32* %9
%18 = sub i32 %16, %17
%19 = call i64 @read(i32 %11, i32* %15, i32 %18)
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = icmp ne i64 %20, 0
br i1 %21, label %23, label %22
22: ; preds = %10
store i32 0, i32* %4
br label %54
23: ; preds = %10
%24 = load i64, i64* %8
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = load i64, i64* %8
%28 = load i32, i32* %9
%29 = zext i32 %28 to i64
%30 = add nsw i64 %29, %27
%31 = trunc i64 %30 to i32
store i32 %31, i32* %9
br label %42
32: ; preds = %23
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %41
36: ; preds = %32
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 -1, i32* %4
br label %54
41: ; preds = %36, %32
br label %42
42: ; preds = %41, %26
br label %43
43: ; preds = %42
br label %44
44: ; preds = %43
%45 = load i32, i32* %9
%46 = load i32, i32* %7
%47 = icmp ult i32 %45, %46
br i1 %47, label %10, label %48
48: ; preds = %44
%49 = load i32, i32* %9
%50 = load i32, i32* %7
%51 = icmp ult i32 %49, %50
br i1 %51, label %52, label %53
52: ; preds = %48
store i32 0, i32* %4
br label %54
53: ; preds = %48
store i32 1, i32* %4
br label %54
54: ; preds = %53, %52, %40, %22
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
define i32 @ReadData(i32 %0, i32* nocapture %1, i32 %2) {
br label %4
4: ; preds = %23, %3
%5 = phi i32 [ 0, %3 ], [ %24, %23 ]
%6 = zext i32 %5 to i64
%7 = getelementptr inbounds i32, i32* %1, i64 %6
%8 = sub i32 %2, %5
%9 = tail call i64 @read(i32 %0, i32* %7, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %26, label %11
11: ; preds = %4
%12 = icmp sgt i64 %9, 0
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = trunc i64 %9 to i32
%15 = add i32 %5, %14
br label %23
16: ; preds = %11
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINTR
%19 = icmp eq i64 %17, %18
%20 = load i64, i64* @EAGAIN
%21 = icmp eq i64 %17, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %23, label %26
23: ; preds = %16, %13
%24 = phi i32 [ %15, %13 ], [ %5, %16 ]
%25 = icmp ult i32 %24, %2
br i1 %25, label %4, label %26
26: ; preds = %23, %16, %4
%27 = phi i32 [ 0, %4 ], [ -1, %16 ], [ 1, %23 ]
ret i32 %27
}