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

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int getgid();
int getuid();
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int strlen(char *);
scalar_t__ unshare(int);
scalar_t__ write_file(char *, char *, int);
int create_and_enter_userns(void) {
  uid_t uid;
  gid_t gid;
  char map[100];
  uid = getuid();
  gid = getgid();
  if (unshare(0x10000000))
    return -1;
  if (write_file("/proc/self/setgroups", "deny", sizeof("deny") - 1) &&
      errno != ENOENT)
    return -1;
  snprintf(map, sizeof(map), "0 %d 1", uid);
  if (write_file("/proc/self/uid_map", map, strlen(map)))
    return -1;
  snprintf(map, sizeof(map), "0 %d 1", gid);
  if (write_file("/proc/self/gid_map", map, strlen(map)))
    return -1;
  if (setgid(0))
    return -1;
  if (setuid(0))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<create_and_enter_userns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1080 <getgid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x10000000,%edi
call   1030 <unshare@plt>
cmp    $0x0,%rax
je     11bf <create_and_enter_userns+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <create_and_enter_userns+0x16a>
lea    0xe3a(%rip),%rdi
lea    0xe48(%rip),%rsi
mov    $0x4,%edx
call   1060 <write_file@plt>
cmp    $0x0,%rax
je     1207 <create_and_enter_userns+0x87>
mov    0x2df8(%rip),%rax
mov    (%rax),%rax
mov    0x2dde(%rip),%rcx
cmp    (%rcx),%rax
je     1207 <create_and_enter_userns+0x87>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <create_and_enter_userns+0x16a>
lea    -0x70(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x64,%esi
lea    0xe00(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x78(%rbp),%rsi
mov    %eax,%edx
lea    0xde4(%rip),%rdi
call   1060 <write_file@plt>
cmp    $0x0,%rax
je     1258 <create_and_enter_userns+0xd8>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <create_and_enter_userns+0x16a>
lea    -0x70(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x64,%esi
lea    0xdaf(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
lea    0xda6(%rip),%rdi
call   1060 <write_file@plt>
cmp    $0x0,%rax
je     12a9 <create_and_enter_userns+0x129>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <create_and_enter_userns+0x16a>
xor    %edi,%edi
call   1090 <setgid@plt>
cmp    $0x0,%rax
je     12c6 <create_and_enter_userns+0x146>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <create_and_enter_userns+0x16a>
xor    %edi,%edi
call   10a0 <setuid@plt>
cmp    $0x0,%rax
je     12e3 <create_and_enter_userns+0x163>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ea <create_and_enter_userns+0x16a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_and_enter_userns>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <getgid@plt>
mov    %eax,%r14d
mov    $0x10000000,%edi
call   1030 <unshare@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    1273 <create_and_enter_userns+0xf3>
lea    0xe44(%rip),%rdi
lea    0xe52(%rip),%rsi
mov    $0x4,%edx
call   1060 <write_file@plt>
test   %rax,%rax
je     11ec <create_and_enter_userns+0x6c>
mov    0x2e07(%rip),%rax
mov    (%rax),%rax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%rax
jne    1273 <create_and_enter_userns+0xf3>
lea    0xe27(%rip),%rdx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x64,%esi
mov    %ebp,%ecx
call   1070 <snprintf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0xe0d(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1060 <write_file@plt>
test   %rax,%rax
jne    1273 <create_and_enter_userns+0xf3>
lea    0xdf0(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x64,%esi
mov    %r14d,%ecx
call   1070 <snprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    0xde8(%rip),%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1060 <write_file@plt>
test   %rax,%rax
jne    1273 <create_and_enter_userns+0xf3>
xor    %edi,%edi
call   1090 <setgid@plt>
test   %rax,%rax
jne    1273 <create_and_enter_userns+0xf3>
xor    %edi,%edi
call   10a0 <setuid@plt>
neg    %rax
sbb    %ebx,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/proc/self/setgroups\00"
@.str.1 = [5 x i8] c"deny\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [7 x i8] c"0 %d 1\00"
@.str.3 = [19 x i8] c"/proc/self/uid_map\00"
@.str.4 = [19 x i8] c"/proc/self/gid_map\00"
define i32 @create_and_enter_userns() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
%5 = call i32 (...) @getuid()
store i32 %5, i32* %2
%6 = call i32 (...) @getgid()
store i32 %6, i32* %3
%7 = call i64 @unshare(i32 268435456)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %47
10: ; preds = %0
%11 = call i64 @write_file(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 -1, i32* %1
br label %47
18: ; preds = %13, %10
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%20 = load i32, i32* %2
%21 = call i32 @snprintf(i8* %19, i32 100, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%24 = call i32 @strlen(i8* %23)
%25 = call i64 @write_file(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %22, i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %18
store i32 -1, i32* %1
br label %47
28: ; preds = %18
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%30 = load i32, i32* %3
%31 = call i32 @snprintf(i8* %29, i32 100, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = call i64 @write_file(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %32, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %28
store i32 -1, i32* %1
br label %47
38: ; preds = %28
%39 = call i64 @setgid(i32 0)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 -1, i32* %1
br label %47
42: ; preds = %38
%43 = call i64 @setuid(i32 0)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %42
store i32 -1, i32* %1
br label %47
46: ; preds = %42
store i32 0, i32* %1
br label %47
47: ; preds = %46, %45, %41, %37, %27, %17, %9
%48 = load i32, i32* %1
ret i32 %48
}

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