#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();
int /*<<< orphan*/ maybe_write_file(char *, char *);
scalar_t__ setgid(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));
  }
  maybe_write_file("/proc/self/setgroups", "deny");
  write_file("/proc/self/uid_map", "0 %d 1", uid);
  write_file("/proc/self/gid_map", "0 %d 1", gid);
  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   1050 <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>
lea    0xe40(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   1040 <maybe_write_file@plt>
mov    -0x4(%rbp),%edx
lea    0xe44(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1060 <write_file@plt>
mov    -0x8(%rbp),%edx
lea    0xe48(%rip),%rdi
lea    0xe3a(%rip),%rsi
call   1060 <write_file@plt>
xor    %edi,%edi
call   1080 <setgid@plt>
cmp    $0x0,%rax
je     1248 <create_and_enter_userns+0xb8>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xe27(%rip),%rdi
call   1090 <die@plt>
xor    %edi,%edi
call   10a0 <setuid@plt>
cmp    $0x0,%rax
je     1275 <create_and_enter_userns+0xe5>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xe0f(%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   1050 <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    0xe58(%rip),%rsi
call   1040 <maybe_write_file@plt>
lea    0xe51(%rip),%rdi
lea    0xe5d(%rip),%rbx
mov    %rbx,%rsi
mov    %ebp,%edx
call   1060 <write_file@plt>
lea    0xe53(%rip),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <write_file@plt>
xor    %edi,%edi
call   1080 <setgid@plt>
test   %rax,%rax
je     1237 <create_and_enter_userns+0xa7>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe3a(%rip),%rdi
mov    %eax,%esi
call   1090 <die@plt>
xor    %edi,%edi
call   10a0 <setuid@plt>
test   %rax,%rax
je     1263 <create_and_enter_userns+0xd3>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe27(%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 = [21 x i8] c"/proc/self/setgroups\00"
@.str.2 = [5 x i8] c"deny\00"
@.str.3 = [19 x i8] c"/proc/self/uid_map\00"
@.str.4 = [7 x i8] c"0 %d 1\00"
@.str.5 = [19 x i8] c"/proc/self/gid_map\00"
@.str.6 = [21 x i8] c"setgid(0) failed %s\0A\00"
@.str.7 = [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 = call i32 @maybe_write_file(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* %1
%14 = 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.4, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %2
%16 = call i32 @write_file(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = call i64 @setgid(i32 0)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %11
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %19, %11
%24 = call i64 @setuid(i32 0)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* @errno
%28 = call i32 @strerror(i32 %27)
%29 = call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %26, %23
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"unshare(CLONE_NEWUSER) failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"/proc/self/setgroups\00"
@.str.2 = [5 x i8] c"deny\00"
@.str.3 = [19 x i8] c"/proc/self/uid_map\00"
@.str.4 = [7 x i8] c"0 %d 1\00"
@.str.5 = [19 x i8] c"/proc/self/gid_map\00"
@.str.6 = [21 x i8] c"setgid(0) failed %s\0A\00"
@.str.7 = [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 @maybe_write_file(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%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.4, i64 0, i64 0), i32 %1)
%12 = tail call i32 @write_file(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %2)
%13 = tail call i64 @setgid(i32 0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = load i32, i32* @errno
%17 = tail call i32 @strerror(i32 %16)
%18 = tail call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %9
%20 = tail call i64 @setuid(i32 0)
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %19
%23 = load i32, i32* @errno
%24 = tail call i32 @strerror(i32 %23)
%25 = tail call i32 @die(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %22, %19
ret void
}