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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
int /*<<< orphan*/ err(int, int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
size_t read(int, char *, size_t);
void read_exact(int fd, void *buf, size_t count) {
  ssize_t done;
  char *_buf = buf;
  while (count > 0) {
    done = read(fd, _buf, count);
    if (done < 0) {
      if ((errno == EAGAIN) || (errno == EINTR))
        continue;
      err(1, NULL);
    }
    if (done == 0) {
      memset(_buf, 0, count);
      done = count;
    }
    count -= done;
    _buf += done;
  }
}


x86 assembly (clang, -O0):
<read_exact>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11fe <read_exact+0xce>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    11bc <read_exact+0x8c>
mov    0x2e64(%rip),%rax
mov    (%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
je     11a9 <read_exact+0x79>
mov    0x2e4a(%rip),%rax
mov    (%rax),%rax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%rax
jne    11ae <read_exact+0x7e>
jmp    114b <read_exact+0x1b>
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <err@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11de <read_exact+0xae>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    114b <read_exact+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_exact>:
push   %rbp
push   %r14
push   %rbx
test   %rdx,%rdx
je     116c <read_exact+0x4c>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
jmp    1148 <read_exact+0x28>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    %rax,%r14
sub    %rax,%rbx
je     116c <read_exact+0x4c>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <read@plt>
test   %rax,%rax
jne    1140 <read_exact+0x20>
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1030 <memset@plt>
mov    %rbx,%rax
jmp    1140 <read_exact+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define void @read_exact(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %5
store i8* %9, i8** %8
br label %10
10: ; preds = %39, %28, %3
%11 = load i64, i64* %6
%12 = icmp ugt i64 %11, 0
br i1 %12, label %13, label %46
13: ; preds = %10
%14 = load i32, i32* %4
%15 = load i8*, i8** %8
%16 = load i64, i64* %6
%17 = call i64 @read(i32 %14, i8* %15, i64 %16)
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = icmp ult i64 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %13
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp eq i64 %21, %22
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %24, %20
br label %10
29: ; preds = %24
%30 = call i32 @err(i32 1, i32* null)
br label %31
31: ; preds = %29, %13
%32 = load i64, i64* %7
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i8*, i8** %8
%36 = load i64, i64* %6
%37 = call i32 @memset(i8* %35, i32 0, i64 %36)
%38 = load i64, i64* %6
store i64 %38, i64* %7
br label %39
39: ; preds = %34, %31
%40 = load i64, i64* %7
%41 = load i64, i64* %6
%42 = sub i64 %41, %40
store i64 %42, i64* %6
%43 = load i64, i64* %7
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i64 %43
store i8* %45, i8** %8
br label %10
46: ; preds = %10
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EINTR = global i64 0
define void @read_exact(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %17, label %5
5: ; preds = %3, %12
%6 = phi i8* [ %15, %12 ], [ %1, %3 ]
%7 = phi i64 [ %14, %12 ], [ %2, %3 ]
%8 = tail call i64 @read(i32 %0, i8* %6, i64 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @memset(i8* %6, i32 0, i64 %7)
br label %12
12: ; preds = %10, %5
%13 = phi i64 [ %7, %10 ], [ %8, %5 ]
%14 = sub i64 %7, %13
%15 = getelementptr inbounds i8, i8* %6, i64 %13
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %5
17: ; preds = %12, %3
ret void
}