#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 {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ print_message(int, int /*<<< orphan*/);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *open_file(char *path) {
  FILE *fp = fopen(path, "r");
  if (fp == NULL) {
    char str[1024];
    strcpy(str, path);
    print_message(1, strcat("Can't open a file:", str));
    return NULL;
  }
  struct stat st;
  stat(path, &st);
  char *filedata = (char *)malloc(st.st_size * sizeof(char) + 1);
  char buf[1024];
  while (fgets(buf, 1024, fp) != NULL) {
    strcat(filedata, buf);
  }
  fclose(fp);
  return filedata;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ea <open_file+0x6a>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rsi
lea    0xe36(%rip),%rdi
call   10a0 <strcat@plt>
mov    %eax,%esi
mov    $0x1,%edi
call   1050 <print_message@plt>
movq   $0x0,-0x8(%rbp)
jmp    1262 <open_file+0xe2>
mov    -0x10(%rbp),%rdi
lea    -0x428(%rbp),%rsi
call   1070 <stat@plt>
movslq -0x428(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x430(%rbp)
lea    -0x830(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     124e <open_file+0xce>
mov    -0x430(%rbp),%rdi
lea    -0x830(%rbp),%rsi
call   10a0 <strcat@plt>
jmp    1217 <open_file+0x97>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x430(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rdi,%rbx
lea    0xe6a(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     120a <open_file+0x8a>
mov    %rax,%r14
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
mov    0x8(%rsp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1200 <open_file+0x80>
lea    0x10(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
jne    11e0 <open_file+0x60>
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    1238 <open_file+0xb8>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xde1(%rip),%rdi
mov    %r14,%rsi
call   10a0 <strcat@plt>
mov    $0x1,%edi
mov    %eax,%esi
call   1050 <print_message@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Can't open a file:\00"
define i8* @open_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca [1024 x i8]
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %4
%11 = load i32*, i32** %4
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %20
13: ; preds = %1
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%15 = load i8*, i8** %3
%16 = call i32 @strcpy(i8* %14, i8* %15)
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%18 = call i32 @strcat(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @print_message(i32 1, i32 %18)
store i8* null, i8** %2
br label %44
20: ; preds = %1
%21 = load i8*, i8** %3
%22 = call i32 @stat(i8* %21, %struct.stat* %6)
%23 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = mul i64 %25, 1
%27 = add i64 %26, 1
%28 = trunc i64 %27 to i32
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %7
br label %31
31: ; preds = %36, %20
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%33 = load i32*, i32** %4
%34 = call i32* @fgets(i8* %32, i32 1024, i32* %33)
%35 = icmp ne i32* %34, null
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = load i8*, i8** %7
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%39 = call i32 @strcat(i8* %37, i8* %38)
br label %31
40: ; preds = %31
%41 = load i32*, i32** %4
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %7
store i8* %43, i8** %2
br label %44
44: ; preds = %40, %13
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"r\00"
@.str.1 = [19 x i8] c"Can't open a file:\00"
define i8* @open_file(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%9 = call i32 @strcpy(i8* %8, i8* %0)
%10 = call i32 @strcat(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%11 = call i32 @print_message(i32 1, i32 %10)
br label %29
12: ; preds = %1
%13 = bitcast %struct.stat* %3 to i8*
%14 = call i32 @stat(i8* %0, %struct.stat* %3)
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = add i32 %16, 1
%18 = tail call i64 @malloc(i32 %17)
%19 = inttoptr i64 %18 to i8*
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%21 = call i32* @fgets(i8* %20, i32 1024, i32* %5)
%22 = icmp eq i32* %21, null
br i1 %22, label %27, label %23
23: ; preds = %12, %23
%24 = call i32 @strcat(i8* %19, i8* %20)
%25 = call i32* @fgets(i8* %20, i32 1024, i32* %5)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %23
27: ; preds = %23, %12
%28 = call i32 @fclose(i32* %5)
br label %29
29: ; preds = %27, %7
%30 = phi i8* [ null, %7 ], [ %19, %27 ]
ret i8* %30
}