#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 LARAttr {
  int dummy;
};
struct LAR {
  int dummy;
};

/* Variables and functions */
int LAR_AppendBuffer(struct LAR *, unsigned char *, int /*<<< orphan*/,
                     struct LARAttr *);
int LAR_AppendSelf(struct LAR *, char const *, struct LARAttr *);
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
scalar_t__ fstat(int, struct stat *);
scalar_t__ iself(char *);
scalar_t__ mmap(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
                int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int LAR_AppendFile(struct LAR *lar, const char *filename,
                   struct LARAttr *attr) {
  int fd;
  struct stat s;
  char *filep;
  int ret;
  if (iself((char *)filename))
    return LAR_AppendSelf(lar, filename, attr);
  fd = open(filename, O_RDONLY);
  if (fd == -1)
    return -1;
  if (fstat(fd, &s))
    return -1;
  filep = (char *)mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (filep == MAP_FAILED)
    return -1;
  ret = LAR_AppendBuffer(lar, (unsigned char *)filep, s.st_size, attr);
  munmap(filep, s.st_size);
  return ret;
}


x86 assembly (clang, -O0):
<LAR_AppendFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <iself@plt>
cmp    $0x0,%rax
je     11b0 <LAR_AppendFile+0x40>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <LAR_AppendSelf@plt>
mov    %eax,-0x4(%rbp)
jmp    126e <LAR_AppendFile+0xfe>
mov    -0x18(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11db <LAR_AppendFile+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <LAR_AppendFile+0xfe>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
je     11fd <LAR_AppendFile+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <LAR_AppendFile+0xfe>
mov    -0x28(%rbp),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    0x2dd8(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%rax
jne    1245 <LAR_AppendFile+0xd5>
movl   $0xffffffff,-0x4(%rbp)
jmp    126e <LAR_AppendFile+0xfe>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1060 <LAR_AppendBuffer@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%esi
call   1070 <munmap@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<LAR_AppendFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <iself@plt>
test   %rax,%rax
je     11a5 <LAR_AppendFile+0x35>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <LAR_AppendSelf@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11cc <LAR_AppendFile+0x5c>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
je     11dc <LAR_AppendFile+0x6c>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%r15d
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    0x2df8(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbp
mov    0x2dbc(%rip),%rax
cmp    %rbp,(%rax)
je     11cc <LAR_AppendFile+0x5c>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15d,%edx
mov    %r14,%rcx
call   1060 <LAR_AppendBuffer@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
mov    %r15d,%esi
call   1070 <munmap@plt>
mov    %ebx,%eax
jmp    11d1 <LAR_AppendFile+0x61>

LLVM IR (-O0):
%struct.LAR = type { i32 }
%struct.LARAttr = type { i32 }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @LAR_AppendFile(%struct.LAR* %0, i8* %1, %struct.LARAttr* %2) {
%4 = alloca i32
%5 = alloca %struct.LAR*
%6 = alloca i8*
%7 = alloca %struct.LARAttr*
%8 = alloca i32
%9 = alloca %struct.stat
%10 = alloca i8*
%11 = alloca i32
store %struct.LAR* %0, %struct.LAR** %5
store i8* %1, i8** %6
store %struct.LARAttr* %2, %struct.LARAttr** %7
%12 = load i8*, i8** %6
%13 = call i64 @iself(i8* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load %struct.LAR*, %struct.LAR** %5
%17 = load i8*, i8** %6
%18 = load %struct.LARAttr*, %struct.LARAttr** %7
%19 = call i32 @LAR_AppendSelf(%struct.LAR* %16, i8* %17, %struct.LARAttr* %18)
store i32 %19, i32* %4
br label %56
20: ; preds = %3
%21 = load i8*, i8** %6
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 -1, i32* %4
br label %56
27: ; preds = %20
%28 = load i32, i32* %8
%29 = call i64 @fstat(i32 %28, %struct.stat* %9)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -1, i32* %4
br label %56
32: ; preds = %27
%33 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%34 = load i32, i32* %33
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @MAP_SHARED
%37 = load i32, i32* %8
%38 = call i64 @mmap(i32 0, i32 %34, i32 %35, i32 %36, i32 %37, i32 0)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %10
%40 = load i8*, i8** %10
%41 = load i8*, i8** @MAP_FAILED
%42 = icmp eq i8* %40, %41
br i1 %42, label %43, label %44
43: ; preds = %32
store i32 -1, i32* %4
br label %56
44: ; preds = %32
%45 = load %struct.LAR*, %struct.LAR** %5
%46 = load i8*, i8** %10
%47 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load %struct.LARAttr*, %struct.LARAttr** %7
%50 = call i32 @LAR_AppendBuffer(%struct.LAR* %45, i8* %46, i32 %48, %struct.LARAttr* %49)
store i32 %50, i32* %11
%51 = load i8*, i8** %10
%52 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @munmap(i8* %51, i32 %53)
%55 = load i32, i32* %11
store i32 %55, i32* %4
br label %56
56: ; preds = %44, %43, %31, %26, %15
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
%struct.LAR = type { i32 }
%struct.LARAttr = type { i32 }
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32 @LAR_AppendFile(%struct.LAR* %0, i8* %1, %struct.LARAttr* %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = tail call i64 @iself(i8* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @LAR_AppendSelf(%struct.LAR* %0, i8* %1, %struct.LARAttr* %2)
br label %29
10: ; preds = %3
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %1, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %29, label %14
14: ; preds = %10
%15 = call i64 @fstat(i32 %12, %struct.stat* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @MAP_SHARED
%22 = tail call i64 @mmap(i32 0, i32 %19, i32 %20, i32 %21, i32 %12, i32 0)
%23 = inttoptr i64 %22 to i8*
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %24, %23
br i1 %25, label %29, label %26
26: ; preds = %17
%27 = tail call i32 @LAR_AppendBuffer(%struct.LAR* %0, i8* %23, i32 %19, %struct.LARAttr* %2)
%28 = tail call i32 @munmap(i8* %23, i32 %19)
br label %29
29: ; preds = %17, %14, %10, %26, %8
%30 = phi i32 [ %9, %8 ], [ %27, %26 ], [ -1, %10 ], [ -1, %14 ], [ -1, %17 ]
ret i32 %30
}