#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__ ENOMEM;
scalar_t__ errno;
int read_in_full(int, char *, size_t);
scalar_t__ xread(int, char *, int);
ssize_t read_file(int fd, char *buf, size_t max_size) {
  ssize_t ret;
  char dummy;
  errno = 0;
  ret = read_in_full(fd, buf, max_size);
  if (xread(fd, &dummy, 1) == 0)
    return ret;
  errno = ENOMEM;
  return -1;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e9e(%rip),%rax
movq   $0x0,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read_in_full@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <xread@plt>
cmp    $0x0,%rax
jne    117a <read_file+0x5a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1195 <read_file+0x75>
mov    0x2e5f(%rip),%rax
mov    (%rax),%rcx
mov    0x2e4d(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    0x2ea7(%rip),%r14
movq   $0x0,(%r14)
call   1040 <read_in_full@plt>
mov    %eax,%ebx
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <xread@plt>
test   %rax,%rax
je     1167 <read_file+0x47>
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ENOMEM = global i64 0
define i32 @read_file(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i64 0, i64* @errno
%10 = load i32, i32* %5
%11 = load i8*, i8** %6
%12 = load i64, i64* %7
%13 = call i32 @read_in_full(i32 %10, i8* %11, i64 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %5
%15 = call i64 @xread(i32 %14, i8* %9, i32 1)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* %8
store i32 %18, i32* %4
br label %21
19: ; preds = %3
%20 = load i64, i64* @ENOMEM
store i64 %20, i64* @errno
store i32 -1, i32* %4
br label %21
21: ; preds = %19, %17
%22 = load i32, i32* %4
ret i32 %22
}

LLVM IR (-O2):
@errno = global i64 0
@ENOMEM = global i64 0
define i32 @read_file(i32 %0, i8* %1, i64 %2) {
%4 = alloca i8
store i64 0, i64* @errno
%5 = tail call i32 @read_in_full(i32 %0, i8* %1, i64 %2)
%6 = call i64 @xread(i32 %0, i8* %4, i32 1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = load i64, i64* @ENOMEM
store i64 %9, i64* @errno
br label %10
10: ; preds = %3, %8
%11 = phi i32 [ -1, %8 ], [ %5, %3 ]
ret i32 %11
}