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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, scalar_t__, int);
scalar_t__ read(int, void *, size_t);
int read_full(int fd, void *buf, size_t count) {
  ssize_t ret = 0;
  ssize_t total = 0;
  while (count) {
    ret = read(fd, buf, count);
    if (ret < 0) {
      if (errno == EINTR) {
        continue;
      }
      printf("read error errno=%d fd=%d\n", errno, fd);
      return ret;
    }
    count -= ret;
    buf += ret;
    total += ret;
  }
  return total;
}


x86 assembly (clang, -O0):
<read_full>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11dd <read_full+0xbd>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11b1 <read_full+0x91>
mov    0x2e6c(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
jne    118c <read_full+0x6c>
jmp    1143 <read_full+0x23>
mov    0x2e4d(%rip),%rax
mov    (%rax),%rsi
mov    -0x8(%rbp),%edx
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    11e4 <read_full+0xc4>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1143 <read_full+0x23>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_full>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     11a5 <read_full+0x85>
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %edi,%r15d
xor    %eax,%eax
mov    0x2e9e(%rip),%r13
mov    0x2e8f(%rip),%r14
mov    %rax,(%rsp)
nopl   (%rax)
mov    %r15d,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
jns    1180 <read_full+0x60>
mov    0x0(%r13),%rsi
cmp    (%r14),%rsi
je     1150 <read_full+0x30>
jmp    1191 <read_full+0x71>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    %rbp,%r12
mov    (%rsp),%rax
add    %rbp,%rax
sub    %rbp,%rbx
jne    1149 <read_full+0x29>
jmp    11a7 <read_full+0x87>
lea    0xe68(%rip),%rdi
mov    %r15d,%edx
call   1030 <printf@plt>
mov    %rbp,%rax
jmp    11a7 <read_full+0x87>
xor    %eax,%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
@.str = [27 x i8] c"read error errno=%d fd=%d\0A\00"
define i32 @read_full(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* %8
store i64 0, i64* %9
br label %10
10: ; preds = %31, %24, %3
%11 = load i64, i64* %7
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %41
13: ; preds = %10
%14 = load i32, i32* %5
%15 = load i8*, i8** %6
%16 = load i64, i64* %7
%17 = call i64 @read(i32 %14, i8* %15, i64 %16)
store i64 %17, i64* %8
%18 = load i64, i64* %8
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %13
%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
br label %10
25: ; preds = %20
%26 = load i64, i64* @errno
%27 = load i32, i32* %5
%28 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %26, i32 %27)
%29 = load i64, i64* %8
%30 = trunc i64 %29 to i32
store i32 %30, i32* %4
br label %44
31: ; preds = %13
%32 = load i64, i64* %8
%33 = load i64, i64* %7
%34 = sub i64 %33, %32
store i64 %34, i64* %7
%35 = load i64, i64* %8
%36 = load i8*, i8** %6
%37 = getelementptr i8, i8* %36, i64 %35
store i8* %37, i8** %6
%38 = load i64, i64* %8
%39 = load i64, i64* %9
%40 = add nsw i64 %39, %38
store i64 %40, i64* %9
br label %10
41: ; preds = %10
%42 = load i64, i64* %9
%43 = trunc i64 %42 to i32
store i32 %43, i32* %4
br label %44
44: ; preds = %41, %25
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [27 x i8] c"read error errno=%d fd=%d\0A\00"
define i32 @read_full(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %23, label %5
5: ; preds = %3, %18
%6 = phi i64 [ %21, %18 ], [ 0, %3 ]
%7 = phi i64 [ %19, %18 ], [ %2, %3 ]
%8 = phi i8* [ %20, %18 ], [ %1, %3 ]
br label %9
9: ; preds = %5, %12
%10 = tail call i64 @read(i32 %0, i8* %8, i64 %7)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %9, label %16
16: ; preds = %12
%17 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i64 %13, i32 %0)
br label %23
18: ; preds = %9
%19 = sub i64 %7, %10
%20 = getelementptr i8, i8* %8, i64 %10
%21 = add nsw i64 %10, %6
%22 = icmp eq i64 %19, 0
br i1 %22, label %23, label %5
23: ; preds = %18, %3, %16
%24 = phi i64 [ %10, %16 ], [ 0, %3 ], [ %21, %18 ]
%25 = trunc i64 %24 to i32
ret i32 %25
}