#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
int open(char const *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int check_unpriv_userns(int euid) {
  char buf[32];
  const char *procf = "/proc/sys/kernel/unprivileged_userns_clone";
  int fd = open(procf, O_RDONLY);
  if (fd < 0)
    error(1, errno, "can't open %s", procf);
  if (read(fd, buf, sizeof buf) < 0)
    error(1, errno, "i/o error while reading %s", procf);
  close(fd);
  if (buf[0] == '1')
    return 0;
  error(1, 0,
        "Unprivileged user (uid %d) can't create user namespace.\n"
        "   %s  is 0",
        euid, procf);
  return 0;
}


x86 assembly (clang, -O0):
<check_unpriv_userns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    0xeae(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1195 <check_unpriv_userns+0x55>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edi
lea    0xe9d(%rip),%rdx
mov    $0x0,%al
call   1050 <error@plt>
mov    -0x3c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x20,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jge    11d0 <check_unpriv_userns+0x90>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edi
lea    0xe70(%rip),%rdx
mov    $0x0,%al
call   1050 <error@plt>
mov    -0x3c(%rbp),%edi
call   1030 <close@plt>
movsbl -0x30(%rbp),%eax
cmp    $0x31,%eax
jne    11f1 <check_unpriv_userns+0xb1>
movl   $0x0,-0x4(%rbp)
jmp    1214 <check_unpriv_userns+0xd4>
mov    -0x8(%rbp),%ecx
mov    -0x38(%rbp),%r8
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe4e(%rip),%rdx
mov    $0x0,%al
call   1050 <error@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_unpriv_userns>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1186 <check_unpriv_userns+0x46>
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdx
lea    0xe86(%rip),%rcx
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <error@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x20,%edx
call   1040 <read@plt>
test   %rax,%rax
jns    11bd <check_unpriv_userns+0x7d>
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdx
lea    0xe4f(%rip),%rcx
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <error@plt>
mov    %ebp,%edi
call   1030 <close@plt>
cmpb   $0x31,(%rsp)
je     11e8 <check_unpriv_userns+0xa8>
lea    0xe83(%rip),%rdx
lea    0xe28(%rip),%r8
mov    $0x1,%edi
xor    %esi,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <error@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"/proc/sys/kernel/unprivileged_userns_clone\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [14 x i8] c"can't open %s\00"
@.str.2 = [27 x i8] c"i/o error while reading %s\00"
@.str.3 = [68 x i8] c"Unprivileged user (uid %d) can't create user namespace.\0A %s is 0\00"
define i32 @check_unpriv_userns(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [32 x i8]
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
store i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8** %5
%7 = load i8*, i8** %5
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i32, i32* @errno
%14 = load i8*, i8** %5
%15 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %13, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %12, %1
%17 = load i32, i32* %6
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%19 = call i64 @read(i32 %17, i8* %18, i32 32)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = load i8*, i8** %5
%24 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %21, %16
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 49
br i1 %31, label %32, label %33
32: ; preds = %25
store i32 0, i32* %2
br label %37
33: ; preds = %25
%34 = load i32, i32* %3
%35 = load i8*, i8** %5
%36 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.3, i64 0, i64 0), i32 %34, i8* %35)
store i32 0, i32* %2
br label %37
37: ; preds = %33, %32
%38 = load i32, i32* %2
ret i32 %38
}

LLVM IR (-O2):
@.str = [43 x i8] c"/proc/sys/kernel/unprivileged_userns_clone\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [14 x i8] c"can't open %s\00"
@.str.2 = [27 x i8] c"i/o error while reading %s\00"
@.str.3 = [68 x i8] c"Unprivileged user (uid %d) can't create user namespace.\0A %s is 0\00"
define i32 @check_unpriv_userns(i32 %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %7, %1
%11 = call i64 @read(i32 %5, i8* %3, i32 32)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @errno
%15 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %13, %10
%17 = tail call i32 @close(i32 %5)
%18 = load i8, i8* %3
%19 = icmp eq i8 %18, 49
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.3, i64 0, i64 0), i32 %0, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %16, %20
ret i32 0
}