#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 */
scalar_t__ ENOENT;
int WriteFile(char *, char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int setresgid(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int setresuid(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void SetupUserNamespace(int uid, int gid) {
  int r = WriteFile("/proc/self/setgroups", "deny");
  if (r < 0 && errno != ENOENT) {
    perror("WriteFile(\"/proc/self/setgroups\", \"deny\")");
    exit(-1);
  }
  if ((WriteFile("/proc/self/uid_map", "0 %d 1\n", uid)) == -1) {
    perror("WriteFile(\"/proc/self/uid_map\", \"0 %d 1\\n\", uid)");
    exit(1);
  };
  if ((WriteFile("/proc/self/gid_map", "0 %d 1\n", gid)) == -1) {
    perror("WriteFile(\"/proc/self/gid_map\", \"0 %d 1\\n\", gid)");
    exit(1);
  };
  if ((setresuid(0, 0, 0)) == -1) {
    perror("setresuid(0, 0, 0)");
    exit(1);
  };
  if ((setresgid(0, 0, 0)) == -1) {
    perror("setresgid(0, 0, 0)");
    exit(1);
  };
}


x86 assembly (clang, -O0):
<SetupUserNamespace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    0xe9b(%rip),%rdi
lea    0xea9(%rip),%rsi
mov    $0x0,%al
call   1050 <WriteFile@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11b0 <SetupUserNamespace+0x60>
mov    0x2e59(%rip),%rax
mov    (%rax),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <SetupUserNamespace+0x60>
lea    0xe79(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%edx
lea    0xe8a(%rip),%rdi
lea    0xe96(%rip),%rsi
mov    $0x0,%al
call   1050 <WriteFile@plt>
cmp    $0xffffffff,%eax
jne    11e7 <SetupUserNamespace+0x97>
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edx
lea    0xe9f(%rip),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1050 <WriteFile@plt>
cmp    $0xffffffff,%eax
jne    121e <SetupUserNamespace+0xce>
lea    0xe94(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edx,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1030 <setresuid@plt>
cmp    $0xffffffff,%eax
jne    1248 <SetupUserNamespace+0xf8>
lea    0xe9b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
xor    %edx,%edx
mov    %edx,%edi
mov    %edx,%esi
call   1040 <setresgid@plt>
cmp    $0xffffffff,%eax
jne    1272 <SetupUserNamespace+0x122>
lea    0xe84(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SetupUserNamespace>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xea2(%rip),%rdi
lea    0xeb0(%rip),%rsi
xor    %eax,%eax
call   1050 <WriteFile@plt>
test   %eax,%eax
jns    118a <SetupUserNamespace+0x3a>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e4f(%rip),%rcx
cmp    (%rcx),%rax
jne    121a <SetupUserNamespace+0xca>
lea    0xeb3(%rip),%rdi
lea    0xebf(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <WriteFile@plt>
cmp    $0xffffffff,%eax
je     11e9 <SetupUserNamespace+0x99>
lea    0xee3(%rip),%rdi
lea    0xea3(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <WriteFile@plt>
cmp    $0xffffffff,%eax
je     11f2 <SetupUserNamespace+0xa2>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <setresuid@plt>
cmp    $0xffffffff,%eax
je     11fb <SetupUserNamespace+0xab>
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <setresgid@plt>
cmp    $0xffffffff,%eax
je     1204 <SetupUserNamespace+0xb4>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe6f(%rip),%rdi
jmp    120b <SetupUserNamespace+0xbb>
lea    0xeaa(%rip),%rdi
jmp    120b <SetupUserNamespace+0xbb>
lea    0xed2(%rip),%rdi
jmp    120b <SetupUserNamespace+0xbb>
lea    0xedc(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdf9(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

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 = [42 x i8] c"WriteFile(\22/proc/self/setgroups\22, \22deny\22)\00"
@.str.3 = [19 x i8] c"/proc/self/uid_map\00"
@.str.4 = [8 x i8] c"0 %d 1\0A\00"
@.str.5 = [49 x i8] c"WriteFile(\22/proc/self/uid_map\22, \220 %d 1\\n\22, uid)\00"
@.str.6 = [19 x i8] c"/proc/self/gid_map\00"
@.str.7 = [49 x i8] c"WriteFile(\22/proc/self/gid_map\22, \220 %d 1\\n\22, gid)\00"
@.str.8 = [19 x i8] c"setresuid(0, 0, 0)\00"
@.str.9 = [19 x i8] c"setresgid(0, 0, 0)\00"
define void @SetupUserNamespace(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = call i32 (i8*, i8*, ...) @WriteFile(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))
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp ne i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %9, %2
%17 = load i32, i32* %3
%18 = call i32 (i8*, i8*, ...) @WriteFile(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = load i32, i32* %4
%25 = call i32 (i8*, i8*, ...) @WriteFile(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = call i32 @setresuid(i32 0, i32 0, i32 0)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %30
%37 = call i32 @setresgid(i32 0, i32 0, i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %36
ret void
}

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 = [42 x i8] c"WriteFile(\22/proc/self/setgroups\22, \22deny\22)\00"
@.str.3 = [19 x i8] c"/proc/self/uid_map\00"
@.str.4 = [8 x i8] c"0 %d 1\0A\00"
@.str.5 = [49 x i8] c"WriteFile(\22/proc/self/uid_map\22, \220 %d 1\\n\22, uid)\00"
@.str.6 = [19 x i8] c"/proc/self/gid_map\00"
@.str.7 = [49 x i8] c"WriteFile(\22/proc/self/gid_map\22, \220 %d 1\\n\22, gid)\00"
@.str.8 = [19 x i8] c"setresuid(0, 0, 0)\00"
@.str.9 = [19 x i8] c"setresgid(0, 0, 0)\00"
define void @SetupUserNamespace(i32 %0, i32 %1) {
%3 = tail call i32 (i8*, i8*, ...) @WriteFile(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))
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp eq i64 %6, %7
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %5, %2
%13 = tail call i32 (i8*, i8*, ...) @WriteFile(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.5, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = tail call i32 (i8*, i8*, ...) @WriteFile(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i32 %1)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %18
%25 = tail call i32 @setresuid(i32 0, i32 0, i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.8, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %24
%31 = tail call i32 @setresgid(i32 0, i32 0, i32 0)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0))
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %30
ret void
}