#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_ANONYMOUS;
void *MAP_FAILED;
int MAP_FIXED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int errno;
void *mmap(int, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ printf(char *, void *);
int /*<<< orphan*/ setgid(int);
int /*<<< orphan*/ setuid(int);
int main(int argc, char *argv[]) {
  int rc;
  void *mem;
  setuid(99);
  setgid(99);
  mem = mmap(0x0, 4096, PROT_READ | PROT_WRITE,
             MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  if (mem == MAP_FAILED)
    return errno;
  printf("mem = %p\n", mem);
  munmap(mem, 4096);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x63,%edi
call   1070 <setuid@plt>
mov    $0x63,%edi
call   1060 <setgid@plt>
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
mov    0x2e3e(%rip),%rax
or     (%rax),%edx
mov    0x2e2d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e44(%rip),%rax
or     (%rax),%ecx
mov    0x2e13(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%rax
jne    11e6 <main+0x96>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    120b <main+0xbb>
mov    -0x20(%rbp),%rsi
lea    0xe0f(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    $0x63,%edi
call   1070 <setuid@plt>
mov    $0x63,%edi
call   1060 <setgid@plt>
mov    0x2e8a(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e40(%rip),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e26(%rip),%rax
or     (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dfb(%rip),%rax
cmp    (%rax),%rbx
je     11d8 <main+0x88>
lea    0xe3f(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1050 <munmap@plt>
jmp    11e1 <main+0x91>
mov    0x2df1(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FIXED = global i32 0
@MAP_FAILED = global i8* null
@errno = global i32 0
@.str = [10 x i8] c"mem = %p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = call i32 @setuid(i32 99)
%9 = call i32 @setgid(i32 99)
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_PRIVATE
%14 = load i32, i32* @MAP_ANONYMOUS
%15 = or i32 %13, %14
%16 = load i32, i32* @MAP_FIXED
%17 = or i32 %15, %16
%18 = call i8* @mmap(i32 0, i32 4096, i32 %12, i32 %17, i32 -1, i32 0)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = load i32, i32* @errno
store i32 %23, i32* %3
br label %29
24: ; preds = %2
%25 = load i8*, i8** %7
%26 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %7
%28 = call i32 @munmap(i8* %27, i32 4096)
store i32 0, i32* %3
br label %29
29: ; preds = %24, %22
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FIXED = global i32 0
@MAP_FAILED = global i8* null
@errno = global i32 0
@.str = [10 x i8] c"mem = %p\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @setuid(i32 99)
%4 = tail call i32 @setgid(i32 99)
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_PRIVATE
%9 = load i32, i32* @MAP_ANONYMOUS
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_FIXED
%12 = or i32 %10, %11
%13 = tail call i8* @mmap(i32 0, i32 4096, i32 %7, i32 %12, i32 -1, i32 0)
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* @errno
br label %21
18: ; preds = %2
%19 = tail call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %13)
%20 = tail call i32 @munmap(i8* %13, i32 4096)
br label %21
21: ; preds = %18, %16
%22 = phi i32 [ %17, %16 ], [ 0, %18 ]
ret i32 %22
}