#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int close(int);
int /*<<< orphan*/ errno;
int get_size(char const *);
char *malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char const *, ...);
int read(int, char *, int);
char *strerror(int /*<<< orphan*/);
char *read_file(const char *path) {
  int fd;
  int file_size;
  char *data;
  int ret;
  if ((file_size = get_size(path)) >= 0) {
    data = malloc(file_size);
    if ((fd = open(path, O_RDONLY)) < 0) {
      printf("Error al abrir el archivo '%s'\n", path);
      data = NULL;
    } else {
      if ((ret = read(fd, data, file_size)) < 0) {
        printf("Error leyendo '%s': %s\n", path, strerror(errno));
        data = NULL;
      } else if ((ret = close(fd)) < 0) {
        printf("Error leyendo '%s': %s\n", path, strerror(errno));
        data = NULL;
      }
    }
  } else {
    printf("Error al abrir el archivo '%s'\n", path);
    data = NULL;
  }
  return data;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <get_size@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jl     1283 <read_file+0x113>
mov    -0x10(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11da <read_file+0x6a>
mov    -0x8(%rbp),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
jmp    127e <read_file+0x10e>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    122d <read_file+0xbd>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
jmp    1279 <read_file+0x109>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1274 <read_file+0x104>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xdbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
jmp    1279 <read_file+0x109>
jmp    127e <read_file+0x10e>
jmp    129d <read_file+0x12d>
mov    -0x8(%rbp),%rsi
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <get_size@plt>
test   %eax,%eax
js     11e8 <read_file+0x78>
mov    %eax,%ebp
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e8 <read_file+0x78>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1060 <read@plt>
test   %eax,%eax
js     11c1 <read_file+0x51>
mov    %ebx,%edi
call   1050 <close@plt>
test   %eax,%eax
jns    11fc <read_file+0x8c>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe4a(%rip),%rdi
xor    %r14d,%r14d
mov    %r15,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11fc <read_file+0x8c>
lea    0xe11(%rip),%rdi
xor    %r14d,%r14d
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [32 x i8] c"Error al abrir el archivo '%s'\0A\00"
@.str.1 = [24 x i8] c"Error leyendo '%s': %s\0A\00"
@errno = global i32 0
define i8* @read_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @get_size(i8* %7)
store i32 %8, i32* %4
%9 = icmp sge i32 %8, 0
br i1 %9, label %10, label %43
10: ; preds = %1
%11 = load i32, i32* %4
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %2
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %3
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i8*, i8** %2
%19 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %18)
store i8* null, i8** %5
br label %42
20: ; preds = %10
%21 = load i32, i32* %3
%22 = load i8*, i8** %5
%23 = load i32, i32* %4
%24 = call i32 @read(i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %20
%27 = load i8*, i8** %2
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* %29)
store i8* null, i8** %5
br label %41
31: ; preds = %20
%32 = load i32, i32* %3
%33 = call i32 @close(i32 %32)
store i32 %33, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %31
%36 = load i8*, i8** %2
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %36, i8* %38)
store i8* null, i8** %5
br label %40
40: ; preds = %35, %31
br label %41
41: ; preds = %40, %26
br label %42
42: ; preds = %41, %17
br label %46
43: ; preds = %1
%44 = load i8*, i8** %2
%45 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %44)
store i8* null, i8** %5
br label %46
46: ; preds = %43, %42
%47 = load i8*, i8** %5
ret i8* %47
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [32 x i8] c"Error al abrir el archivo '%s'\0A\00"
@.str.1 = [24 x i8] c"Error leyendo '%s': %s\0A\00"
@errno = global i32 0
define noalias i8* @read_file(i8* %0) {
%2 = tail call i32 @get_size(i8* %0)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %25
4: ; preds = %1
%5 = tail call i8* @malloc(i32 %2)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %27
11: ; preds = %4
%12 = tail call i32 @read(i32 %7, i8* %5, i32 %2)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %16)
br label %27
18: ; preds = %11
%19 = tail call i32 @close(i32 %7)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = tail call i8* @strerror(i32 %22)
%24 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %23)
br label %27
25: ; preds = %1
%26 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %27
27: ; preds = %9, %18, %21, %14, %25
%28 = phi i8* [ null, %9 ], [ null, %14 ], [ null, %21 ], [ %5, %18 ], [ null, %25 ]
ret i8* %28
}