#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 {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
size_t read(int, char *, size_t);
int stat(char *, struct stat *);
int /*<<< orphan*/ trio_printf(char *, char *);
char *get_text_from_file(char *file_name, size_t *text_size) {
  struct stat st;
  size_t fsize, bread, i = 0;
  int fd = 0;
  char *buff = NULL;
  int ret_stat = stat(file_name, &st);
  fsize = st.st_size;
  buff = (char *)malloc(sizeof(char) * (fsize + 10));
  fd = open(file_name, O_RDONLY);
  if (fd <= 0) {
    trio_printf("Cannot open %s file\n", file_name);
    return NULL;
  }
  bread = read(fd, buff, fsize);
  close(fd);
  *text_size = bread;
  return buff;
}


x86 assembly (clang, -O0):
<get_text_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0xa,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jg     11f2 <get_text_from_file+0x92>
mov    -0x10(%rbp),%rsi
lea    0xe20(%rip),%rdi
call   1030 <trio_printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1221 <get_text_from_file+0xc1>
mov    -0x3c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <read@plt>
mov    %rax,-0x30(%rbp)
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_text_from_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
mov    0x8(%rsp),%r12
lea    0xa(%r12),%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
jle    11c1 <get_text_from_file+0x61>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <read@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,(%r14)
jmp    11d3 <get_text_from_file+0x73>
lea    0xe38(%rip),%rdi
mov    %rbx,%rsi
call   1030 <trio_printf@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [21 x i8] c"Cannot open %s file\0A\00"
define i8* @get_text_from_file(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca %struct.stat
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
store i64 0, i64* %9
store i32 0, i32* %10
store i8* null, i8** %11
%13 = load i8*, i8** %4
%14 = call i32 @stat(i8* %13, %struct.stat* %6)
store i32 %14, i32* %12
%15 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%16 = load i64, i64* %15
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = add i64 %17, 10
%19 = mul i64 1, %18
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %11
%23 = load i8*, i8** %4
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp sle i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %2
%29 = load i8*, i8** %4
%30 = call i32 @trio_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %29)
store i8* null, i8** %3
br label %41
31: ; preds = %2
%32 = load i32, i32* %10
%33 = load i8*, i8** %11
%34 = load i64, i64* %7
%35 = call i64 @read(i32 %32, i8* %33, i64 %34)
store i64 %35, i64* %8
%36 = load i32, i32* %10
%37 = call i32 @close(i32 %36)
%38 = load i64, i64* %8
%39 = load i64*, i64** %5
store i64 %38, i64* %39
%40 = load i8*, i8** %11
store i8* %40, i8** %3
br label %41
41: ; preds = %31, %28
%42 = load i8*, i8** %3
ret i8* %42
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [21 x i8] c"Cannot open %s file\0A\00"
define i8* @get_text_from_file(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %0, %struct.stat* %3)
%6 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%7 = load i64, i64* %6
%8 = trunc i64 %7 to i32
%9 = add i32 %8, 10
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %0, i32 %12)
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = tail call i32 @trio_printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %20
17: ; preds = %2
%18 = tail call i64 @read(i32 %13, i8* %11, i64 %7)
%19 = tail call i32 @close(i32 %13)
store i64 %18, i64* %1
br label %20
20: ; preds = %17, %15
%21 = phi i8* [ null, %15 ], [ %11, %17 ]
ret i8* %21
}