#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 stat {
  scalar_t__ st_size;
};
struct buffer {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *buffer_cstr(struct buffer *);
int /*<<< orphan*/ buffer_end(struct buffer *);
struct buffer *buffer_fromfile(int /*<<< orphan*/ *, int);
int buffer_insertfile(struct buffer **, int, int /*<<< orphan*/ *);
size_t buffer_length(struct buffer *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int test_fromfile(void) {
  const char fn[] = "buffertest.c";
  struct stat st;
  struct buffer *b;
  FILE *fh;
  int ret = -1, rc;
  fh = fopen(fn, "r");
  if (fh == NULL) {
    perror("fopen");
    return -1;
  }
  b = buffer_fromfile(fh, 32);
  fclose(fh);
  if (b == NULL)
    return -1;
  if (stat(fn, &st) < 0)
    goto error;
  if ((size_t)st.st_size + 1 != buffer_length(b)) {
    fprintf(stderr, "Error: size mismatch\n");
    goto error;
  }
  fh = fopen(fn, "r");
  if (fh == NULL) {
    perror("fopen");
    goto error;
  }
  rc = buffer_insertfile(&b, 32, fh);
  fclose(fh);
  if (rc < 0)
    goto error;
  if ((size_t)st.st_size * 2 + 1 != buffer_length(b)) {
    fprintf(stderr, "Error: size mismatch #2.\n");
    goto error;
  }
  printf("<%s>\n", buffer_cstr(b));
  ret = 1;
error:
  buffer_end(b);
  return ret;
}


x86 assembly (clang, -O0):
<test_fromfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0xe41(%rip),%rax
mov    %rax,-0x11(%rbp)
mov    0xe3f(%rip),%eax
mov    %eax,-0x9(%rbp)
mov    0xe3a(%rip),%al
mov    %al,-0x5(%rbp)
movl   $0xffffffff,-0x34(%rbp)
lea    -0x11(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1213 <test_fromfile+0x63>
lea    0xe0d(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136e <test_fromfile+0x1be>
mov    -0x30(%rbp),%rdi
mov    $0x20,%esi
call   10d0 <buffer_fromfile@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
jne    1245 <test_fromfile+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    136e <test_fromfile+0x1be>
lea    -0x11(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   10a0 <stat@plt>
cmp    $0x0,%rax
jge    1261 <test_fromfile+0xb1>
jmp    135f <test_fromfile+0x1af>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <buffer_length@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
je     12a0 <test_fromfile+0xf0>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
call   1090 <fprintf@plt>
jmp    135f <test_fromfile+0x1af>
lea    -0x11(%rbp),%rdi
lea    0xd62(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12d0 <test_fromfile+0x120>
lea    0xd49(%rip),%rdi
call   10c0 <perror@plt>
jmp    135f <test_fromfile+0x1af>
mov    -0x30(%rbp),%rdx
lea    -0x28(%rbp),%rdi
mov    $0x20,%esi
call   1040 <buffer_insertfile@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
cmpl   $0x0,-0x38(%rbp)
jge    12fd <test_fromfile+0x14d>
jmp    135f <test_fromfile+0x1af>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <buffer_length@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
cmp    %rcx,%rax
je     1340 <test_fromfile+0x190>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
lea    0xcf5(%rip),%rsi
call   1090 <fprintf@plt>
jmp    135f <test_fromfile+0x1af>
mov    -0x28(%rbp),%rdi
call   1030 <buffer_cstr@plt>
mov    %rax,%rsi
lea    0xcf2(%rip),%rdi
call   1080 <printf@plt>
movl   $0x1,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <buffer_end@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fromfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
movabs $0x632e7473657472,%rax
mov    %rax,0x15(%rsp)
movabs $0x6574726566667562,%rax
72 74 65
mov    %rax,0x10(%rsp)
lea    0xe2e(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12ed <test_fromfile+0x13d>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x20,%esi
call   10d0 <buffer_fromfile@plt>
mov    %rax,%rbx
mov    %rax,0x8(%rsp)
mov    %rbp,%rdi
call   1050 <fclose@plt>
test   %rbx,%rbx
je     12d3 <test_fromfile+0x123>
lea    0x10(%rsp),%rdi
lea    0x20(%rsp),%rsi
call   10a0 <stat@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
js     1316 <test_fromfile+0x166>
mov    0x20(%rsp),%rbp
add    $0x1,%rbp
mov    %rbx,%rdi
call   1070 <buffer_length@plt>
cmp    %rax,%rbp
jne    12db <test_fromfile+0x12b>
lea    0xdb3(%rip),%rsi
lea    0x10(%rsp),%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     132e <test_fromfile+0x17e>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
mov    $0x20,%esi
mov    %rax,%rdx
call   1040 <buffer_insertfile@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %r14d,%r14d
js     1316 <test_fromfile+0x166>
mov    0x20(%rsp),%rax
lea    (%rax,%rax,1),%rbx
add    $0x1,%rbx
mov    0x8(%rsp),%rdi
call   1070 <buffer_length@plt>
cmp    %rax,%rbx
jne    1301 <test_fromfile+0x151>
mov    0x8(%rsp),%rdi
call   1030 <buffer_cstr@plt>
lea    0xd82(%rip),%rdi
mov    %rax,%rsi
call   1080 <printf@plt>
mov    $0x1,%r15d
jmp    1316 <test_fromfile+0x166>
mov    $0xffffffff,%r15d
jmp    1320 <test_fromfile+0x170>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
lea    0xd2a(%rip),%rsi
jmp    1311 <test_fromfile+0x161>
lea    0xd1b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%r15d
jmp    1320 <test_fromfile+0x170>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xd1a(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1060 <buffer_end@plt>
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcda(%rip),%rdi
call   10c0 <perror@plt>
jmp    1316 <test_fromfile+0x166>

LLVM IR (-O0):
%struct.stat = type { i64 }
%struct.buffer = type { i32 }
@__const.test_fromfile.fn = [13 x i8] c"buffertest.c\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"Error: size mismatch\0A\00"
@.str.3 = [26 x i8] c"Error: size mismatch.\0A\00"
@.str.4 = [6 x i8] c"<%s>\0A\00"
define i32 @test_fromfile() {
%1 = alloca i32
%2 = alloca [13 x i8]
%3 = alloca %struct.stat
%4 = alloca %struct.buffer*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = bitcast [13 x i8]* %2 to i8*
store i32 -1, i32* %6
%9 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %72
15: ; preds = %0
%16 = load i32*, i32** %5
%17 = call %struct.buffer* @buffer_fromfile(i32* %16, i32 32)
store %struct.buffer* %17, %struct.buffer** %4
%18 = load i32*, i32** %5
%19 = call i32 @fclose(i32* %18)
%20 = load %struct.buffer*, %struct.buffer** %4
%21 = icmp eq %struct.buffer* %20, null
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 -1, i32* %1
br label %72
23: ; preds = %15
%24 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%25 = call i64 @stat(i8* %24, %struct.stat* %3)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
br label %68
28: ; preds = %23
%29 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%30 = load i64, i64* %29
%31 = add i64 %30, 1
%32 = load %struct.buffer*, %struct.buffer** %4
%33 = call i64 @buffer_length(%struct.buffer* %32)
%34 = icmp ne i64 %31, %33
br i1 %34, label %35, label %38
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %68
38: ; preds = %28
%39 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %40, i32** %5
%41 = load i32*, i32** %5
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %68
45: ; preds = %38
%46 = load i32*, i32** %5
%47 = call i32 @buffer_insertfile(%struct.buffer** %4, i32 32, i32* %46)
store i32 %47, i32* %7
%48 = load i32*, i32** %5
%49 = call i32 @fclose(i32* %48)
%50 = load i32, i32* %7
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %45
br label %68
53: ; preds = %45
%54 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%55 = load i64, i64* %54
%56 = mul i64 %55, 2
%57 = add i64 %56, 1
%58 = load %struct.buffer*, %struct.buffer** %4
%59 = call i64 @buffer_length(%struct.buffer* %58)
%60 = icmp ne i64 %57, %59
br i1 %60, label %61, label %64
61: ; preds = %53
%62 = load i32, i32* @stderr
%63 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %68
64: ; preds = %53
%65 = load %struct.buffer*, %struct.buffer** %4
%66 = call i8* @buffer_cstr(%struct.buffer* %65)
%67 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %66)
store i32 1, i32* %6
br label %68
68: ; preds = %64, %61, %52, %43, %35, %27
%69 = load %struct.buffer*, %struct.buffer** %4
%70 = call i32 @buffer_end(%struct.buffer* %69)
%71 = load i32, i32* %6
store i32 %71, i32* %1
br label %72
72: ; preds = %68, %22, %13
%73 = load i32, i32* %1
ret i32 %73
}

LLVM IR (-O2):
%struct.stat = type { i64 }
%struct.buffer = type { i32 }
@__const.test_fromfile.fn = [13 x i8] c"buffertest.c\00"
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"fopen\00"
@stderr = global i32 0
@.str.2 = [22 x i8] c"Error: size mismatch\0A\00"
@.str.3 = [26 x i8] c"Error: size mismatch.\0A\00"
@.str.4 = [6 x i8] c"<%s>\0A\00"
define i32 @test_fromfile() {
%1 = alloca [13 x i8]
%2 = alloca %struct.stat
%3 = alloca %struct.buffer*
%4 = getelementptr inbounds [13 x i8], [13 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.stat* %2 to i8*
%6 = bitcast %struct.buffer** %3 to i8*
%7 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %54
11: ; preds = %0
%12 = tail call %struct.buffer* @buffer_fromfile(i32* %7, i32 32)
store %struct.buffer* %12, %struct.buffer** %3
%13 = tail call i32 @fclose(i32* %7)
%14 = icmp eq %struct.buffer* %12, null
br i1 %14, label %54, label %15
15: ; preds = %11
%16 = call i64 @stat(i8* %4, %struct.stat* %2)
%17 = icmp slt i64 %16, 0
br i1 %17, label %50, label %18
18: ; preds = %15
%19 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%20 = load i64, i64* %19
%21 = add i64 %20, 1
%22 = call i64 @buffer_length(%struct.buffer* %12)
%23 = icmp eq i64 %21, %22
br i1 %23, label %27, label %24
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %50
27: ; preds = %18
%28 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %50
32: ; preds = %27
%33 = call i32 @buffer_insertfile(%struct.buffer** %3, i32 32, i32* %28)
%34 = call i32 @fclose(i32* %28)
%35 = icmp slt i32 %33, 0
br i1 %35, label %50, label %36
36: ; preds = %32
%37 = load i64, i64* %19
%38 = shl i64 %37, 1
%39 = or i64 %38, 1
%40 = load %struct.buffer*, %struct.buffer** %3
%41 = call i64 @buffer_length(%struct.buffer* %40)
%42 = icmp eq i64 %39, %41
br i1 %42, label %46, label %43
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %50
46: ; preds = %36
%47 = load %struct.buffer*, %struct.buffer** %3
%48 = call i8* @buffer_cstr(%struct.buffer* %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %32, %15, %46, %43, %30, %24
%51 = phi i32 [ -1, %15 ], [ -1, %24 ], [ -1, %30 ], [ -1, %32 ], [ -1, %43 ], [ 1, %46 ]
%52 = load %struct.buffer*, %struct.buffer** %3
%53 = call i32 @buffer_end(%struct.buffer* %52)
br label %54
54: ; preds = %11, %50, %9
%55 = phi i32 [ -1, %9 ], [ %51, %50 ], [ -1, %11 ]
ret i32 %55
}