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

/* Variables and functions */
void *__xmalloc(int, char *);
struct elf_file_t *elf_file_create_from_allocated_buffer(void *, int,
                                                         char const *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ m2s_opencl_fatal(char *, char const *);
scalar_t__ stat(char const *, struct stat *);
struct elf_file_t *elf_file_create_from_path(const char *path) {
  struct elf_file_t *elf_file;
  struct stat st;
  FILE *f;
  void *buffer;
  int size;
  int count;
  if (stat(path, &st))
    m2s_opencl_fatal("'%s': path not found", path);
  size = st.st_size;
  f = fopen(path, "rt");
  if (!f)
    m2s_opencl_fatal("'%s': cannot open file", path);
  buffer = (__xmalloc(
      (size), "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/MrWhoami/lacs_implementation/refs/"
              "heads/HSA_Develop/m2s-rcsl/runtime/cuda/elf-format.c"
              ":"
              "619"));
  count = fread(buffer, 1, size, f);
  if (count != size)
    m2s_opencl_fatal("'%s': error reading file contents", path);
  elf_file = elf_file_create_from_allocated_buffer(buffer, size, path);
  return elf_file;
}


x86 assembly (clang, -O0):
<elf_file_create_from_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     1193 <elf_file_create_from_path+0x33>
mov    -0x8(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1070 <m2s_opencl_fatal@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c8 <elf_file_create_from_path+0x68>
mov    -0x8(%rbp),%rsi
lea    0xe55(%rip),%rdi
call   1070 <m2s_opencl_fatal@plt>
mov    -0x2c(%rbp),%edi
lea    0xe5d(%rip),%rsi
call   1050 <__xmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     120f <elf_file_create_from_path+0xaf>
mov    -0x8(%rbp),%rsi
lea    0xeda(%rip),%rdi
call   1070 <m2s_opencl_fatal@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x8(%rbp),%rdx
call   1030 <elf_file_create_from_allocated_buffer@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<elf_file_create_from_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     1186 <elf_file_create_from_path+0x26>
lea    0xe82(%rip),%rdi
mov    %r14,%rsi
call   1070 <m2s_opencl_fatal@plt>
mov    (%rsp),%ebp
lea    0xe85(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11af <elf_file_create_from_path+0x4f>
lea    0xe71(%rip),%rdi
mov    %r14,%rsi
call   1070 <m2s_opencl_fatal@plt>
lea    0xe79(%rip),%rsi
mov    %ebp,%edi
call   1050 <__xmalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    %ebp,%eax
je     11e5 <elf_file_create_from_path+0x85>
lea    0xf07(%rip),%rdi
mov    %r14,%rsi
call   1070 <m2s_opencl_fatal@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1030 <elf_file_create_from_allocated_buffer@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.elf_file_t = type { i32 }
%struct.stat = type { i32 }
@.str = [21 x i8] c"'%s': path not found\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [23 x i8] c"'%s': cannot open file\00"
@.str.3 = [181 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/MrWhoami/lacs_implementation/refs/heads/HSA_Develop/m2s-rcsl/runtime/cuda/elf-format.c:619\00"
@.str.4 = [34 x i8] c"'%s': error reading file contents\00"
define %struct.elf_file_t* @elf_file_create_from_path(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.elf_file_t*
%4 = alloca %struct.stat
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i64 @stat(i8* %9, %struct.stat* %4)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = call i32 @m2s_opencl_fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %12, %1
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%17 = load i32, i32* %16
store i32 %17, i32* %7
%18 = load i8*, i8** %2
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %5
%20 = load i32*, i32** %5
%21 = icmp ne i32* %20, null
br i1 %21, label %25, label %22
22: ; preds = %15
%23 = load i8*, i8** %2
%24 = call i32 @m2s_opencl_fatal(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %15
%26 = load i32, i32* %7
%27 = call i8* @__xmalloc(i32 %26, i8* getelementptr inbounds ([181 x i8], [181 x i8]* @.str.3, i64 0, i64 0))
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = load i32*, i32** %5
%31 = call i32 @fread(i8* %28, i32 1, i32 %29, i32* %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = load i8*, i8** %2
%37 = call i32 @m2s_opencl_fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %35, %25
%39 = load i8*, i8** %6
%40 = load i32, i32* %7
%41 = load i8*, i8** %2
%42 = call %struct.elf_file_t* @elf_file_create_from_allocated_buffer(i8* %39, i32 %40, i8* %41)
store %struct.elf_file_t* %42, %struct.elf_file_t** %3
%43 = load %struct.elf_file_t*, %struct.elf_file_t** %3
ret %struct.elf_file_t* %43
}

LLVM IR (-O2):
%struct.elf_file_t = type { i32 }
%struct.stat = type { i32 }
@.str = [21 x i8] c"'%s': path not found\00"
@.str.1 = [3 x i8] c"rt\00"
@.str.2 = [23 x i8] c"'%s': cannot open file\00"
@.str.3 = [181 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/MrWhoami/lacs_implementation/refs/heads/HSA_Develop/m2s-rcsl/runtime/cuda/elf-format.c:619\00"
@.str.4 = [34 x i8] c"'%s': error reading file contents\00"
define %struct.elf_file_t* @elf_file_create_from_path(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = call i32 @m2s_opencl_fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %6, %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = call i32 @m2s_opencl_fatal(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %13, %8
%16 = call i8* @__xmalloc(i32 %10, i8* getelementptr inbounds ([181 x i8], [181 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @fread(i8* %16, i32 1, i32 %10, i32* %11)
%18 = icmp eq i32 %17, %10
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = call i32 @m2s_opencl_fatal(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %21
21: ; preds = %19, %15
%22 = call %struct.elf_file_t* @elf_file_create_from_allocated_buffer(i8* %16, i32 %10, i8* %0)
ret %struct.elf_file_t* %22
}