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

/* Variables and functions */
scalar_t__ receive(int, int /*<<< orphan*/ *, size_t, size_t *);
int read_exactly(int fd, uint8_t *buf, size_t n) {
  size_t rx;
  while (n > 0) {
    if (receive(fd, buf, n, &rx) != 0)
      return -1;
    n -= rx;
    buf += n;
  }
  return 0;
}


x86 assembly (clang, -O0):
<read_exactly>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    117c <read_exactly+0x6c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1030 <receive@plt>
cmp    $0x0,%rax
je     1158 <read_exactly+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    1183 <read_exactly+0x73>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x2,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1123 <read_exactly+0x13>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_exactly>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %r14d,%r14d
test   %rdx,%rdx
je     116a <read_exactly+0x5a>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r15d
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
call   1030 <receive@plt>
test   %rax,%rax
jne    1164 <read_exactly+0x54>
sub    0x8(%rsp),%rbx
lea    0x0(%rbp,%rbx,4),%rbp
jne    1140 <read_exactly+0x30>
jmp    116a <read_exactly+0x5a>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_exactly(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %5
store i32* %1, i32** %6
store i64 %2, i64* %7
br label %9
9: ; preds = %19, %3
%10 = load i64, i64* %7
%11 = icmp ugt i64 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %9
%13 = load i32, i32* %5
%14 = load i32*, i32** %6
%15 = load i64, i64* %7
%16 = call i64 @receive(i32 %13, i32* %14, i64 %15, i64* %8)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %12
store i32 -1, i32* %4
br label %27
19: ; preds = %12
%20 = load i64, i64* %8
%21 = load i64, i64* %7
%22 = sub i64 %21, %20
store i64 %22, i64* %7
%23 = load i64, i64* %7
%24 = load i32*, i32** %6
%25 = getelementptr inbounds i32, i32* %24, i64 %23
store i32* %25, i32** %6
br label %9
26: ; preds = %9
store i32 0, i32* %4
br label %27
27: ; preds = %26, %18
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
define i32 @read_exactly(i32 %0, i32* %1, i64 %2) {
%4 = alloca i64
%5 = bitcast i64* %4 to i8*
%6 = icmp eq i64 %2, 0
br i1 %6, label %17, label %7
7: ; preds = %3, %12
%8 = phi i64 [ %14, %12 ], [ %2, %3 ]
%9 = phi i32* [ %15, %12 ], [ %1, %3 ]
%10 = call i64 @receive(i32 %0, i32* %9, i64 %8, i64* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %7
%13 = load i64, i64* %4
%14 = sub i64 %8, %13
%15 = getelementptr inbounds i32, i32* %9, i64 %14
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %7
17: ; preds = %7, %12, %3
%18 = phi i32 [ 0, %3 ], [ 0, %12 ], [ -1, %7 ]
ret i32 %18
}