#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 */
struct sstrbuf {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
int sstrbuf_read(struct sstrbuf *, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ stderr;
ssize_t sstrbuf_read_file(struct sstrbuf *sstr, const char *path, size_t hint) {
  int len;
  FILE *f = fopen(path, "r");
  if (!f) {
    perror("fopen");
    return -1;
  }
  len = sstrbuf_read(sstr, f, hint);
  fclose(f);
  if (len < 0) {
    fprintf(stderr, "sstrbuf_read failed, returned %d!\n", len);
    return -1;
  }
  return len;
}


x86 assembly (clang, -O0):
<sstrbuf_read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119b <sstrbuf_read_file+0x4b>
lea    0xe78(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <sstrbuf_read_file+0x9c>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <sstrbuf_read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x24(%rbp)
jge    11e6 <sstrbuf_read_file+0x96>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
lea    0xe33(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <sstrbuf_read_file+0x9c>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sstrbuf_read_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rax
mov    %rdi,%rbp
lea    0xe9c(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b3 <sstrbuf_read_file+0x63>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
call   1040 <sstrbuf_read@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %ebp,%ebp
jns    11ac <sstrbuf_read_file+0x5c>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
mov    %ebp,%edx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe48(%rip),%rdi
call   1070 <perror@plt>
jmp    11a7 <sstrbuf_read_file+0x57>

LLVM IR (-O0):
%struct.sstrbuf = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"sstrbuf_read failed, returned %d!\0A\00"
define i32 @sstrbuf_read_file(%struct.sstrbuf* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.sstrbuf*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32*
store %struct.sstrbuf* %0, %struct.sstrbuf** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = icmp ne i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %31
16: ; preds = %3
%17 = load %struct.sstrbuf*, %struct.sstrbuf** %5
%18 = load i32*, i32** %9
%19 = load i64, i64* %7
%20 = call i32 @sstrbuf_read(%struct.sstrbuf* %17, i32* %18, i64 %19)
store i32 %20, i32* %8
%21 = load i32*, i32** %9
%22 = call i32 @fclose(i32* %21)
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = load i32, i32* @stderr
%27 = load i32, i32* %8
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %27)
store i32 -1, i32* %4
br label %31
29: ; preds = %16
%30 = load i32, i32* %8
store i32 %30, i32* %4
br label %31
31: ; preds = %29, %25, %14
%32 = load i32, i32* %4
ret i32 %32
}

LLVM IR (-O2):
%struct.sstrbuf = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.2 = [35 x i8] c"sstrbuf_read failed, returned %d!\0A\00"
define i32 @sstrbuf_read_file(%struct.sstrbuf* %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %15
8: ; preds = %3
%9 = tail call i32 @sstrbuf_read(%struct.sstrbuf* %0, i32* %4, i64 %2)
%10 = tail call i32 @fclose(i32* %4)
%11 = icmp slt i32 %9, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %9)
br label %15
15: ; preds = %8, %12, %6
%16 = phi i32 [ -1, %12 ], [ -1, %6 ], [ %9, %8 ]
ret i32 %16
}