#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 *mmap(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 (mmap(to, size, prot, MAP_SHARED | MAP_FIXED, fd, 0) != to) {
    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    11c3 <switcheroo+0xa3>
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 <mmap@plt>
cmp    -0x20(%rbp),%rax
je     119a <switcheroo+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <switcheroo+0xa3>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <munmap@plt>
cmp    $0x0,%rax
jge    11bc <switcheroo+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <switcheroo+0xa3>
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,%r15d
mov    %rcx,%rbp
mov    %rdx,%r14
mov    %esi,%r13d
mov    %edi,%r12d
mov    %rcx,%rdi
mov    %r8d,%esi
call   1040 <munmap@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     118c <switcheroo+0x6c>
mov    0x2e9a(%rip),%rax
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r13d,%edx
mov    %r12d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    %rbp,%rax
jne    118c <switcheroo+0x6c>
mov    %r14,%rdi
mov    %r15d,%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 %36
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* @mmap(i8* %18, i32 %19, i32 %20, i32 %23, i32 %24, i32 0)
%26 = load i8*, i8** %10
%27 = icmp ne i8* %25, %26
br i1 %27, label %28, label %29
28: ; preds = %17
store i32 -1, i32* %6
br label %36
29: ; preds = %17
%30 = load i8*, i8** %9
%31 = load i32, i32* %11
%32 = call i64 @munmap(i8* %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 -1, i32* %6
br label %36
35: ; preds = %29
store i32 0, i32* %6
br label %36
36: ; preds = %35, %34, %28, %16
%37 = load i32, i32* %6
ret i32 %37
}

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* @mmap(i8* %3, i32 %4, i32 %1, i32 %11, i32 %0, i32 0)
%13 = icmp eq i8* %12, %3
br i1 %13, label %14, label %18
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
}