#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 */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
int fs_read(const char *path, unsigned char *output, int size) {
  int file = open(path, O_RDONLY);
  int readed;
  if (file == -1) {
    perror("fs_read: ");
    return -1;
  }
  readed = read(file, output, size);
  if (readed == -1) {
    perror("fs_read: ");
    return -1;
  }
  close(file);
  return readed;
}


x86 assembly (clang, -O0):
<fs_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    118a <fs_read+0x4a>
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <fs_read+0x8c>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11be <fs_read+0x7e>
lea    0xe53(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <fs_read+0x8c>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_read>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    0x2e97(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1181 <fs_read+0x41>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1181 <fs_read+0x41>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
jmp    117a <fs_read+0x3a>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [10 x i8] c"fs_read: \00"
define i32 @fs_read(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %30
17: ; preds = %3
%18 = load i32, i32* %8
%19 = load i8*, i8** %6
%20 = load i32, i32* %7
%21 = call i32 @read(i32 %18, i8* %19, i32 %20)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %30
26: ; preds = %17
%27 = load i32, i32* %8
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %9
store i32 %29, i32* %4
br label %30
30: ; preds = %26, %24, %15
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [10 x i8] c"fs_read: \00"
define i32 @fs_read(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %16
9: ; preds = %3
%10 = tail call i32 @read(i32 %5, i8* %1, i32 %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %16
14: ; preds = %9
%15 = tail call i32 @close(i32 %5)
br label %16
16: ; preds = %14, %12, %7
%17 = phi i32 [ -1, %7 ], [ -1, %12 ], [ %10, %14 ]
ret i32 %17
}