#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*/ uint8_t;
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char const *, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *realloc(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcmp(char *, char const *);
int readallocfile(uint8_t **dat, const char *filename) {
  int ret, fd, done;
  struct stat st;
  if (!filename || !strcmp("-", filename)) {
    fd = fileno(stdin);
  } else {
    fd = open(filename, O_RDONLY);
    if (fd < 0)
      error(1, errno, "open %s", filename);
  }
  fstat(fd, &st);
  if (st.st_size) {
    done = st.st_size;
    *dat = realloc(*dat, done);
    if (!*dat)
      error(1, errno, "realloc %u", done);
    ret = read(fd, *dat, done);
    if (ret < 0)
      error(1, errno, "read %s", filename);
  } else {
    int size;
    done = size = 0;
    do {
      size += 1024;
      *dat = realloc(*dat, size);
      ret = read(fd, (*dat) + done, 1024);
      if (ret < 0)
        error(1, errno, "read %s", filename);
      done += ret;
    } while (ret == 1024);
    ret = done;
  }
  return ret;
}


x86 assembly (clang, -O0):
<readallocfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a4 <readallocfile+0x34>
mov    -0x10(%rbp),%rsi
lea    0xe6a(%rip),%rdi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11ba <readallocfile+0x4a>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,-0x18(%rbp)
jmp    11fe <readallocfile+0x8e>
mov    -0x10(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f9 <readallocfile+0x89>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edi
lea    0xe10(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
jmp    11fe <readallocfile+0x8e>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1090 <fstat@plt>
cmpl   $0x0,-0x20(%rbp)
je     12a4 <readallocfile+0x134>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x1c(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1260 <readallocfile+0xf0>
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%ecx
mov    $0x1,%edi
lea    0xdb1(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    129f <readallocfile+0x12f>
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edi
lea    0xd7d(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
jmp    133e <readallocfile+0x1ce>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x400,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1322 <readallocfile+0x1b2>
mov    0x2cc7(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edi
lea    0xcfa(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x14(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x400,-0x14(%rbp)
je     12b2 <readallocfile+0x142>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readallocfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
test   %rsi,%rsi
je     11d2 <readallocfile+0x62>
lea    0xe73(%rip),%rdi
mov    %r14,%rsi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11d2 <readallocfile+0x62>
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11e3 <readallocfile+0x73>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    0xe41(%rip),%rdx
mov    $0x1,%edi
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <error@plt>
jmp    11e3 <readallocfile+0x73>
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %r15d,%edi
call   1090 <fstat@plt>
mov    (%rsp),%ebp
test   %ebp,%ebp
je     1264 <readallocfile+0xf4>
mov    (%r12),%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,(%r12)
test   %rax,%rax
jne    122b <readallocfile+0xbb>
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
lea    0xdf1(%rip),%rdx
mov    $0x1,%edi
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <error@plt>
mov    (%r12),%rax
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    12ce <readallocfile+0x15e>
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
lea    0xdc2(%rip),%rdx
mov    $0x1,%edi
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <error@plt>
jmp    12ce <readallocfile+0x15e>
xor    %r13d,%r13d
mov    $0x400,%ebx
nopl   0x0(%rax)
mov    (%r12),%rdi
mov    %ebx,%esi
call   1060 <realloc@plt>
mov    %rax,(%r12)
movslq %r13d,%rcx
lea    (%rax,%rcx,4),%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1030 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12ac <readallocfile+0x13c>
add    %ebp,%r13d
add    $0x400,%ebx
cmp    $0x400,%ebp
je     1270 <readallocfile+0x100>
jmp    12ce <readallocfile+0x15e>
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
lea    0xd59(%rip),%rdx
mov    $0x1,%edi
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <error@plt>
add    %ebp,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"-\00"
@stdin = global i32 0
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [8 x i8] c"open %s\00"
@.str.2 = [11 x i8] c"realloc %u\00"
@.str.3 = [8 x i8] c"read %s\00"
define i32 @readallocfile(i32** %0, i8* %1) {
%3 = alloca i32**
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
store i32** %0, i32*** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i32 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12, %2
%17 = load i32, i32* @stdin
%18 = call i32 @fileno(i32 %17)
store i32 %18, i32* %6
br label %30
19: ; preds = %12
%20 = load i8*, i8** %4
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = load i8*, i8** %4
%28 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %26, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %25, %19
br label %30
30: ; preds = %29, %16
%31 = load i32, i32* %6
%32 = call i32 @fstat(i32 %31, %struct.stat* %8)
%33 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%34 = load i32, i32* %33
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %64
36: ; preds = %30
%37 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%38 = load i32, i32* %37
store i32 %38, i32* %7
%39 = load i32**, i32*** %3
%40 = load i32*, i32** %39
%41 = load i32, i32* %7
%42 = call i32* @realloc(i32* %40, i32 %41)
%43 = load i32**, i32*** %3
store i32* %42, i32** %43
%44 = load i32**, i32*** %3
%45 = load i32*, i32** %44
%46 = icmp ne i32* %45, null
br i1 %46, label %51, label %47
47: ; preds = %36
%48 = load i32, i32* @errno
%49 = load i32, i32* %7
%50 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %48, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %47, %36
%52 = load i32, i32* %6
%53 = load i32**, i32*** %3
%54 = load i32*, i32** %53
%55 = load i32, i32* %7
%56 = call i32 @read(i32 %52, i32* %54, i32 %55)
store i32 %56, i32* %5
%57 = load i32, i32* %5
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %51
%60 = load i32, i32* @errno
%61 = load i8*, i8** %4
%62 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %60, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %59, %51
br label %95
64: ; preds = %30
store i32 0, i32* %9
store i32 0, i32* %7
br label %65
65: ; preds = %90, %64
%66 = load i32, i32* %9
%67 = add nsw i32 %66, 1024
store i32 %67, i32* %9
%68 = load i32**, i32*** %3
%69 = load i32*, i32** %68
%70 = load i32, i32* %9
%71 = call i32* @realloc(i32* %69, i32 %70)
%72 = load i32**, i32*** %3
store i32* %71, i32** %72
%73 = load i32, i32* %6
%74 = load i32**, i32*** %3
%75 = load i32*, i32** %74
%76 = load i32, i32* %7
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i32, i32* %75, i64 %77
%79 = call i32 @read(i32 %73, i32* %78, i32 1024)
store i32 %79, i32* %5
%80 = load i32, i32* %5
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %86
82: ; preds = %65
%83 = load i32, i32* @errno
%84 = load i8*, i8** %4
%85 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %83, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %84)
br label %86
86: ; preds = %82, %65
%87 = load i32, i32* %5
%88 = load i32, i32* %7
%89 = add nsw i32 %88, %87
store i32 %89, i32* %7
br label %90
90: ; preds = %86
%91 = load i32, i32* %5
%92 = icmp eq i32 %91, 1024
br i1 %92, label %65, label %93
93: ; preds = %90
%94 = load i32, i32* %7
store i32 %94, i32* %5
br label %95
95: ; preds = %93, %63
%96 = load i32, i32* %5
ret i32 %96
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"-\00"
@stdin = global i32 0
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [8 x i8] c"open %s\00"
@.str.2 = [11 x i8] c"realloc %u\00"
@.str.3 = [8 x i8] c"read %s\00"
define i32 @readallocfile(i32** nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i8* %1, null
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @strcmp(i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6, %2
%10 = load i32, i32* @stdin
%11 = tail call i32 @fileno(i32 %10)
br label %19
12: ; preds = %6
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %1, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %19
19: ; preds = %12, %16, %9
%20 = phi i32 [ %14, %16 ], [ %14, %12 ], [ %11, %9 ]
%21 = call i32 @fstat(i32 %20, %struct.stat* %3)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 0
br i1 %24, label %40, label %25
25: ; preds = %19
%26 = load i32*, i32** %0
%27 = tail call i32* @realloc(i32* %26, i32 %23)
store i32* %27, i32** %0
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @errno
%31 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %30, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%32 = load i32*, i32** %0
br label %33
33: ; preds = %29, %25
%34 = phi i32* [ %32, %29 ], [ %27, %25 ]
%35 = tail call i32 @read(i32 %20, i32* %34, i32 %23)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %57
37: ; preds = %33
%38 = load i32, i32* @errno
%39 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %38, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %57
40: ; preds = %19, %54
%41 = phi i32 [ %55, %54 ], [ 0, %19 ]
%42 = phi i32 [ %43, %54 ], [ 0, %19 ]
%43 = add nuw nsw i32 %42, 1024
%44 = load i32*, i32** %0
%45 = tail call i32* @realloc(i32* %44, i32 %43)
store i32* %45, i32** %0
%46 = sext i32 %41 to i64
%47 = getelementptr inbounds i32, i32* %45, i64 %46
%48 = tail call i32 @read(i32 %20, i32* %47, i32 1024)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %40
%51 = load i32, i32* @errno
%52 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %51, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%53 = add nsw i32 %48, %41
br label %57
54: ; preds = %40
%55 = add nsw i32 %48, %41
%56 = icmp eq i32 %48, 1024
br i1 %56, label %40, label %57
57: ; preds = %54, %50, %33, %37
%58 = phi i32 [ %35, %37 ], [ %35, %33 ], [ %53, %50 ], [ %55, %54 ]
ret i32 %58
}