#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 {
  size_t st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
unsigned char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int S_IRUSR;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int S_IWUSR;
int /*<<< orphan*/ close(int);
int ftruncate(int, size_t);
scalar_t__ mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(unsigned char **, size_t);
int open(char const *, int, ...);
int stat(char const *, struct stat *);
int map_pair(const char *ppath, char const *spath, unsigned char **text,
             unsigned char **create, size_t *size, int tweak) {
  struct stat st;
  int pid, sid, err;
  size_t csize;
  err = stat(ppath, &st);
  if (err != 0)
    return err;
  if (!S_ISREG(st.st_mode))
    return 1;
  *size = st.st_size;
  pid = open(ppath, O_RDONLY);
  *text = (unsigned char *)mmap(NULL, *size, PROT_READ, MAP_SHARED, pid, 0);
  if (*text == MAP_FAILED)
    err = 1;
  csize = *size + tweak;
  sid = open(spath, O_RDWR | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
  err = ftruncate(sid, csize);
  *create = (unsigned char *)mmap(NULL, csize, PROT_READ | PROT_WRITE,
                                  MAP_SHARED, sid, 0);
  if (*create == MAP_FAILED)
    err = 1;
  close(sid);
  close(pid);
  if (err)
    munmap(text, *size);
  return err;
}


x86 assembly (clang, -O0):
<map_pair>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     11b5 <map_pair+0x45>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1328 <map_pair+0x1b8>
mov    -0x40(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11d2 <map_pair+0x62>
movl   $0x1,-0x4(%rbp)
jmp    1328 <map_pair+0x1b8>
mov    -0x48(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
mov    0x2dcd(%rip),%rax
mov    (%rax),%ecx
mov    -0x4c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2d63(%rip),%rcx
cmp    (%rcx),%rax
jne    1245 <map_pair+0xd5>
movl   $0x1,-0x54(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movslq -0x34(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
mov    0x2d8d(%rip),%rax
or     (%rax),%esi
mov    0x2d7c(%rip),%rax
or     (%rax),%esi
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
mov    0x2d1a(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x50(%rbp)
mov    -0x50(%rbp),%edi
mov    -0x60(%rbp),%rsi
call   1040 <ftruncate@plt>
mov    %eax,-0x54(%rbp)
mov    -0x60(%rbp),%rsi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
mov    0x2cf3(%rip),%rax
or     (%rax),%edx
mov    0x2d1a(%rip),%rax
mov    (%rax),%ecx
mov    -0x50(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2cb0(%rip),%rcx
cmp    (%rcx),%rax
jne    12f8 <map_pair+0x188>
movl   $0x1,-0x54(%rbp)
mov    -0x50(%rbp),%edi
call   1050 <close@plt>
mov    -0x4c(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x54(%rbp)
je     1322 <map_pair+0x1b2>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   1080 <munmap@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_pair>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%ebx
mov    %r8,%r14
mov    %rcx,%r12
mov    %rdx,%r15
mov    %rsi,%r13
mov    %rdi,%rbp
lea    0x18(%rsp),%rsi
call   1060 <stat@plt>
test   %eax,%eax
je     11ad <map_pair+0x3d>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,0x8(%rsp)
mov    %r12,0x10(%rsp)
mov    0x20(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     12d2 <map_pair+0x162>
mov    0x18(%rsp),%rax
mov    %r14,%r12
mov    %rax,(%r14)
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,0x4(%rsp)
mov    (%r14),%rsi
mov    0x2df4(%rip),%rcx
mov    (%rcx),%edx
mov    0x2ddb(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r15)
movslq %ebx,%rbp
add    (%r14),%rbp
mov    0x2dac(%rip),%rax
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc2(%rip),%rax
or     (%rax),%esi
mov    0x2d81(%rip),%rax
mov    0x2d62(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x8(%rsp),%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%r14d
mov    %eax,%edi
mov    %rbp,%rsi
call   1040 <ftruncate@plt>
mov    %eax,%r13d
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
mov    0x2d7a(%rip),%rax
or     (%rax),%edx
mov    0x2d61(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbp,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
mov    0x2d00(%rip),%rcx
cmp    %rax,(%rcx)
mov    $0x1,%ebp
cmovne %r13d,%ebp
mov    %r14d,%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
test   %ebp,%ebp
je     119e <map_pair+0x2e>
mov    (%r12),%rsi
mov    %r15,%rdi
call   1080 <munmap@plt>
mov    %ebp,%eax
jmp    119e <map_pair+0x2e>
mov    $0x1,%eax
jmp    119e <map_pair+0x2e>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@PROT_WRITE = global i32 0
define i32 @map_pair(i8* %0, i8* %1, i8** %2, i8** %3, i64* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8**
%11 = alloca i8**
%12 = alloca i64*
%13 = alloca i32
%14 = alloca %struct.stat
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i64
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8** %2, i8*** %10
store i8** %3, i8*** %11
store i64* %4, i64** %12
store i32 %5, i32* %13
%19 = load i8*, i8** %8
%20 = call i32 @stat(i8* %19, %struct.stat* %14)
store i32 %20, i32* %17
%21 = load i32, i32* %17
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %6
%24 = load i32, i32* %17
store i32 %24, i32* %7
br label %98
25: ; preds = %6
%26 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 1
%27 = load i32, i32* %26
%28 = call i32 @S_ISREG(i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %25
store i32 1, i32* %7
br label %98
31: ; preds = %25
%32 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i64*, i64** %12
store i64 %33, i64* %34
%35 = load i8*, i8** %8
%36 = load i32, i32* @O_RDONLY
%37 = call i32 (i8*, i32, ...) @open(i8* %35, i32 %36)
store i32 %37, i32* %15
%38 = load i64*, i64** %12
%39 = load i64, i64* %38
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* @MAP_SHARED
%42 = load i32, i32* %15
%43 = call i64 @mmap(i32* null, i64 %39, i32 %40, i32 %41, i32 %42, i32 0)
%44 = inttoptr i64 %43 to i8*
%45 = load i8**, i8*** %10
store i8* %44, i8** %45
%46 = load i8**, i8*** %10
%47 = load i8*, i8** %46
%48 = load i8*, i8** @MAP_FAILED
%49 = icmp eq i8* %47, %48
br i1 %49, label %50, label %51
50: ; preds = %31
store i32 1, i32* %17
br label %51
51: ; preds = %50, %31
%52 = load i64*, i64** %12
%53 = load i64, i64* %52
%54 = load i32, i32* %13
%55 = sext i32 %54 to i64
%56 = add i64 %53, %55
store i64 %56, i64* %18
%57 = load i8*, i8** %9
%58 = load i32, i32* @O_RDWR
%59 = load i32, i32* @O_CREAT
%60 = or i32 %58, %59
%61 = load i32, i32* @O_TRUNC
%62 = or i32 %60, %61
%63 = load i32, i32* @S_IWUSR
%64 = load i32, i32* @S_IRUSR
%65 = or i32 %63, %64
%66 = call i32 (i8*, i32, ...) @open(i8* %57, i32 %62, i32 %65)
store i32 %66, i32* %16
%67 = load i32, i32* %16
%68 = load i64, i64* %18
%69 = call i32 @ftruncate(i32 %67, i64 %68)
store i32 %69, i32* %17
%70 = load i64, i64* %18
%71 = load i32, i32* @PROT_READ
%72 = load i32, i32* @PROT_WRITE
%73 = or i32 %71, %72
%74 = load i32, i32* @MAP_SHARED
%75 = load i32, i32* %16
%76 = call i64 @mmap(i32* null, i64 %70, i32 %73, i32 %74, i32 %75, i32 0)
%77 = inttoptr i64 %76 to i8*
%78 = load i8**, i8*** %11
store i8* %77, i8** %78
%79 = load i8**, i8*** %11
%80 = load i8*, i8** %79
%81 = load i8*, i8** @MAP_FAILED
%82 = icmp eq i8* %80, %81
br i1 %82, label %83, label %84
83: ; preds = %51
store i32 1, i32* %17
br label %84
84: ; preds = %83, %51
%85 = load i32, i32* %16
%86 = call i32 @close(i32 %85)
%87 = load i32, i32* %15
%88 = call i32 @close(i32 %87)
%89 = load i32, i32* %17
%90 = icmp ne i32 %89, 0
br i1 %90, label %91, label %96
91: ; preds = %84
%92 = load i8**, i8*** %10
%93 = load i64*, i64** %12
%94 = load i64, i64* %93
%95 = call i32 @munmap(i8** %92, i64 %94)
br label %96
96: ; preds = %91, %84
%97 = load i32, i32* %17
store i32 %97, i32* %7
br label %98
98: ; preds = %96, %30, %23
%99 = load i32, i32* %7
ret i32 %99
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@PROT_WRITE = global i32 0
define i32 @map_pair(i8* nocapture %0, i8* nocapture %1, i8** %2, i8** nocapture writeonly %3, i64* nocapture %4, i32 %5) {
%7 = alloca %struct.stat
%8 = bitcast %struct.stat* %7 to i8*
%9 = call i32 @stat(i8* %0, %struct.stat* %7)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %54
11: ; preds = %6
%12 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 1
%13 = load i32, i32* %12
%14 = tail call i32 @S_ISREG(i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %54, label %16
16: ; preds = %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 0
%18 = load i64, i64* %17
store i64 %18, i64* %4
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %19)
%21 = load i64, i64* %4
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @MAP_SHARED
%24 = tail call i64 @mmap(i32* null, i64 %21, i32 %22, i32 %23, i32 %20, i32 0)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %2
%26 = load i64, i64* %4
%27 = sext i32 %5 to i64
%28 = add i64 %26, %27
%29 = load i32, i32* @O_RDWR
%30 = load i32, i32* @O_CREAT
%31 = or i32 %30, %29
%32 = load i32, i32* @O_TRUNC
%33 = or i32 %31, %32
%34 = load i32, i32* @S_IWUSR
%35 = load i32, i32* @S_IRUSR
%36 = or i32 %35, %34
%37 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %33, i32 %36)
%38 = tail call i32 @ftruncate(i32 %37, i64 %28)
%39 = load i32, i32* @PROT_READ
%40 = load i32, i32* @PROT_WRITE
%41 = or i32 %40, %39
%42 = load i32, i32* @MAP_SHARED
%43 = tail call i64 @mmap(i32* null, i64 %28, i32 %41, i32 %42, i32 %37, i32 0)
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %3
%45 = load i8*, i8** @MAP_FAILED
%46 = icmp eq i8* %45, %44
%47 = select i1 %46, i32 1, i32 %38
%48 = tail call i32 @close(i32 %37)
%49 = tail call i32 @close(i32 %20)
%50 = icmp eq i32 %47, 0
br i1 %50, label %54, label %51
51: ; preds = %16
%52 = load i64, i64* %4
%53 = tail call i32 @munmap(i8** %2, i64 %52)
br label %54
54: ; preds = %16, %51, %11, %6
%55 = phi i32 [ %9, %6 ], [ 1, %11 ], [ %47, %51 ], [ 0, %16 ]
ret i32 %55
}