#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 /*<<< orphan*/ abort();
scalar_t__ ftruncate(int, size_t);
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void *remap_shm_shared_copy(void *old_address, size_t old_size, size_t new_size,
                            int fd) {
  if (ftruncate(fd, new_size)) {
    perror("ftruncate");
    abort();
  }
  void *tmp = mmap(NULL, new_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (tmp == MAP_FAILED) {
    perror("mmap");
    abort();
  }
  return tmp;
}


x86 assembly (clang, -O0):
<remap_shm_shared_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <ftruncate@plt>
cmp    $0x0,%rax
je     1180 <remap_shm_shared_copy+0x40>
lea    0xe8c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x18(%rbp),%rsi
mov    0x2e6d(%rip),%rax
mov    (%rax),%edx
mov    0x2e3c(%rip),%rax
or     (%rax),%edx
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%rax
jne    11da <remap_shm_shared_copy+0x9a>
lea    0xe3c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remap_shm_shared_copy>:
push   %rbp
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %ecx,%edi
mov    %rdx,%rsi
call   1050 <ftruncate@plt>
test   %rax,%rax
jne    1195 <remap_shm_shared_copy+0x55>
mov    0x2e9a(%rip),%rax
mov    0x2e6b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e78(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
je     119e <remap_shm_shared_copy+0x5e>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe64(%rip),%rdi
jmp    11a5 <remap_shm_shared_copy+0x65>
lea    0xe65(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

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* @remap_shm_shared_copy(i8* %0, i64 %1, i64 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32 %3, i32* %8
%10 = load i32, i32* %8
%11 = load i64, i64* %7
%12 = call i64 @ftruncate(i32 %10, i64 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %4
%15 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %4
%18 = load i64, i64* %7
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_SHARED
%23 = load i32, i32* %8
%24 = call i8* @mmap(i32* null, i64 %18, i32 %21, i32 %22, i32 %23, i32 0)
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %17
%32 = load i8*, i8** %9
ret i8* %32
}

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* @remap_shm_shared_copy(i8* nocapture readnone %0, i64 %1, i64 %2, i32 %3) {
%5 = tail call i64 @ftruncate(i32 %3, i64 %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 (...) @abort()
unreachable
10: ; preds = %4
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %12, %11
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap(i32* null, i64 %2, i32 %13, i32 %14, i32 %3, i32 0)
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %21
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 (...) @abort()
unreachable
21: ; preds = %10
ret i8* %15
}