#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
size_t get_str_from_path(char **, int /*<<< orphan*/, char *);
size_t get_str_from_stream(char **, int, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *, size_t);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <file>\n", argv[0]);
    return -1;
  }
  FILE *file;
  if ((file = fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "Error while reading %s\n", argv[1]);
    return -2;
  }
  char *text = NULL;
  size_t size;
  size = get_str_from_path(&text, 0, argv[1]);
  printf("\x1b[31;1mTest #1\x1b[0m\nFile contents:\n%s\nSize: %ld\n", text,
         size);
  free(text);
  text = NULL;
  text = (char *)malloc(10);
  size = get_str_from_stream(&text, 10, file);
  printf("\x1b[31;1mTest #2\x1b[0m\nFile contents:\n%s\nSize: %ld\n", text,
         size);
  free(text);
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11c8 <main+0x48>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <main+0x128>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1213 <main+0x93>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe12(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    12a8 <main+0x128>
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
call   10a0 <get_str_from_path@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdeb(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x20(%rbp)
mov    $0xa,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1060 <get_str_from_stream@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xdd2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11b3 <main+0x33>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe5c(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1279 <main+0xf9>
mov    0x8(%rbx),%rdi
lea    0xe54(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     125b <main+0xdb>
mov    %rax,%r14
movq   $0x0,0x8(%rsp)
mov    0x8(%rbx),%rdx
lea    0x8(%rsp),%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
call   10a0 <get_str_from_path@plt>
mov    0x8(%rsp),%rsi
lea    0xe33(%rip),%rdi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
movq   $0x0,0x8(%rsp)
mov    $0xa,%edi
call   1080 <malloc@plt>
mov    %rax,0x8(%rsp)
mov    %r15,%rdi
mov    $0xa,%esi
mov    %r14,%rdx
call   1060 <get_str_from_stream@plt>
mov    0x8(%rsp),%rsi
lea    0xe1d(%rip),%rdi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    1279 <main+0xf9>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xda5(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Error while reading %s\0A\00"
@.str.3 = [48 x i8] c"\1B[31;1mTest\1B[0m\0AFile contents:\0A%s\0ASize: %ld\0A\00"
@.str.4 = [48 x i8] c"\1B[31;1mTest\1B[0m\0AFile contents:\0A%s\0ASize: %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %50
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %6
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %27)
store i32 -2, i32* %3
br label %50
29: ; preds = %17
store i8* null, i8** %7
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i64 @get_str_from_path(i8** %7, i32 0, i8* %32)
store i64 %33, i64* %8
%34 = load i8*, i8** %7
%35 = load i64, i64* %8
%36 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %34, i64 %35)
%37 = load i8*, i8** %7
%38 = call i32 @free(i8* %37)
store i8* null, i8** %7
%39 = call i64 @malloc(i32 10)
%40 = inttoptr i64 %39 to i8*
store i8* %40, i8** %7
%41 = load i32*, i32** %6
%42 = call i64 @get_str_from_stream(i8** %7, i32 10, i32* %41)
store i64 %42, i64* %8
%43 = load i8*, i8** %7
%44 = load i64, i64* %8
%45 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0), i8* %43, i64 %44)
%46 = load i8*, i8** %7
%47 = call i32 @free(i8* %46)
%48 = load i32*, i32** %6
%49 = call i32 @fclose(i32* %48)
store i32 0, i32* %3
br label %50
50: ; preds = %29, %23, %11
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [24 x i8] c"Error while reading %s\0A\00"
@.str.3 = [48 x i8] c"\1B[31;1mTest\1B[0m\0AFile contents:\0A%s\0ASize: %ld\0A\00"
@.str.4 = [48 x i8] c"\1B[31;1mTest\1B[0m\0AFile contents:\0A%s\0ASize: %ld\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %34
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %10
%17 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %16)
br label %34
18: ; preds = %9
%19 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%20 = load i8*, i8** %10
%21 = call i64 @get_str_from_path(i8** %3, i32 0, i8* %20)
%22 = load i8*, i8** %3
%23 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %22, i64 %21)
%24 = load i8*, i8** %3
%25 = call i32 @free(i8* %24)
store i8* null, i8** %3
%26 = call i64 @malloc(i32 10)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %3
%28 = call i64 @get_str_from_stream(i8** %3, i32 10, i32* %12)
%29 = load i8*, i8** %3
%30 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.4, i64 0, i64 0), i8* %29, i64 %28)
%31 = load i8*, i8** %3
%32 = call i32 @free(i8* %31)
%33 = call i32 @fclose(i32* %12)
br label %34
34: ; preds = %14, %18, %5
%35 = phi i32 [ -1, %5 ], [ -2, %14 ], [ 0, %18 ]
ret i32 %35
}