#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 /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
scalar_t__ read(int, int /*<<< orphan*/ *, size_t);
ssize_t full_read(int fd, uint8_t *buf, size_t count) {
  size_t readed = 0;
  while (readed < count) {
    ssize_t n = read(fd, buf + readed, count - readed);
    if (n < 0) {
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        continue;
      }
      return n;
    }
    readed += n;
  }
  return readed;
}


x86 assembly (clang, -O0):
<full_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11be <full_read+0xae>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
sub    -0x28(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    11ad <full_read+0x9d>
mov    0x2e72(%rip),%rax
mov    (%rax),%rax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%rax
je     119b <full_read+0x8b>
mov    0x2e58(%rip),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
cmp    (%rcx),%rax
jne    11a0 <full_read+0x90>
jmp    112b <full_read+0x1b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c6 <full_read+0xb6>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    112b <full_read+0x1b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<full_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1179 <full_read+0x69>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2eaf(%rip),%r12
mov    0x2e90(%rip),%r13
jmp    1148 <full_read+0x38>
nopw   0x0(%rax,%rax,1)
add    %rax,%rbx
cmp    %r14,%rbx
jae    117b <full_read+0x6b>
lea    (%r15,%rbx,4),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <read@plt>
test   %rax,%rax
jns    1140 <full_read+0x30>
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
je     1143 <full_read+0x33>
mov    0x2e69(%rip),%rdx
cmp    (%rdx),%rcx
je     1143 <full_read+0x33>
mov    %rax,%rbx
jmp    117b <full_read+0x6b>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
define i64 @full_read(i32 %0, i32* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %23, label %5
5: ; preds = %3, %20
%6 = phi i64 [ %21, %20 ], [ 0, %3 ]
%7 = getelementptr inbounds i32, i32* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i64 @read(i32 %0, i32* %7, i64 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @EAGAIN
%14 = icmp eq i64 %12, %13
%15 = load i64, i64* @EWOULDBLOCK
%16 = icmp eq i64 %12, %15
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %20, label %23
18: ; preds = %5
%19 = add i64 %9, %6
br label %20
20: ; preds = %11, %18
%21 = phi i64 [ %19, %18 ], [ %6, %11 ]
%22 = icmp ult i64 %21, %2
br i1 %22, label %5, label %23
23: ; preds = %20, %11, %3
%24 = phi i64 [ 0, %3 ], [ %9, %11 ], [ %21, %20 ]
ret i64 %24
}