#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 /*<<< orphan*/ st_size;
};
struct directory {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ add_de(struct directory *, char const *, int,
                          int /*<<< orphan*/, void *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void add_file(struct directory *parent, const char *filename) {
  int fd = open(filename, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "cannot open '%s': %s\n", filename, strerror(errno));
    exit(1);
  }
  struct stat st;
  fstat(fd, &st);
  void *data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (data == (void *)-1) {
    fprintf(stderr, "cannot load '%s': %s\n", filename, strerror(errno));
    exit(1);
  }
  add_de(parent, filename, false, st.st_size, data);
}


x86 assembly (clang, -O0):
<add_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e1 <add_file+0x71>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xe2e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
mov    -0x18(%rbp),%esi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
mov    0x2dc0(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    1269 <add_file+0xf9>
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdbc(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%ecx
mov    -0x20(%rbp),%r8
xor    %edx,%edx
call   1030 <add_de@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11e5 <add_file+0x75>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
mov    (%rsp),%ebp
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
mov    0x2e0e(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1205 <add_file+0x95>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %edx,%edx
mov    %ebp,%ecx
mov    %rax,%r8
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <add_de@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdfd(%rip),%rsi
jmp    1223 <add_file+0xb3>
mov    0x2dc4(%rip),%rax
mov    (%rax),%ebx
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf3(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.directory = type { i32 }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"cannot open '%s': %s\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [22 x i8] c"cannot load '%s': %s\0A\00"
define void @add_file(%struct.directory* %0, i8* %1) {
%3 = alloca %struct.directory*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i8*
store %struct.directory* %0, %struct.directory** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %4
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %5
%22 = call i32 @fstat(i32 %21, %struct.stat* %6)
%23 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_PRIVATE
%27 = load i32, i32* %5
%28 = call i8* @mmap(i32* null, i32 %24, i32 %25, i32 %26, i32 %27, i32 0)
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = icmp eq i8* %29, inttoptr (i64 -1 to i8*)
br i1 %30, label %31, label %38
31: ; preds = %20
%32 = load i32, i32* @stderr
%33 = load i8*, i8** %4
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %33, i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %20
%39 = load %struct.directory*, %struct.directory** %3
%40 = load i8*, i8** %4
%41 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i8*, i8** %7
%44 = call i32 @add_de(%struct.directory* %39, i8* %40, i32 0, i32 %42, i8* %43)
ret void
}

LLVM IR (-O2):
%struct.directory = type { i32 }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"cannot open '%s': %s\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [22 x i8] c"cannot load '%s': %s\0A\00"
define void @add_file(%struct.directory* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %1, i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = bitcast %struct.stat* %3 to i8*
%15 = call i32 @fstat(i32 %5, %struct.stat* %3)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_PRIVATE
%20 = tail call i8* @mmap(i32* null, i32 %17, i32 %18, i32 %19, i32 %5, i32 0)
%21 = icmp eq i8* %20, inttoptr (i64 -1 to i8*)
br i1 %21, label %22, label %28
22: ; preds = %13
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %25)
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %13
%29 = tail call i32 @add_de(%struct.directory* %0, i8* %1, i32 0, i32 %17, i8* %20)
ret void
}