#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int fileno(void *);
int ftruncate(int, size_t);
void *mmap(void *, size_t, int, int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int shmem_unmap(void *, size_t);
char *shmem_grow(void *fh, void *old_mem, size_t old_size, size_t new_size) {
  int fd;
  void *mem;
  if (old_mem != NULL) {
    if (shmem_unmap(old_mem, old_size) == false) {
      return (NULL);
    }
  }
  fd = fileno(fh);
  if (ftruncate(fd, new_size) == -1) {
    perror("ftruncate");
    return (NULL);
  }
  if ((mem = mmap(old_mem, new_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                  0)) == MAP_FAILED) {
    perror("mmap");
    return (NULL);
  }
  return ((char *)mem);
}


x86 assembly (clang, -O0):
<shmem_grow>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     119b <shmem_grow+0x4b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <shmem_unmap@plt>
cmp    $0x0,%eax
jne    1196 <shmem_grow+0x46>
movq   $0x0,-0x8(%rbp)
jmp    1239 <shmem_grow+0xe9>
jmp    119b <shmem_grow+0x4b>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
jne    11d5 <shmem_grow+0x85>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1239 <shmem_grow+0xe9>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2e14(%rip),%rax
mov    (%rax),%edx
mov    0x2de3(%rip),%rax
or     (%rax),%edx
mov    0x2df2(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x38(%rbp)
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%rax
jne    1231 <shmem_grow+0xe1>
lea    0xdeb(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1239 <shmem_grow+0xe9>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shmem_grow>:
push   %rbp
push   %r14
push   %rbx
mov    %rcx,%r14
mov    %rsi,%rbx
mov    %rdi,%rbp
test   %rsi,%rsi
je     1171 <shmem_grow+0x21>
mov    %rbx,%rdi
mov    %rdx,%rsi
call   1030 <shmem_unmap@plt>
test   %eax,%eax
je     11d7 <shmem_grow+0x87>
mov    %rbp,%rdi
call   1060 <fileno@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <ftruncate@plt>
cmp    $0xffffffff,%eax
je     11cb <shmem_grow+0x7b>
mov    0x2e67(%rip),%rax
mov    0x2e38(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%rax
jne    11d9 <shmem_grow+0x89>
lea    0xe41(%rip),%rdi
jmp    11d2 <shmem_grow+0x82>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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