#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 */

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int open(char const *, int /*<<< orphan*/);
int read(int, char *, size_t);
int read_file(const char *pathname, char *buffer, size_t buffsize) {
  int fd, count;
  fd = open(pathname, O_RDONLY);
  if (fd < 0) {
    return -1;
  }
  count = 0;
  while (count < (int)buffsize) {
    int ret = read(fd, buffer + count, buffsize - count);
    if (ret < 0) {
      if (errno == EINTR) {
        continue;
      }
      if (count == 0) {
        count = -1;
      }
      break;
    }
    if (ret == 0) {
      break;
    }
    count += ret;
  }
  close(fd);
  return count;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    116f <read_file+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1210 <read_file+0xe0>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x20(%rbp),%rcx
cmp    %ecx,%eax
jge    1202 <read_file+0xd2>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
movslq -0x28(%rbp),%rax
sub    %rax,%rdx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11e5 <read_file+0xb5>
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%rax
jne    11cf <read_file+0x9f>
jmp    1176 <read_file+0x46>
cmpl   $0x0,-0x28(%rbp)
jne    11e0 <read_file+0xb0>
movl   $0xffffffff,-0x28(%rbp)
jmp    1202 <read_file+0xd2>
cmpl   $0x0,-0x2c(%rbp)
jne    11f4 <read_file+0xc4>
jmp    1202 <read_file+0xd2>
mov    -0x2c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1176 <read_file+0x46>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     119f <read_file+0x6f>
mov    %eax,%r14d
xor    %ebp,%ebp
test   %ebx,%ebx
jle    11ad <read_file+0x7d>
mov    0x2e75(%rip),%r12
mov    0x2e66(%rip),%r13
jmp    117e <read_file+0x4e>
nopl   0x0(%rax)
mov    (%r12),%rax
cmp    0x0(%r13),%rax
jne    11a6 <read_file+0x76>
cmp    %ebx,%ebp
jge    11ad <read_file+0x7d>
movslq %ebp,%rax
lea    (%r15,%rax,1),%rsi
mov    %rbx,%rdx
sub    %rax,%rdx
mov    %r14d,%edi
call   1040 <read@plt>
test   %eax,%eax
js     1170 <read_file+0x40>
add    %eax,%ebp
test   %eax,%eax
jne    117a <read_file+0x4a>
jmp    11ad <read_file+0x7d>
mov    $0xffffffff,%ebp
jmp    11b5 <read_file+0x85>
cmp    $0x1,%ebp
sbb    %eax,%eax
or     %eax,%ebp
mov    %r14d,%edi
call   1030 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @read_file(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %58
17: ; preds = %3
store i32 0, i32* %9
br label %18
18: ; preds = %50, %40, %17
%19 = load i32, i32* %9
%20 = load i64, i64* %7
%21 = trunc i64 %20 to i32
%22 = icmp slt i32 %19, %21
br i1 %22, label %23, label %54
23: ; preds = %18
%24 = load i32, i32* %8
%25 = load i8*, i8** %6
%26 = load i32, i32* %9
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i64, i64* %7
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = sub i64 %29, %31
%33 = call i32 @read(i32 %24, i8* %28, i64 %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %23
%37 = load i64, i64* @errno
%38 = load i64, i64* @EINTR
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %36
br label %18
41: ; preds = %36
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %41
store i32 -1, i32* %9
br label %45
45: ; preds = %44, %41
br label %54
46: ; preds = %23
%47 = load i32, i32* %10
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %46
br label %54
50: ; preds = %46
%51 = load i32, i32* %10
%52 = load i32, i32* %9
%53 = add nsw i32 %52, %51
store i32 %53, i32* %9
br label %18
54: ; preds = %49, %45, %18
%55 = load i32, i32* %8
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %9
store i32 %57, i32* %4
br label %58
58: ; preds = %54, %16
%59 = load i32, i32* %4
ret i32 %59
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @read_file(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %33, label %7
7: ; preds = %3
%8 = trunc i64 %2 to i32
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %30
10: ; preds = %7, %27
%11 = phi i32 [ %28, %27 ], [ 0, %7 ]
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
%14 = sub i64 %2, %12
%15 = tail call i32 @read(i32 %5, i8* %13, i64 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %10
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %27, label %21
21: ; preds = %17
%22 = icmp eq i32 %11, 0
%23 = select i1 %22, i32 -1, i32 %11
br label %30
24: ; preds = %10
%25 = icmp eq i32 %15, 0
%26 = add nsw i32 %15, %11
br i1 %25, label %30, label %27
27: ; preds = %24, %17
%28 = phi i32 [ %11, %17 ], [ %26, %24 ]
%29 = icmp slt i32 %28, %8
br i1 %29, label %10, label %30
30: ; preds = %27, %24, %7, %21
%31 = phi i32 [ %23, %21 ], [ 0, %7 ], [ %26, %24 ], [ %28, %27 ]
%32 = tail call i32 @close(i32 %5)
br label %33
33: ; preds = %3, %30
%34 = phi i32 [ %31, %30 ], [ -1, %3 ]
ret i32 %34
}