#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 */
typedef int /*<<< orphan*/ uid_t;
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
int /*<<< orphan*/ die(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ getgid();
int /*<<< orphan*/ getuid();
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setgroups(int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
scalar_t__ unshare(int);
int /*<<< orphan*/ write_file(char *, char *, int /*<<< orphan*/);
void create_and_enter_userns(void) {
  uid_t uid;
  gid_t gid;
  uid = getuid();
  gid = getgid();
  if (unshare(0x10000000) != 0) {
    die("unshare(CLONE_NEWUSER) failed: %s\n", strerror(errno));
  }
  write_file("/proc/self/uid_map", "0 %d 1", uid);
  write_file("/proc/self/gid_map", "0 %d 1", gid);
  if (setgroups(0, NULL) != 0) {
    die("setgroups failed: %s\n", strerror(errno));
  }
  if (setgid(0) != 0) {
    die("setgid(0) failed %s\n", strerror(errno));
  }
  if (setuid(0) != 0) {
    die("setuid(0) failed %s\n", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<create_and_enter_userns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   1070 <getgid@plt>
mov    %eax,-0x8(%rbp)
mov    $0x10000000,%edi
call   1030 <unshare@plt>
cmp    $0x0,%rax
je     11dc <create_and_enter_userns+0x4c>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xe29(%rip),%rdi
call   1090 <die@plt>
mov    -0x4(%rbp),%edx
lea    0xe3d(%rip),%rdi
lea    0xe49(%rip),%rsi
call   1050 <write_file@plt>
mov    -0x8(%rbp),%edx
lea    0xe41(%rip),%rdi
lea    0xe33(%rip),%rsi
call   1050 <write_file@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <setgroups@plt>
cmp    $0x0,%rax
je     1239 <create_and_enter_userns+0xa9>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xe1c(%rip),%rdi
call   1090 <die@plt>
xor    %edi,%edi
call   1080 <setgid@plt>
cmp    $0x0,%rax
je     1266 <create_and_enter_userns+0xd6>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xe05(%rip),%rdi
call   1090 <die@plt>
xor    %edi,%edi
call   10a0 <setuid@plt>
cmp    $0x0,%rax
je     1293 <create_and_enter_userns+0x103>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xded(%rip),%rdi
call   1090 <die@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_and_enter_userns>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <getgid@plt>
mov    %eax,%r14d
mov    $0x10000000,%edi
call   1030 <unshare@plt>
test   %rax,%rax
je     11d2 <create_and_enter_userns+0x42>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe35(%rip),%rdi
mov    %eax,%esi
call   1090 <die@plt>
lea    0xe4a(%rip),%rdi
lea    0xe56(%rip),%rbx
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <write_file@plt>
lea    0xe4c(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <write_file@plt>
xor    %edi,%edi
xor    %esi,%esi
call   1060 <setgroups@plt>
test   %rax,%rax
je     1226 <create_and_enter_userns+0x96>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe31(%rip),%rdi
mov    %eax,%esi
call   1090 <die@plt>
xor    %edi,%edi
call   1080 <setgid@plt>
test   %rax,%rax
je     124e <create_and_enter_userns+0xbe>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe1f(%rip),%rdi
mov    %eax,%esi
call   1090 <die@plt>
xor    %edi,%edi
call   10a0 <setuid@plt>
test   %rax,%rax
je     127a <create_and_enter_userns+0xea>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe0c(%rip),%rdi
mov    %eax,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1090 <die@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"unshare(CLONE_NEWUSER) failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [19 x i8] c"/proc/self/uid_map\00"
@.str.2 = [7 x i8] c"0 %d 1\00"
@.str.3 = [19 x i8] c"/proc/self/gid_map\00"
@.str.4 = [22 x i8] c"setgroups failed: %s\0A\00"
@.str.5 = [21 x i8] c"setgid(0) failed %s\0A\00"
@.str.6 = [21 x i8] c"setuid(0) failed %s\0A\00"
define void @create_and_enter_userns() {
%1 = alloca i32
%2 = alloca i32
%3 = call i32 (...) @getuid()
store i32 %3, i32* %1
%4 = call i32 (...) @getgid()
store i32 %4, i32* %2
%5 = call i64 @unshare(i32 268435456)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = load i32, i32* @errno
%9 = call i32 @strerror(i32 %8)
%10 = call i32 @die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %7, %0
%12 = load i32, i32* %1
%13 = call i32 @write_file(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %2
%15 = call i32 @write_file(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = call i64 @setgroups(i32 0, i32* null)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %11
%23 = call i64 @setgid(i32 0)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25, %22
%30 = call i64 @setuid(i32 0)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* @errno
%34 = call i32 @strerror(i32 %33)
%35 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %32, %29
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"unshare(CLONE_NEWUSER) failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [19 x i8] c"/proc/self/uid_map\00"
@.str.2 = [7 x i8] c"0 %d 1\00"
@.str.3 = [19 x i8] c"/proc/self/gid_map\00"
@.str.4 = [22 x i8] c"setgroups failed: %s\0A\00"
@.str.5 = [21 x i8] c"setgid(0) failed %s\0A\00"
@.str.6 = [21 x i8] c"setuid(0) failed %s\0A\00"
define void @create_and_enter_userns() {
%1 = tail call i32 (...) @getuid()
%2 = tail call i32 (...) @getgid()
%3 = tail call i64 @unshare(i32 268435456)
%4 = icmp eq i64 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %0
%6 = load i32, i32* @errno
%7 = tail call i32 @strerror(i32 %6)
%8 = tail call i32 @die(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %7)
br label %9
9: ; preds = %5, %0
%10 = tail call i32 @write_file(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%11 = tail call i32 @write_file(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%12 = tail call i64 @setgroups(i32 0, i32* null)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %9
%15 = load i32, i32* @errno
%16 = tail call i32 @strerror(i32 %15)
%17 = tail call i32 @die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %14, %9
%19 = tail call i64 @setgid(i32 0)
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = load i32, i32* @errno
%23 = tail call i32 @strerror(i32 %22)
%24 = tail call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %21, %18
%26 = tail call i64 @setuid(i32 0)
%27 = icmp eq i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %25
%29 = load i32, i32* @errno
%30 = tail call i32 @strerror(i32 %29)
%31 = tail call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %28, %25
ret void
}