#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__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ MSG_WAITALL;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int recv(int, void *, size_t, int /*<<< orphan*/);
int readall(int fd, void *buffer, size_t sz) {
  for (;;) {
    int err = recv(fd, buffer, sz, MSG_WAITALL);
    if (err < 0) {
      if (errno == EAGAIN || errno == EINTR)
        continue;
      break;
    }
    return err;
  }
  perror("Socket error");
  exit(1);
}


x86 assembly (clang, -O0):
<readall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2e73(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a7 <readall+0x77>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
je     119d <readall+0x6d>
mov    0x2e56(%rip),%rax
mov    (%rax),%rax
mov    0x2e44(%rip),%rcx
cmp    (%rcx),%rax
jne    11a2 <readall+0x72>
jmp    1143 <readall+0x13>
jmp    11b0 <readall+0x80>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret
lea    0xe49(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

x86 assembly (clang, -O2):
<readall>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e7e(%rip),%r15
mov    0x2e8f(%rip),%r12
mov    0x2e68(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    (%r15),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <recv@plt>
test   %eax,%eax
jns    11a0 <readall+0x70>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1160 <readall+0x30>
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
je     1160 <readall+0x30>
lea    0xe6f(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_WAITALL = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [13 x i8] c"Socket error\00"
define i32 @readall(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %8
8: ; preds = %24, %3
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i64, i64* %6
%12 = load i32, i32* @MSG_WAITALL
%13 = call i32 @recv(i32 %9, i8* %10, i64 %11, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %8
%17 = load i64, i64* @errno
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %17, %18
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20, %16
br label %8
25: ; preds = %20
br label %28
26: ; preds = %8
%27 = load i32, i32* %7
ret i32 %27
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@MSG_WAITALL = global i32 0
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
@.str = [13 x i8] c"Socket error\00"
define i32 @readall(i32 %0, i8* %1, i64 %2) {
br label %4
4: ; preds = %8, %3
%5 = load i32, i32* @MSG_WAITALL
%6 = tail call i32 @recv(i32 %0, i8* %1, i64 %2, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %4
%9 = load i64, i64* @errno
%10 = load i64, i64* @EAGAIN
%11 = icmp eq i64 %9, %10
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %9, %12
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %4, label %15
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %4
ret i32 %6
}