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

/* Variables and functions */
int buffer_open_input(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ copy_stream_to_buffer(int /*<<< orphan*/ *, char **,
                                         int /*<<< orphan*/ *);
int dup(int);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
char *buffer_load(const char *tag) {
  FILE *stream = 0;
  char *buffer;
  int nfd = -1, fd = -1;
  int save_errno;
  fd = buffer_open_input(tag);
  if (fd < 0)
    goto failure;
  nfd = dup(fd);
  if (nfd < 0)
    goto failure;
  stream = fdopen(nfd, "r");
  if (!stream)
    goto failure;
  if (!copy_stream_to_buffer(stream, &buffer, NULL))
    goto failure;
  fclose(stream);
  return buffer;
failure:
  save_errno = errno;
  if (stream) {
    fclose(stream);
  } else {
    if (nfd >= 0)
      close(nfd);
  }
  errno = save_errno;
  return 0;
}


x86 assembly (clang, -O0):
<buffer_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <buffer_open_input@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    119d <buffer_load+0x3d>
jmp    120f <buffer_load+0xaf>
mov    -0x28(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b7 <buffer_load+0x57>
jmp    120f <buffer_load+0xaf>
mov    -0x24(%rbp),%edi
lea    0xe3f(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11da <buffer_load+0x7a>
jmp    120f <buffer_load+0xaf>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <copy_stream_to_buffer@plt>
cmp    $0x0,%eax
jne    11f9 <buffer_load+0x99>
jmp    120f <buffer_load+0xaf>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125f <buffer_load+0xff>
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1234 <buffer_load+0xd4>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    124b <buffer_load+0xeb>
cmpl   $0x0,-0x24(%rbp)
jl     1246 <buffer_load+0xe6>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
jmp    124b <buffer_load+0xeb>
mov    -0x2c(%rbp),%ecx
mov    0x2d8b(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_load>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
call   1060 <buffer_open_input@plt>
test   %eax,%eax
js     11b6 <buffer_load+0x56>
mov    %eax,%edi
call   1040 <dup@plt>
test   %eax,%eax
js     11b6 <buffer_load+0x56>
mov    %eax,%ebp
lea    0xe7b(%rip),%rsi
mov    %eax,%edi
call   1070 <fdopen@plt>
test   %rax,%rax
je     11d3 <buffer_load+0x73>
mov    %rax,%r14
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
xor    %edx,%edx
call   1080 <copy_stream_to_buffer@plt>
test   %eax,%eax
je     11e5 <buffer_load+0x85>
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rax
jmp    11ca <buffer_load+0x6a>
mov    0x2e23(%rip),%rax
mov    (%rax),%ebx
mov    0x2e1a(%rip),%rax
mov    %ebx,(%rax)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e06(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11bf <buffer_load+0x5f>
mov    0x2df4(%rip),%rax
mov    (%rax),%ebx
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    11bf <buffer_load+0x5f>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i8* @buffer_load(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32* null, i32** %4
store i32 -1, i32* %6
store i32 -1, i32* %7
%9 = load i8*, i8** %3
%10 = call i32 @buffer_open_input(i8* %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
br label %35
14: ; preds = %1
%15 = load i32, i32* %7
%16 = call i32 @dup(i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
br label %35
20: ; preds = %14
%21 = load i32, i32* %6
%22 = call i32* @fdopen(i32 %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %4
%23 = load i32*, i32** %4
%24 = icmp ne i32* %23, null
br i1 %24, label %26, label %25
25: ; preds = %20
br label %35
26: ; preds = %20
%27 = load i32*, i32** %4
%28 = call i32 @copy_stream_to_buffer(i32* %27, i8** %5, i32* null)
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %26
br label %35
31: ; preds = %26
%32 = load i32*, i32** %4
%33 = call i32 @fclose(i32* %32)
%34 = load i8*, i8** %5
store i8* %34, i8** %2
br label %51
35: ; preds = %30, %25, %19, %13
%36 = load i32, i32* @errno
store i32 %36, i32* %8
%37 = load i32*, i32** %4
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32*, i32** %4
%41 = call i32 @fclose(i32* %40)
br label %49
42: ; preds = %35
%43 = load i32, i32* %6
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* %6
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %45, %42
br label %49
49: ; preds = %48, %39
%50 = load i32, i32* %8
store i32 %50, i32* @errno
store i8* null, i8** %2
br label %51
51: ; preds = %49, %31
%52 = load i8*, i8** %2
ret i8* %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@errno = global i32 0
define i8* @buffer_load(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call i32 @buffer_open_input(i8* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %21, label %6
6: ; preds = %1
%7 = tail call i32 @dup(i32 %4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %6
%10 = tail call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %23, label %12
12: ; preds = %9
%13 = call i32 @copy_stream_to_buffer(i32* %10, i8** %2, i32* null)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
%16 = call i32 @fclose(i32* %10)
%17 = load i8*, i8** %2
br label %28
18: ; preds = %12
%19 = load i32, i32* @errno
%20 = call i32 @fclose(i32* %10)
br label %26
21: ; preds = %6, %1
%22 = load i32, i32* @errno
br label %26
23: ; preds = %9
%24 = load i32, i32* @errno
%25 = tail call i32 @close(i32 %7)
br label %26
26: ; preds = %21, %23, %18
%27 = phi i32 [ %24, %23 ], [ %19, %18 ], [ %22, %21 ]
store i32 %27, i32* @errno
br label %28
28: ; preds = %26, %15
%29 = phi i8* [ null, %26 ], [ %17, %15 ]
ret i8* %29
}