#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fd_len(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *const);
scalar_t__ is_regular_file(char const *const);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(int);
int open(char const *const, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ stderr;
char *regular_file_to_string(const char *const file_path) {
  if (is_regular_file(file_path)) {
    int fd = open(file_path, O_RDONLY);
    int length = fd_len(fd);
    char *bf;
    if (fd == -1) {
      fprintf(stderr, "Cannot open file %s", file_path);
      exit(EXIT_FAILURE);
    } else {
      lseek(fd, 0, SEEK_SET);
      bf = malloc(length + 1);
      read(fd, bf, length);
      bf[length] = '\0';
    }
    return bf;
  } else {
    return NULL;
  }
}


x86 assembly (clang, -O0):
<regular_file_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <is_regular_file@plt>
cmp    $0x0,%rax
je     123a <regular_file_to_string+0xba>
mov    -0x10(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1080 <fd_len@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f0 <regular_file_to_string+0x70>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe23(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <read@plt>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1242 <regular_file_to_string+0xc2>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<regular_file_to_string>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1050 <is_regular_file@plt>
test   %rax,%rax
je     11e7 <regular_file_to_string+0x67>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <fd_len@plt>
cmp    $0xffffffff,%ebp
je     11f1 <regular_file_to_string+0x71>
mov    %eax,%r14d
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
lea    0x1(%r14),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
movslq %r14d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    11e9 <regular_file_to_string+0x69>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Cannot open file %s\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
define i8* @regular_file_to_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @is_regular_file(i8* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %41
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %11, i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = call i32 @fd_len(i32 %14)
store i32 %15, i32* %5
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %24
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %10
%25 = load i32, i32* %4
%26 = load i32, i32* @SEEK_SET
%27 = call i32 @lseek(i32 %25, i32 0, i32 %26)
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
%30 = call i8* @malloc(i32 %29)
store i8* %30, i8** %6
%31 = load i32, i32* %4
%32 = load i8*, i8** %6
%33 = load i32, i32* %5
%34 = call i32 @read(i32 %31, i8* %32, i32 %33)
%35 = load i8*, i8** %6
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %24
%40 = load i8*, i8** %6
store i8* %40, i8** %2
br label %42
41: ; preds = %1
store i8* null, i8** %2
br label %42
42: ; preds = %41, %39
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"Cannot open file %s\00"
@EXIT_FAILURE = global i32 0
@SEEK_SET = global i32 0
define noalias i8* @regular_file_to_string(i8* %0) {
%2 = tail call i64 @is_regular_file(i8* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %22, label %4
4: ; preds = %1
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = tail call i32 @fd_len(i32 %6)
%8 = icmp eq i32 %6, -1
br i1 %8, label %9, label %14
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %4
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @lseek(i32 %6, i32 0, i32 %15)
%17 = add nsw i32 %7, 1
%18 = tail call i8* @malloc(i32 %17)
%19 = tail call i32 @read(i32 %6, i8* %18, i32 %7)
%20 = sext i32 %7 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %1, %14
%23 = phi i8* [ %18, %14 ], [ null, %1 ]
ret i8* %23
}