#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*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int ftruncate(int, size_t);
int /*<<< orphan*/ *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/,
                         int, int /*<<< orphan*/);
int munmap(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ perror(char *);
int expand(uint8_t **f, int fd, size_t l, size_t off) {
  if (-1 == ftruncate(fd, l + off)) {
    perror("ftruncate");
    return -1;
  }
  if (-1 == munmap(*f, l)) {
    perror("munmap");
    return -1;
  }
  *f = mmap(NULL, l + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (NULL == *f) {
    perror("mmap");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<expand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
add    -0x28(%rbp),%rsi
call   1040 <ftruncate@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    118e <expand+0x4e>
lea    0xe83(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <expand+0xf0>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <munmap@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11c5 <expand+0x85>
lea    0xe56(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <expand+0xf0>
mov    -0x20(%rbp),%rsi
add    -0x28(%rbp),%rsi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    0x2df3(%rip),%rax
or     (%rax),%edx
mov    0x2e02(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rcx
xor    %eax,%eax
cmp    (%rcx),%rax
jne    1229 <expand+0xe9>
lea    0xdf9(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <expand+0xf0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %rdi,%r14
add    %rdx,%rbx
mov    %esi,%edi
mov    %rbx,%rsi
call   1040 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11b7 <expand+0x77>
mov    (%r14),%rdi
mov    %rbp,%rsi
call   1050 <munmap@plt>
cmp    $0xffffffff,%eax
je     11c0 <expand+0x80>
mov    0x2e7c(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e5a(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     11c9 <expand+0x89>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe42(%rip),%rdi
jmp    11d0 <expand+0x90>
lea    0xe43(%rip),%rdi
jmp    11d0 <expand+0x90>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
jmp    11aa <expand+0x6a>

LLVM IR (-O0):
@.str = [10 x i8] c"ftruncate\00"
@.str.1 = [7 x i8] c"munmap\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
define i32 @expand(i32** %0, i32 %1, i64 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
store i32** %0, i32*** %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%10 = load i32, i32* %7
%11 = load i64, i64* %8
%12 = load i64, i64* %9
%13 = add i64 %11, %12
%14 = call i32 @ftruncate(i32 %10, i64 %13)
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %43
18: ; preds = %4
%19 = load i32**, i32*** %6
%20 = load i32*, i32** %19
%21 = load i64, i64* %8
%22 = call i32 @munmap(i32* %20, i64 %21)
%23 = icmp eq i32 -1, %22
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %43
26: ; preds = %18
%27 = load i64, i64* %8
%28 = load i64, i64* %9
%29 = add i64 %27, %28
%30 = load i32, i32* @PROT_READ
%31 = load i32, i32* @PROT_WRITE
%32 = or i32 %30, %31
%33 = load i32, i32* @MAP_SHARED
%34 = load i32, i32* %7
%35 = call i32* @mmap(i32* null, i64 %29, i32 %32, i32 %33, i32 %34, i32 0)
%36 = load i32**, i32*** %6
store i32* %35, i32** %36
%37 = load i32**, i32*** %6
%38 = load i32*, i32** %37
%39 = icmp eq i32* null, %38
br i1 %39, label %40, label %42
40: ; preds = %26
%41 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %5
br label %43
42: ; preds = %26
store i32 0, i32* %5
br label %43
43: ; preds = %42, %40, %24, %16
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@.str = [10 x i8] c"ftruncate\00"
@.str.1 = [7 x i8] c"munmap\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
define i32 @expand(i32** nocapture %0, i32 %1, i64 %2, i64 %3) {
%5 = add i64 %3, %2
%6 = tail call i32 @ftruncate(i32 %1, i64 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %19, label %8
8: ; preds = %4
%9 = load i32*, i32** %0
%10 = tail call i32 @munmap(i32* %9, i64 %2)
%11 = icmp eq i32 %10, -1
br i1 %11, label %19, label %12
12: ; preds = %8
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_SHARED
%17 = tail call i32* @mmap(i32* null, i64 %5, i32 %15, i32 %16, i32 %1, i32 0)
store i32* %17, i32** %0
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %12, %8, %4
%20 = phi i8* [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %8 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %12 ]
%21 = tail call i32 @perror(i8* %20)
br label %22
22: ; preds = %19, %12
%23 = phi i32 [ 0, %12 ], [ -1, %19 ]
ret i32 %23
}