#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*/ pid_t;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ __NR_setns;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ die(int /*<<< orphan*/, char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
char *string(char *, int /*<<< orphan*/, char *);
scalar_t__ syscall(int /*<<< orphan*/, int, int /*<<< orphan*/);
void join(pid_t pid, char *type) {
  char *path;
  int fd;
  path = string("/proc/%u/ns/%s", pid, type);
  if ((fd = open(path, O_RDONLY)) >= 0) {
    if (syscall(__NR_setns, fd, 0) < 0 && strcmp(type, "user") == 0)
      die(0, "Failed to join user namespace");
    close(fd);
  } else if (errno != ENOENT) {
    die(0, "PID %u does not belong to you", pid);
  } else if (strcmp(type, "user") == 0) {
    die(0, "PID %u not found or user namespace unavailable", pid);
  }
  free(path);
}


x86 assembly (clang, -O0):
<join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xe73(%rip),%rdi
call   1040 <string@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jl     1208 <join+0x98>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%esi
xor    %edx,%edx
call   1070 <syscall@plt>
cmp    $0x0,%rax
jge    11fb <join+0x8b>
mov    -0x10(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11fb <join+0x8b>
xor    %edi,%edi
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1090 <die@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
jmp    1271 <join+0x101>
mov    0x2dc9(%rip),%rax
mov    (%rax),%rax
mov    0x2da7(%rip),%rcx
cmp    (%rcx),%rax
je     123a <join+0xca>
mov    -0x4(%rbp),%edx
xor    %edi,%edi
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1090 <die@plt>
jmp    126c <join+0xfc>
mov    -0x10(%rbp),%rdi
lea    0xdca(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1267 <join+0xf7>
mov    -0x4(%rbp),%edx
xor    %edi,%edi
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1090 <die@plt>
jmp    126c <join+0xfc>
jmp    1271 <join+0x101>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<join>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0xe7c(%rip),%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
call   1040 <string@plt>
mov    %rax,%r14
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11ed <join+0x7d>
mov    %eax,%ebp
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
call   1070 <syscall@plt>
test   %rax,%rax
jns    11e4 <join+0x74>
lea    0xe48(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11e4 <join+0x74>
lea    0xe39(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1090 <die@plt>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1233 <join+0xc3>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <join+0xb0>
lea    0xe05(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1233 <join+0xc3>
lea    0xe32(%rip),%rsi
jmp    1227 <join+0xb7>
lea    0xe0b(%rip),%rsi
xor    %edi,%edi
mov    %r15d,%edx
xor    %eax,%eax
call   1090 <die@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/%u/ns/%s\00"
@O_RDONLY = global i32 0
@__NR_setns = global i32 0
@.str.1 = [5 x i8] c"user\00"
@.str.2 = [30 x i8] c"Failed to join user namespace\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.3 = [30 x i8] c"PID %u does not belong to you\00"
@.str.4 = [47 x i8] c"PID %u not found or user namespace unavailable\00"
define void @join(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = load i32, i32* %3
%8 = load i8*, i8** %4
%9 = call i8* @string(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %7, i8* %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %6
%13 = icmp sge i32 %12, 0
br i1 %13, label %14, label %28
14: ; preds = %2
%15 = load i32, i32* @__NR_setns
%16 = load i32, i32* %6
%17 = call i64 @syscall(i32 %15, i32 %16, i32 0)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 (i32, i8*, ...) @die(i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %19, %14
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
br label %44
28: ; preds = %2
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOENT
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* %3
%34 = call i32 (i32, i8*, ...) @die(i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %33)
br label %43
35: ; preds = %28
%36 = load i8*, i8** %4
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32, i32* %3
%41 = call i32 (i32, i8*, ...) @die(i32 0, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %39, %35
br label %43
43: ; preds = %42, %32
br label %44
44: ; preds = %43, %25
%45 = load i8*, i8** %5
%46 = call i32 @free(i8* %45)
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%u/ns/%s\00"
@O_RDONLY = global i32 0
@__NR_setns = global i32 0
@.str.1 = [5 x i8] c"user\00"
@.str.2 = [30 x i8] c"Failed to join user namespace\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.3 = [30 x i8] c"PID %u does not belong to you\00"
@.str.4 = [47 x i8] c"PID %u not found or user namespace unavailable\00"
define void @join(i32 %0, i8* %1) {
%3 = tail call i8* @string(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %3, i32 %4)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %18
7: ; preds = %2
%8 = load i32, i32* @__NR_setns
%9 = tail call i64 @syscall(i32 %8, i32 %5, i32 0)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %7
%12 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 (i32, i8*, ...) @die(i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %11, %7
%17 = tail call i32 @close(i32 %5)
br label %29
18: ; preds = %2
%19 = load i64, i64* @errno
%20 = load i64, i64* @ENOENT
%21 = icmp eq i64 %19, %20
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = tail call i32 (i32, i8*, ...) @die(i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %29
24: ; preds = %18
%25 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 (i32, i8*, ...) @die(i32 0, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.4, i64 0, i64 0), i32 %0)
br label %29
29: ; preds = %22, %27, %24, %16
%30 = tail call i32 @free(i8* %3)
ret void
}