#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 BUFSIZ;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ die(char *);
char *malloc_or_die(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
char *realloc(char *, int);
char *tracecmd_get_tracing_file(char *);
int /*<<< orphan*/ tracecmd_put_tracing_file(char *);
int /*<<< orphan*/ warning(char *, char *);
char *read_file(char *file, int *psize) {
  char buffer[BUFSIZ];
  char *path;
  char *buf;
  int size = 0;
  int fd;
  int r;
  path = tracecmd_get_tracing_file(file);
  fd = open(path, O_RDONLY);
  tracecmd_put_tracing_file(path);
  if (fd < 0) {
    warning("%s not found, --date ignored", file);
    return NULL;
  }
  do {
    r = read(fd, buffer, BUFSIZ);
    if (r <= 0)
      continue;
    if (size) {
      buf = realloc(buf, size + r + 1);
      if (!buf)
        die("malloc");
    } else
      buf = malloc_or_die(r + 1);
    memcpy(buf + size, buffer, r);
    size += r;
  } while (r);
  buf[size] = '\0';
  if (psize)
    *psize = size;
  return buf;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <tracecmd_get_tracing_file@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <tracecmd_put_tracing_file@plt>
cmpl   $0x0,-0x40(%rbp)
jge    122c <read_file+0x9c>
mov    -0x10(%rbp),%rsi
lea    0xded(%rip),%rdi
call   1040 <warning@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x48(%rbp)
jmp    12fc <read_file+0x16c>
jmp    1231 <read_file+0xa1>
mov    -0x50(%rbp),%rsi
mov    -0x40(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
call   1050 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jg     1258 <read_file+0xc8>
jmp    12c3 <read_file+0x133>
cmpl   $0x0,-0x3c(%rbp)
je     1294 <read_file+0x104>
mov    -0x38(%rbp),%rdi
mov    -0x3c(%rbp),%esi
add    -0x44(%rbp),%esi
add    $0x1,%esi
call   1090 <realloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    128f <read_file+0xff>
lea    0xd93(%rip),%rdi
call   10b0 <die@plt>
jmp    12a3 <read_file+0x113>
mov    -0x44(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc_or_die@plt>
mov    %rax,-0x38(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x38(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
mov    -0x44(%rbp),%edx
call   1070 <memcpy@plt>
mov    -0x44(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    1231 <read_file+0xa1>
mov    -0x38(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x18(%rbp)
je     12ed <read_file+0x15d>
mov    -0x3c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,-0x38(%rbp)
mov    %rdi,%r14
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
call   1060 <tracecmd_get_tracing_file@plt>
mov    %rax,%r15
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1030 <tracecmd_put_tracing_file@plt>
mov    %ebx,-0x2c(%rbp)
test   %ebx,%ebx
js     128a <read_file+0xfa>
xor    %r12d,%r12d
jmp    1228 <read_file+0x98>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%r14),%edi
call   1080 <malloc_or_die@plt>
mov    %rax,%rbx
mov    %r14d,%r15d
movslq %r12d,%rdi
add    %rbx,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
call   1070 <memcpy@plt>
mov    %r15d,%r12d
test   %r14d,%r14d
je     1275 <read_file+0xe5>
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
mov    -0x2c(%rbp),%edi
mov    %r13,%rsi
call   1050 <read@plt>
mov    %eax,%r14d
test   %eax,%eax
jle    1223 <read_file+0x93>
test   %r12d,%r12d
je     1200 <read_file+0x70>
lea    (%r14,%r12,1),%r15d
lea    (%r14,%r12,1),%esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1090 <realloc@plt>
test   %rax,%rax
je     1265 <read_file+0xd5>
mov    %rax,%rbx
jmp    120f <read_file+0x7f>
lea    0xdb1(%rip),%rdi
call   10b0 <die@plt>
xor    %ebx,%ebx
jmp    120f <read_file+0x7f>
movslq %r12d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    -0x38(%rbp),%rax
test   %rax,%rax
je     129b <read_file+0x10b>
mov    %r12d,(%rax)
jmp    129b <read_file+0x10b>
lea    0xd6f(%rip),%rdi
mov    %r14,%rsi
call   1040 <warning@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@BUFSIZ = global i32 0
@O_RDONLY = global i32 0
@.str = [29 x i8] c"%s not found, --date ignored\00"
@.str.1 = [7 x i8] c"malloc\00"
define i8* @read_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%14 = load i32, i32* @BUFSIZ
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
store i32 0, i32* %10
%18 = load i8*, i8** %4
%19 = call i8* @tracecmd_get_tracing_file(i8* %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %11
%23 = load i8*, i8** %8
%24 = call i32 @tracecmd_put_tracing_file(i8* %23)
%25 = load i32, i32* %11
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %2
%28 = load i8*, i8** %4
%29 = call i32 @warning(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %28)
store i8* null, i8** %3
store i32 1, i32* %13
br label %82
30: ; preds = %2
br label %31
31: ; preds = %67, %30
%32 = load i32, i32* %11
%33 = load i32, i32* @BUFSIZ
%34 = call i32 @read(i32 %32, i8* %17, i32 %33)
store i32 %34, i32* %12
%35 = load i32, i32* %12
%36 = icmp sle i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31
br label %67
38: ; preds = %31
%39 = load i32, i32* %10
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %53
41: ; preds = %38
%42 = load i8*, i8** %9
%43 = load i32, i32* %10
%44 = load i32, i32* %12
%45 = add nsw i32 %43, %44
%46 = add nsw i32 %45, 1
%47 = call i8* @realloc(i8* %42, i32 %46)
store i8* %47, i8** %9
%48 = load i8*, i8** %9
%49 = icmp ne i8* %48, null
br i1 %49, label %52, label %50
50: ; preds = %41
%51 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %52
52: ; preds = %50, %41
br label %57
53: ; preds = %38
%54 = load i32, i32* %12
%55 = add nsw i32 %54, 1
%56 = call i8* @malloc_or_die(i32 %55)
store i8* %56, i8** %9
br label %57
57: ; preds = %53, %52
%58 = load i8*, i8** %9
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = load i32, i32* %12
%63 = call i32 @memcpy(i8* %61, i8* %17, i32 %62)
%64 = load i32, i32* %12
%65 = load i32, i32* %10
%66 = add nsw i32 %65, %64
store i32 %66, i32* %10
br label %67
67: ; preds = %57, %37
%68 = load i32, i32* %12
%69 = icmp ne i32 %68, 0
br i1 %69, label %31, label %70
70: ; preds = %67
%71 = load i8*, i8** %9
%72 = load i32, i32* %10
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %71, i64 %73
store i8 0, i8* %74
%75 = load i32*, i32** %5
%76 = icmp ne i32* %75, null
br i1 %76, label %77, label %80
77: ; preds = %70
%78 = load i32, i32* %10
%79 = load i32*, i32** %5
store i32 %78, i32* %79
br label %80
80: ; preds = %77, %70
%81 = load i8*, i8** %9
store i8* %81, i8** %3
store i32 1, i32* %13
br label %82
82: ; preds = %80, %27
%83 = load i8*, i8** %6
%84 = load i8*, i8** %3
ret i8* %84
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@O_RDONLY = global i32 0
@.str = [29 x i8] c"%s not found, --date ignored\00"
@.str.1 = [7 x i8] c"malloc\00"
define i8* @read_file(i8* %0, i32* writeonly %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i8* @tracecmd_get_tracing_file(i8* %0)
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %6, i32 %7)
%9 = tail call i32 @tracecmd_put_tracing_file(i8* %6)
%10 = icmp slt i32 %8, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @warning(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %46
13: ; preds = %2, %37
%14 = phi i32 [ %38, %37 ], [ 0, %2 ]
%15 = phi i8* [ %39, %37 ], [ undef, %2 ]
%16 = load i32, i32* @BUFSIZ
%17 = call i32 @read(i32 %8, i8* %5, i32 %16)
%18 = icmp slt i32 %17, 1
br i1 %18, label %37, label %19
19: ; preds = %13
%20 = icmp eq i32 %14, 0
br i1 %20, label %28, label %21
21: ; preds = %19
%22 = add nsw i32 %17, %14
%23 = add nsw i32 %22, 1
%24 = call i8* @realloc(i8* %15, i32 %23)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %31
28: ; preds = %19
%29 = add nuw nsw i32 %17, 1
%30 = call i8* @malloc_or_die(i32 %29)
br label %31
31: ; preds = %21, %26, %28
%32 = phi i32 [ %22, %21 ], [ %22, %26 ], [ %17, %28 ]
%33 = phi i8* [ %24, %21 ], [ null, %26 ], [ %30, %28 ]
%34 = sext i32 %14 to i64
%35 = getelementptr inbounds i8, i8* %33, i64 %34
%36 = call i32 @memcpy(i8* %35, i8* %5, i32 %17)
br label %37
37: ; preds = %13, %31
%38 = phi i32 [ %14, %13 ], [ %32, %31 ]
%39 = phi i8* [ %15, %13 ], [ %33, %31 ]
%40 = icmp eq i32 %17, 0
br i1 %40, label %41, label %13
41: ; preds = %37
%42 = sext i32 %38 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
store i8 0, i8* %43
%44 = icmp eq i32* %1, null
br i1 %44, label %46, label %45
45: ; preds = %41
store i32 %38, i32* %1
br label %46
46: ; preds = %41, %45, %11
%47 = phi i8* [ null, %11 ], [ %39, %45 ], [ %39, %41 ]
ret i8* %47
}