#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*/ SEEK_END;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
long const fread(char *, int, long const, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long const);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *load_file_to_cstring(const char *path) {
  FILE *pem_file = fopen(path, "rb");
  if (!pem_file) {
    fprintf(stderr, "Failed to open file %s: '%s'\n", path, strerror(errno));
    return NULL;
  }
  if (fseek(pem_file, 0, SEEK_END) < 0) {
    fprintf(stderr, "Failed calling fseek: '%s'\n", strerror(errno));
    fclose(pem_file);
    return NULL;
  }
  const long int pem_file_size = ftell(pem_file);
  if (pem_file_size < 0) {
    fprintf(stderr, "Failed calling ftell: '%s'\n", strerror(errno));
    fclose(pem_file);
    return NULL;
  }
  rewind(pem_file);
  char *pem_out = malloc(pem_file_size + 1);
  if (pem_out == NULL) {
    fprintf(stderr, "Failed allocating memory\n");
    fclose(pem_file);
    return NULL;
  }
  if (fread(pem_out, sizeof(char), pem_file_size, pem_file) < pem_file_size) {
    fprintf(stderr, "Failed reading file: '%s'\n", strerror(errno));
    free(pem_out);
    fclose(pem_file);
    return NULL;
  }
  pem_out[pem_file_size] = '\0';
  fclose(pem_file);
  return pem_out;
}


x86 assembly (clang, -O0):
<load_file_to_cstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1212 <load_file_to_cstring+0x72>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ac <load_file_to_cstring+0x20c>
mov    -0x18(%rbp),%rdi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
jge    1274 <load_file_to_cstring+0xd4>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ac <load_file_to_cstring+0x20c>
mov    -0x18(%rbp),%rdi
call   1080 <ftell@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    12d0 <load_file_to_cstring+0x130>
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    %rax,%rdx
lea    0xd8a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ac <load_file_to_cstring+0x20c>
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1322 <load_file_to_cstring+0x182>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ac <load_file_to_cstring+0x20c>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
cmp    -0x20(%rbp),%rax
jge    138f <load_file_to_cstring+0x1ef>
mov    0x2c8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    %rax,%rdx
lea    0xd0a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ac <load_file_to_cstring+0x20c>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_file_to_cstring>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbx
lea    0xe4e(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     126e <load_file_to_cstring+0xce>
mov    %rax,%r12
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
js     12a0 <load_file_to_cstring+0x100>
mov    %r12,%rdi
call   1080 <ftell@plt>
test   %rax,%rax
js     12c0 <load_file_to_cstring+0x120>
mov    %rax,%r15
mov    %r12,%rdi
call   1060 <rewind@plt>
lea    0x1(%r15),%rdi
call   1090 <malloc@plt>
test   %rax,%rax
je     12ef <load_file_to_cstring+0x14f>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r12,%rcx
call   1040 <fread@plt>
cmp    %r15,%rax
jge    130b <load_file_to_cstring+0x16b>
mov    0x2da5(%rip),%rax
mov    (%rax),%ebx
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xe29(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
xor    %r14d,%r14d
jmp    1318 <load_file_to_cstring+0x178>
mov    0x2d63(%rip),%rax
mov    (%rax),%ebp
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd77(%rip),%rsi
xor    %r14d,%r14d
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1318 <load_file_to_cstring+0x178>
mov    0x2d31(%rip),%rax
mov    (%rax),%ebx
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd63(%rip),%rsi
jmp    12de <load_file_to_cstring+0x13e>
mov    0x2d11(%rip),%rax
mov    (%rax),%ebx
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd5f(%rip),%rsi
xor    %r14d,%r14d
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1310 <load_file_to_cstring+0x170>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
lea    0xd5a(%rip),%rsi
xor    %r14d,%r14d
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1310 <load_file_to_cstring+0x170>
movb   $0x0,(%r14,%r15,1)
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Failed to open file %s: '%s'\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [28 x i8] c"Failed calling fseek: '%s'\0A\00"
@.str.3 = [28 x i8] c"Failed calling ftell: '%s'\0A\00"
@.str.4 = [26 x i8] c"Failed allocating memory\0A\00"
@.str.5 = [27 x i8] c"Failed reading file: '%s'\0A\00"
define i8* @load_file_to_cstring(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %4
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %17, label %11
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = load i8*, i8** %3
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %13, i8* %15)
store i8* null, i8** %2
br label %77
17: ; preds = %1
%18 = load i32*, i32** %4
%19 = load i32, i32* @SEEK_END
%20 = call i64 @fseek(i32* %18, i32 0, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32*, i32** %4
%28 = call i32 @fclose(i32* %27)
store i8* null, i8** %2
br label %77
29: ; preds = %17
%30 = load i32*, i32** %4
%31 = call i64 @ftell(i32* %30)
store i64 %31, i64* %5
%32 = load i64, i64* %5
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = load i32*, i32** %4
%40 = call i32 @fclose(i32* %39)
store i8* null, i8** %2
br label %77
41: ; preds = %29
%42 = load i32*, i32** %4
%43 = call i32 @rewind(i32* %42)
%44 = load i64, i64* %5
%45 = add nsw i64 %44, 1
%46 = call i8* @malloc(i64 %45)
store i8* %46, i8** %6
%47 = load i8*, i8** %6
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %54
49: ; preds = %41
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32*, i32** %4
%53 = call i32 @fclose(i32* %52)
store i8* null, i8** %2
br label %77
54: ; preds = %41
%55 = load i8*, i8** %6
%56 = load i64, i64* %5
%57 = load i32*, i32** %4
%58 = call i64 @fread(i8* %55, i32 1, i64 %56, i32* %57)
%59 = load i64, i64* %5
%60 = icmp slt i64 %58, %59
br i1 %60, label %61, label %70
61: ; preds = %54
%62 = load i32, i32* @stderr
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %64)
%66 = load i8*, i8** %6
%67 = call i32 @free(i8* %66)
%68 = load i32*, i32** %4
%69 = call i32 @fclose(i32* %68)
store i8* null, i8** %2
br label %77
70: ; preds = %54
%71 = load i8*, i8** %6
%72 = load i64, i64* %5
%73 = getelementptr inbounds i8, i8* %71, i64 %72
store i8 0, i8* %73
%74 = load i32*, i32** %4
%75 = call i32 @fclose(i32* %74)
%76 = load i8*, i8** %6
store i8* %76, i8** %2
br label %77
77: ; preds = %70, %61, %49, %34, %22, %11
%78 = load i8*, i8** %2
ret i8* %78
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Failed to open file %s: '%s'\0A\00"
@errno = global i32 0
@SEEK_END = global i32 0
@.str.2 = [28 x i8] c"Failed calling fseek: '%s'\0A\00"
@.str.3 = [28 x i8] c"Failed calling ftell: '%s'\0A\00"
@.str.4 = [26 x i8] c"Failed allocating memory\0A\00"
@.str.5 = [27 x i8] c"Failed reading file: '%s'\0A\00"
define noalias i8* @load_file_to_cstring(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %9
4: ; preds = %1
%5 = load i32, i32* @stderr
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %7)
br label %50
9: ; preds = %1
%10 = load i32, i32* @SEEK_END
%11 = tail call i64 @fseek(i32* %2, i32 0, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = tail call i32 @fclose(i32* %2)
br label %50
19: ; preds = %9
%20 = tail call i64 @ftell(i32* %2)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = tail call i32 @fclose(i32* %2)
br label %50
28: ; preds = %19
%29 = tail call i32 @rewind(i32* %2)
%30 = add nuw nsw i64 %20, 1
%31 = tail call i8* @malloc(i64 %30)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%36 = tail call i32 @fclose(i32* %2)
br label %50
37: ; preds = %28
%38 = tail call i64 @fread(i8* %31, i32 1, i64 %20, i32* %2)
%39 = icmp slt i64 %38, %20
br i1 %39, label %40, label %47
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = tail call i8* @strerror(i32 %42)
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %43)
%45 = tail call i32 @free(i8* %31)
%46 = tail call i32 @fclose(i32* %2)
br label %50
47: ; preds = %37
%48 = getelementptr inbounds i8, i8* %31, i64 %20
store i8 0, i8* %48
%49 = tail call i32 @fclose(i32* %2)
br label %50
50: ; preds = %22, %47, %40, %33, %13, %4
%51 = phi i8* [ null, %13 ], [ null, %4 ], [ null, %22 ], [ null, %33 ], [ null, %40 ], [ %31, %47 ]
ret i8* %51
}