#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ fatal(char *, int /*<<< orphan*/);
int read(int, void *, size_t);
int /*<<< orphan*/ strerror(scalar_t__);
void xread(int fd, void *buf, size_t count) {
  ssize_t ret = read(fd, buf, count);
  if (ret == (ssize_t)count) {
    return;
  } else if (ret == -1) {
    if (errno != EINTR)
      fatal("Unable to read from socket: %s", strerror(errno));
    else
      ret = 0;
  }
  xread(fd, (char *)buf + ret, count - ret);
}


x86 assembly (clang, -O0):
<xread>:
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
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x18(%rbp),%rcx
cmp    %ecx,%eax
jne    117a <xread+0x3a>
jmp    11ef <xread+0xaf>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11cc <xread+0x8c>
mov    0x2e55(%rip),%rax
mov    (%rax),%rax
mov    0x2e43(%rip),%rcx
cmp    (%rcx),%rax
je     11c0 <xread+0x80>
mov    0x2e3b(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    %eax,%esi
lea    0xe4a(%rip),%rdi
call   1060 <fatal@plt>
jmp    11c7 <xread+0x87>
movl   $0x0,-0x1c(%rbp)
jmp    11cc <xread+0x8c>
jmp    11d1 <xread+0x91>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
movslq -0x1c(%rbp),%rax
sub    %rax,%rdx
call   1040 <xread@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
call   1030 <read@plt>
cmp    %ebx,%eax
jne    115b <xread+0x2b>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e7e(%rip),%r12
mov    0x2e6f(%rip),%r13
lea    0xe90(%rip),%r15
jmp    1199 <xread+0x69>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cltq
add    %rax,%r14
sub    %rax,%rbx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    %ebx,%eax
je     114c <xread+0x1c>
cmp    $0xffffffff,%eax
jne    1180 <xread+0x50>
mov    (%r12),%rdi
xor    %eax,%eax
cmp    0x0(%r13),%rdi
je     1180 <xread+0x50>
call   1040 <strerror@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <fatal@plt>
mov    $0xffffffff,%eax
jmp    1180 <xread+0x50>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [31 x i8] c"Unable to read from socket: %s\00"
define void @xread(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
%8 = load i32, i32* %4
%9 = load i8*, i8** %5
%10 = load i64, i64* %6
%11 = call i32 @read(i32 %8, i8* %9, i64 %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = load i64, i64* %6
%14 = trunc i64 %13 to i32
%15 = icmp eq i32 %12, %14
br i1 %15, label %16, label %17
16: ; preds = %3
br label %41
17: ; preds = %3
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = call i32 @strerror(i64 %25)
%27 = call i32 @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %29
28: ; preds = %20
store i32 0, i32* %7
br label %29
29: ; preds = %28, %24
br label %30
30: ; preds = %29, %17
br label %31
31: ; preds = %30
%32 = load i32, i32* %4
%33 = load i8*, i8** %5
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i64, i64* %6
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = sub i64 %37, %39
br label %41
41: ; preds = %31, %16
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [31 x i8] c"Unable to read from socket: %s\00"
define void @xread(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @read(i32 %0, i8* %1, i64 %2)
%5 = trunc i64 %2 to i32
%6 = icmp eq i32 %4, %5
br i1 %6, label %27, label %7
7: ; preds = %3, %19
%8 = phi i32 [ %24, %19 ], [ %4, %3 ]
%9 = phi i64 [ %23, %19 ], [ %2, %3 ]
%10 = phi i8* [ %22, %19 ], [ %1, %3 ]
%11 = icmp eq i32 %8, -1
br i1 %11, label %12, label %19
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = tail call i32 @strerror(i64 %13)
%18 = tail call i32 @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %12, %7, %16
%20 = phi i32 [ -1, %16 ], [ %8, %7 ], [ 0, %12 ]
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %10, i64 %21
%23 = sub i64 %9, %21
%24 = tail call i32 @read(i32 %0, i8* %22, i64 %23)
%25 = trunc i64 %23 to i32
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %7
27: ; preds = %19, %3
ret void
}