#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fread(char *, int, size_t, int /*<<< orphan*/ *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char *);
void *realloc(char *, size_t);
int /*<<< orphan*/ stderr;
int in_source(FILE *fin, char **buf, int *buf_len) {
  char *stdin_buf;
  size_t stdin_buflen = 4096, stdin_len = 0;
  stdin_buf = malloc(stdin_buflen);
  if (!stdin_buf) {
    perror("Cannot get memory");
    return 1;
  }
  int tmp_len = 0;
  while ((tmp_len = fread(&stdin_buf[stdin_len], 1, stdin_buflen - stdin_len,
                          fin)) > 0) {
    stdin_len += tmp_len;
    if (stdin_len >= stdin_buflen) {
      void *np = realloc(stdin_buf, stdin_buflen * 2);
      if (np) {
        stdin_buf = np;
        stdin_buflen *= 2;
      }
    }
  }
  if (stdin_len < 1) {
    fprintf(stderr, "No data in file. Quitting.\n");
    return 1;
  }
  *buf = stdin_buf;
  *buf_len = stdin_len;
  fclose(fin);
  return 0;
}


x86 assembly (clang, -O0):
<in_source>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x1000,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b4 <in_source+0x54>
lea    0xe5d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1290 <in_source+0x130>
movl   $0x0,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
sub    -0x38(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jle    123d <in_source+0xdd>
movslq -0x3c(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jb     1238 <in_source+0xd8>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
shl    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1233 <in_source+0xd3>
mov    -0x48(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1238 <in_source+0xd8>
jmp    11bb <in_source+0x5b>
cmpq   $0x1,-0x38(%rbp)
jae    1269 <in_source+0x109>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1290 <in_source+0x130>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<in_source>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r13
mov    $0x1000,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     125e <in_source+0xfe>
mov    %rax,%rbp
mov    %rbx,0x8(%rsp)
mov    %r14,0x10(%rsp)
mov    $0x1,%r12d
mov    $0x1000,%r14d
mov    $0x1000,%edx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r13,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1237 <in_source+0xd7>
xor    %r15d,%r15d
jmp    11eb <in_source+0x8b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    (%r15,%rbp,1),%rdi
mov    %r14,%rdx
sub    %r15,%rdx
mov    $0x1,%esi
mov    %r13,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1215 <in_source+0xb5>
mov    %r15,%rcx
mov    %eax,%r15d
add    %rcx,%r15
cmp    %r14,%r15
jb     11d0 <in_source+0x70>
lea    (%r14,%r14,1),%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1070 <realloc@plt>
test   %rax,%rax
cmovne %rax,%rbp
cmovne %rbx,%r14
jmp    11d0 <in_source+0x70>
test   %r15,%r15
je     1237 <in_source+0xd7>
mov    0x8(%rsp),%rax
mov    %rbp,(%rax)
mov    0x10(%rsp),%rax
mov    %r15d,(%rax)
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %r12d,%r12d
jmp    124c <in_source+0xec>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1050 <fprintf@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%r12d
jmp    124c <in_source+0xec>

LLVM IR (-O0):
@.str = [18 x i8] c"Cannot get memory\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"No data in file. Quitting.\0A\00"
define i32 @in_source(i32* %0, i8** %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i8*
store i32* %0, i32** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i64 4096, i64* %9
store i64 0, i64* %10
%13 = load i64, i64* %9
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %8
%15 = load i8*, i8** %8
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %65
19: ; preds = %3
store i32 0, i32* %11
br label %20
20: ; preds = %50, %19
%21 = load i8*, i8** %8
%22 = load i64, i64* %10
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i64, i64* %9
%25 = load i64, i64* %10
%26 = sub i64 %24, %25
%27 = load i32*, i32** %5
%28 = call i32 @fread(i8* %23, i32 1, i64 %26, i32* %27)
store i32 %28, i32* %11
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %51
30: ; preds = %20
%31 = load i32, i32* %11
%32 = sext i32 %31 to i64
%33 = load i64, i64* %10
%34 = add i64 %33, %32
store i64 %34, i64* %10
%35 = load i64, i64* %10
%36 = load i64, i64* %9
%37 = icmp uge i64 %35, %36
br i1 %37, label %38, label %50
38: ; preds = %30
%39 = load i8*, i8** %8
%40 = load i64, i64* %9
%41 = mul i64 %40, 2
%42 = call i8* @realloc(i8* %39, i64 %41)
store i8* %42, i8** %12
%43 = load i8*, i8** %12
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %49
45: ; preds = %38
%46 = load i8*, i8** %12
store i8* %46, i8** %8
%47 = load i64, i64* %9
%48 = mul i64 %47, 2
store i64 %48, i64* %9
br label %49
49: ; preds = %45, %38
br label %50
50: ; preds = %49, %30
br label %20
51: ; preds = %20
%52 = load i64, i64* %10
%53 = icmp ult i64 %52, 1
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %65
57: ; preds = %51
%58 = load i8*, i8** %8
%59 = load i8**, i8*** %6
store i8* %58, i8** %59
%60 = load i64, i64* %10
%61 = trunc i64 %60 to i32
%62 = load i32*, i32** %7
store i32 %61, i32* %62
%63 = load i32*, i32** %5
%64 = call i32 @fclose(i32* %63)
store i32 0, i32* %4
br label %65
65: ; preds = %57, %54, %17
%66 = load i32, i32* %4
ret i32 %66
}

LLVM IR (-O2):
@.str = [18 x i8] c"Cannot get memory\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"No data in file. Quitting.\0A\00"
define i32 @in_source(i32* nocapture %0, i8** nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = tail call dereferenceable_or_null(4096) i8* @malloc(i64 4096)
%5 = icmp eq i8* %4, null
br i1 %5, label %9, label %6
6: ; preds = %3
%7 = tail call i32 @fread(i8* %4, i32 1, i64 4096, i32* %0)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %11, label %34
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %40
11: ; preds = %6, %25
%12 = phi i32 [ %30, %25 ], [ %7, %6 ]
%13 = phi i64 [ %17, %25 ], [ 0, %6 ]
%14 = phi i64 [ %27, %25 ], [ 4096, %6 ]
%15 = phi i8* [ %26, %25 ], [ %4, %6 ]
%16 = zext i32 %12 to i64
%17 = add i64 %13, %16
%18 = icmp ult i64 %17, %14
br i1 %18, label %25, label %19
19: ; preds = %11
%20 = shl i64 %14, 1
%21 = tail call i8* @realloc(i8* %15, i64 %20)
%22 = icmp eq i8* %21, null
%23 = select i1 %22, i8* %15, i8* %21
%24 = select i1 %22, i64 %14, i64 %20
br label %25
25: ; preds = %19, %11
%26 = phi i8* [ %23, %19 ], [ %15, %11 ]
%27 = phi i64 [ %24, %19 ], [ %14, %11 ]
%28 = getelementptr inbounds i8, i8* %26, i64 %17
%29 = sub i64 %27, %17
%30 = tail call i32 @fread(i8* %28, i32 1, i64 %29, i32* %0)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %11, label %32
32: ; preds = %25
%33 = icmp eq i64 %17, 0
br i1 %33, label %34, label %37
34: ; preds = %6, %32
%35 = load i32, i32* @stderr
%36 = tail call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %40
37: ; preds = %32
store i8* %26, i8** %1
%38 = trunc i64 %17 to i32
store i32 %38, i32* %2
%39 = tail call i32 @fclose(i32* %0)
br label %40
40: ; preds = %34, %37, %9
%41 = phi i32 [ 1, %9 ], [ 1, %34 ], [ 0, %37 ]
ret i32 %41
}