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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ __NR_setns;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
scalar_t__ strcmp(char *, char *);
char *string(char *, int, 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 (errno == ENOENT)
      error(1, 0, "PID %u not found", pid);
    error(1, 0, "PID %u does not belong to you", pid);
  }
  if (syscall(__NR_setns, fd, 0) < 0 && strcmp(type, "user") == 0)
    error(1, 0, "Failed to join user namespace");
  close(fd);
  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   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11fe <join+0x8e>
mov    0x2e1d(%rip),%rax
mov    (%rax),%rax
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%rax
jne    11e6 <join+0x76>
mov    -0x4(%rbp),%ecx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe30(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x4(%rbp),%ecx
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe29(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%esi
xor    %edx,%edx
call   1070 <syscall@plt>
cmp    $0x0,%rax
jge    124a <join+0xda>
mov    -0x10(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    124a <join+0xda>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe00(%rip),%rdx
mov    $0x0,%al
call   1080 <error@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
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,%r14
mov    %edi,%ebx
lea    0xe7d(%rip),%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   1040 <string@plt>
mov    %rax,%r15
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11eb <join+0x7b>
mov    0x2e2a(%rip),%rax
mov    (%rax),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
jne    11d4 <join+0x64>
lea    0xe4b(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1080 <error@plt>
lea    0xe45(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1080 <error@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
call   1070 <syscall@plt>
test   %rax,%rax
jns    122b <join+0xbb>
lea    0xe35(%rip),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    122b <join+0xbb>
lea    0xe26(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
xor    %eax,%eax
call   1080 <error@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15,%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
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [17 x i8] c"PID %u not found\00"
@.str.2 = [30 x i8] c"PID %u does not belong to you\00"
@__NR_setns = global i32 0
@.str.3 = [5 x i8] c"user\00"
@.str.4 = [30 x i8] c"Failed to join user namespace\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 slt i32 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %2
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOENT
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %3
%20 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %18, %14
%22 = load i32, i32* %3
%23 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %21, %2
%25 = load i32, i32* @__NR_setns
%26 = load i32, i32* %6
%27 = call i64 @syscall(i32 %25, i32 %26, i32 0)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %24
%30 = load i8*, i8** %4
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 0, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %29, %24
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
ret void
}

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