#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 ssize_t;

/* Variables and functions */
int errno;
int /*<<< orphan*/ printf(char *, ...);
int read(int, void *, size_t);
int readBytes(int sockFd, void *buffer, size_t count) {
  size_t bytesRead = 0;
  ssize_t result;
  while (bytesRead < count) {
    result = read(sockFd, buffer + bytesRead, count - bytesRead);
    if (result < 1) {
      if (result == 0) {
        printf("Could not read from socket. Zero returned.\n");
        return 0;
      } else {
        printf("Could not read from socket. errno=%i\n", errno);
        return 0;
      }
    }
    bytesRead += result;
  }
  return 1;
}


x86 assembly (clang, -O0):
<readBytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11c6 <readBytes+0xa6>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jge    11b5 <readBytes+0x95>
cmpl   $0x0,-0x24(%rbp)
jne    1192 <readBytes+0x72>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cd <readBytes+0xad>
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cd <readBytes+0xad>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    113b <readBytes+0x1b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readBytes>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x1,%r14d
test   %rdx,%rdx
je     119f <readBytes+0x6f>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebp,%ebp
xchg   %ax,%ax
lea    (%r15,%rbp,1),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r12d,%edi
call   1050 <read@plt>
test   %eax,%eax
jle    1172 <readBytes+0x42>
mov    %eax,%eax
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1150 <readBytes+0x20>
jmp    119f <readBytes+0x6f>
je     1190 <readBytes+0x60>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe7c(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <printf@plt>
jmp    119f <readBytes+0x6f>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [44 x i8] c"Could not read from socket. Zero returned.\0A\00"
@.str.1 = [38 x i8] c"Could not read from socket. errno=%i\0A\00"
@errno = global i32 0
define i32 @readBytes(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
br label %10
10: ; preds = %33, %3
%11 = load i64, i64* %8
%12 = load i64, i64* %7
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %38
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = load i64, i64* %8
%18 = getelementptr i8, i8* %16, i64 %17
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = sub i64 %19, %20
%22 = call i32 @read(i32 %15, i8* %18, i64 %21)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %33
25: ; preds = %14
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %39
30: ; preds = %25
%31 = load i32, i32* @errno
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 0, i32* %4
br label %39
33: ; preds = %14
%34 = load i32, i32* %9
%35 = sext i32 %34 to i64
%36 = load i64, i64* %8
%37 = add i64 %36, %35
store i64 %37, i64* %8
br label %10
38: ; preds = %10
store i32 1, i32* %4
br label %39
39: ; preds = %38, %30, %28
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@.str.1 = [38 x i8] c"Could not read from socket. errno=%i\0A\00"
@errno = global i32 0
@str = [43 x i8] c"Could not read from socket. Zero returned.\00"
define i32 @readBytes(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %22, label %7
5: ; preds = %7
%6 = icmp ult i64 %14, %2
br i1 %6, label %7, label %22
7: ; preds = %3, %5
%8 = phi i64 [ %14, %5 ], [ 0, %3 ]
%9 = getelementptr i8, i8* %1, i64 %8
%10 = sub i64 %2, %8
%11 = tail call i32 @read(i32 %0, i8* %9, i64 %10)
%12 = icmp slt i32 %11, 1
%13 = zext i32 %11 to i64
%14 = add i64 %8, %13
br i1 %12, label %15, label %5
15: ; preds = %7
%16 = icmp eq i32 %11, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
br label %22
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %5, %3, %19, %17
%23 = phi i32 [ 0, %17 ], [ 0, %19 ], [ 1, %3 ], [ 1, %5 ]
ret i32 %23
}