#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 */
int MAP_FIXED;
int MAP_SHARED;
void *mmap2(void *, int, int, int, int, int /*<<< orphan*/);
scalar_t__ munmap(void *, int);
int switcheroo(int fd, int prot, void *from, void *to, int size) {
  if (munmap(to, size) < 0) {
    return (-1);
  }
  if (mmap2(to, size, prot, MAP_SHARED | MAP_FIXED, fd, 0) == (void *)-1) {
    return (-1);
  }
  if (munmap(from, size) < 0) {
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<switcheroo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <munmap@plt>
cmp    $0x0,%rax
jge    115c <switcheroo+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <switcheroo+0xa9>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    0x2e83(%rip),%rax
mov    (%rax),%ecx
mov    0x2e62(%rip),%rax
or     (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
call   1030 <mmap2@plt>
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11a0 <switcheroo+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <switcheroo+0xa9>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <munmap@plt>
cmp    $0x0,%rax
jge    11c2 <switcheroo+0xa2>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <switcheroo+0xa9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switcheroo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r13d
mov    %rcx,%rbp
mov    %rdx,%r14
mov    %esi,%r12d
mov    %edi,%r15d
mov    %rcx,%rdi
mov    %r8d,%esi
call   1040 <munmap@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     118d <switcheroo+0x6d>
mov    0x2e9a(%rip),%rax
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %rbp,%rdi
mov    %r13d,%esi
mov    %r12d,%edx
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1030 <mmap2@plt>
cmp    $0xffffffffffffffff,%rax
je     118d <switcheroo+0x6d>
mov    %r14,%rdi
mov    %r13d,%esi
call   1040 <munmap@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAP_SHARED = global i32 0
@MAP_FIXED = global i32 0
define i32 @switcheroo(i32 %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%12 = load i8*, i8** %10
%13 = load i32, i32* %11
%14 = call i64 @munmap(i8* %12, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %5
store i32 -1, i32* %6
br label %35
17: ; preds = %5
%18 = load i8*, i8** %10
%19 = load i32, i32* %11
%20 = load i32, i32* %8
%21 = load i32, i32* @MAP_SHARED
%22 = load i32, i32* @MAP_FIXED
%23 = or i32 %21, %22
%24 = load i32, i32* %7
%25 = call i8* @mmap2(i8* %18, i32 %19, i32 %20, i32 %23, i32 %24, i32 0)
%26 = icmp eq i8* %25, inttoptr (i64 -1 to i8*)
br i1 %26, label %27, label %28
27: ; preds = %17
store i32 -1, i32* %6
br label %35
28: ; preds = %17
%29 = load i8*, i8** %9
%30 = load i32, i32* %11
%31 = call i64 @munmap(i8* %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 -1, i32* %6
br label %35
34: ; preds = %28
store i32 0, i32* %6
br label %35
35: ; preds = %34, %33, %27, %16
%36 = load i32, i32* %6
ret i32 %36
}

LLVM IR (-O2):
@MAP_SHARED = global i32 0
@MAP_FIXED = global i32 0
define i32 @switcheroo(i32 %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = tail call i64 @munmap(i8* %3, i32 %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %5
%9 = load i32, i32* @MAP_SHARED
%10 = load i32, i32* @MAP_FIXED
%11 = or i32 %10, %9
%12 = tail call i8* @mmap2(i8* %3, i32 %4, i32 %1, i32 %11, i32 %0, i32 0)
%13 = icmp eq i8* %12, inttoptr (i64 -1 to i8*)
br i1 %13, label %18, label %14
14: ; preds = %8
%15 = tail call i64 @munmap(i8* %2, i32 %4)
%16 = ashr i64 %15, 63
%17 = trunc i64 %16 to i32
br label %18
18: ; preds = %14, %8, %5
%19 = phi i32 [ -1, %5 ], [ -1, %8 ], [ %17, %14 ]
ret i32 %19
}