#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 */
struct stat {
  int dummy;
};
typedef int pid_t;
typedef int /*<<< orphan*/ namespaces;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AT_SYMLINK_NOFOLLOW;
scalar_t__ ENOENT;
int O_DIRECTORY;
int O_RDONLY;
int PATH_MAX;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fstatat(int, char *, struct stat *, int /*<<< orphan*/);
char *getenv(char *);
int open(char *, int);
int openat(int, char *, int);
int setns(int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
void joinmnt() {
  char *namespaces[] = {"mnt"};
  const int num = sizeof(namespaces) / sizeof(char *);
  char buf[PATH_MAX], *val;
  int i, tfd;
  pid_t docker_pid;
  val = getenv("_DOCKER_PID");
  if (val == NULL)
    return;
  docker_pid = atoi(val);
  snprintf(buf, sizeof(buf), "%d", docker_pid);
  if (strcmp(val, buf)) {
    fprintf(stderr, "nsenter: "
                    "Unable to parse _DOCKER_PID"
                    ": %m\n");
    exit(1);
  }
  snprintf(buf, PATH_MAX - 1, "/proc/%d/ns", docker_pid);
  tfd = open(buf, O_DIRECTORY | O_RDONLY);
  if (tfd == -1) {
    fprintf(stderr,
            "nsenter: "
            "Failed to open \"%s\""
            ": %m\n",
            buf);
    exit(1);
  }
  for (i = 0; i < num; i++) {
    struct stat st;
    int fd;
    if (fstatat(tfd, namespaces[i], &st, AT_SYMLINK_NOFOLLOW) == -1) {
      if (errno == ENOENT)
        continue;
    }
    fd = openat(tfd, namespaces[i], O_RDONLY);
    if (fd == -1) {
      fprintf(stderr,
              "nsenter: "
              "Failed to open ns file %s for ns %s"
              ": %m\n",
              buf, namespaces[i]);
      exit(1);
    }
    if (setns(fd, 0) == -1) {
      fprintf(stderr,
              "nsenter: "
              "Failed to setns for %s"
              ": %m\n",
              namespaces[i]);
      exit(1);
    }
    close(fd);
  }
}


x86 assembly (clang, -O0):
<joinmnt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x2c21(%rip),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    0xe03(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1221 <joinmnt+0x71>
movl   $0x1,-0x38(%rbp)
jmp    13f7 <joinmnt+0x247>
mov    -0x28(%rbp),%rdi
call   10b0 <atoi@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
mov    %ecx,-0x34(%rbp)
mov    %eax,%esi
mov    -0x34(%rbp),%ecx
lea    0xdcd(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x50(%rbp),%rsi
mov    -0x28(%rbp),%rdi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1280 <joinmnt+0xd0>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x50(%rbp),%rdi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
sub    $0x1,%esi
mov    -0x34(%rbp),%ecx
lea    0xda3(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x50(%rbp),%rdi
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
mov    0x2d2d(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    12ec <joinmnt+0x13c>
mov    -0x50(%rbp),%rdx
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
lea    0xd6e(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jge    13f0 <joinmnt+0x240>
mov    -0x30(%rbp),%edi
movslq -0x2c(%rbp),%rax
mov    -0x8(%rbp,%rax,8),%rsi
mov    0x2ca8(%rip),%rax
mov    (%rax),%ecx
lea    -0x40(%rbp),%rdx
call   10d0 <fstatat@plt>
cmp    $0xffffffff,%eax
jne    1348 <joinmnt+0x198>
mov    0x2c95(%rip),%rax
mov    (%rax),%rax
mov    0x2c73(%rip),%rcx
cmp    (%rcx),%rax
jne    1343 <joinmnt+0x193>
jmp    13e2 <joinmnt+0x232>
jmp    1348 <joinmnt+0x198>
mov    -0x30(%rbp),%edi
movslq -0x2c(%rbp),%rax
mov    -0x8(%rbp,%rax,8),%rsi
mov    0x2c85(%rip),%rax
mov    (%rax),%edx
call   1040 <openat@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    139d <joinmnt+0x1ed>
mov    -0x50(%rbp),%rdx
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
movslq -0x2c(%rbp),%rax
mov    -0x8(%rbp,%rax,8),%rcx
lea    0xcdf(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x44(%rbp),%edi
xor    %esi,%esi
call   1060 <setns@plt>
cmp    $0xffffffff,%eax
jne    13da <joinmnt+0x22a>
mov    0x2c11(%rip),%rax
mov    (%rax),%edi
movslq -0x2c(%rbp),%rax
mov    -0x8(%rbp,%rax,8),%rdx
lea    0xcd4(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12f3 <joinmnt+0x143>
movl   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<joinmnt>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e2b(%rip),%r13
mov    0x0(%r13),%r12d
mov    %rsp,%r15
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    0xe22(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     12c9 <joinmnt+0x119>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
lea    0xe0b(%rip),%rdx
mov    %r15,%rdi
mov    %r12d,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    12d8 <joinmnt+0x128>
mov    0x0(%r13),%esi
add    $0xffffffff,%esi
lea    0xe09(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%ecx
call   1050 <snprintf@plt>
mov    0x2d8a(%rip),%rax
mov    0x2d93(%rip),%rbx
mov    (%rbx),%esi
or     (%rax),%esi
mov    %r15,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12f9 <joinmnt+0x149>
mov    %eax,%r14d
mov    0x2d4c(%rip),%rax
mov    (%rax),%ecx
lea    0xd8b(%rip),%rsi
lea    -0x30(%rbp),%rdx
mov    %r14d,%edi
call   10d0 <fstatat@plt>
cmp    $0xffffffff,%eax
jne    129c <joinmnt+0xec>
mov    0x2d33(%rip),%rax
mov    (%rax),%rax
mov    0x2d11(%rip),%rcx
cmp    (%rcx),%rax
je     12c9 <joinmnt+0x119>
mov    (%rbx),%edx
lea    0xd5b(%rip),%rsi
mov    %r14d,%edi
call   1040 <openat@plt>
cmp    $0xffffffff,%eax
je     130e <joinmnt+0x15e>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1060 <setns@plt>
cmp    $0xffffffff,%eax
je     1339 <joinmnt+0x189>
mov    %ebx,%edi
call   1070 <close@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    0xd2b(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
lea    0xd40(%rip),%rsi
mov    %r15,%rdx
jmp    1350 <joinmnt+0x1a0>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
lea    0xd4d(%rip),%rsi
lea    0xcdb(%rip),%rcx
mov    %r15,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2c88(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
lea    0xcb0(%rip),%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [4 x i8] c"mnt\00"
@__const.joinmnt.namespaces = [1 x i8*] [i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0)]
@PATH_MAX = global i32 0
@.str.1 = [12 x i8] c"_DOCKER_PID\00"
@.str.2 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.3 = [42 x i8] c"nsenter: Unable to parse _DOCKER_PID: %m\0A\00"
@.str.4 = [12 x i8] c"/proc/%d/ns\00"
@O_DIRECTORY = global i32 0
@O_RDONLY = global i32 0
@.str.5 = [34 x i8] c"nsenter: Failed to open \22%s\22: %m\0A\00"
@AT_SYMLINK_NOFOLLOW = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.6 = [50 x i8] c"nsenter: Failed to open ns file %s for ns %s: %m\0A\00"
@.str.7 = [37 x i8] c"nsenter: Failed to setns for %s: %m\0A\00"
define void @joinmnt() {
%1 = alloca [1 x i8*]
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i32
%12 = bitcast [1 x i8*]* %1 to i8*
store i32 1, i32* %2
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %3
%16 = alloca i8, i64 %14
store i64 %14, i64* %4
%17 = call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %0
store i32 1, i32* %9
br label %105
21: ; preds = %0
%22 = load i8*, i8** %5
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %8
%24 = trunc i64 %14 to i32
%25 = load i32, i32* %8
%26 = call i32 @snprintf(i8* %16, i32 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i8*, i8** %5
%28 = call i64 @strcmp(i8* %27, i8* %16)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %21
%35 = load i32, i32* @PATH_MAX
%36 = sub nsw i32 %35, 1
%37 = load i32, i32* %8
%38 = call i32 @snprintf(i8* %16, i32 %36, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = load i32, i32* @O_DIRECTORY
%40 = load i32, i32* @O_RDONLY
%41 = or i32 %39, %40
%42 = call i32 @open(i8* %16, i32 %41)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %49
45: ; preds = %34
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %16)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %34
store i32 0, i32* %6
br label %50
50: ; preds = %101, %49
%51 = load i32, i32* %6
%52 = icmp slt i32 %51, 1
br i1 %52, label %53, label %104
53: ; preds = %50
%54 = load i32, i32* %7
%55 = load i32, i32* %6
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [1 x i8*], [1 x i8*]* %1, i64 0, i64 %56
%58 = load i8*, i8** %57
%59 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%60 = call i32 @fstatat(i32 %54, i8* %58, %struct.stat* %10, i32 %59)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %68
62: ; preds = %53
%63 = load i64, i64* @errno
%64 = load i64, i64* @ENOENT
%65 = icmp eq i64 %63, %64
br i1 %65, label %66, label %67
66: ; preds = %62
br label %101
67: ; preds = %62
br label %68
68: ; preds = %67, %53
%69 = load i32, i32* %7
%70 = load i32, i32* %6
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [1 x i8*], [1 x i8*]* %1, i64 0, i64 %71
%73 = load i8*, i8** %72
%74 = load i32, i32* @O_RDONLY
%75 = call i32 @openat(i32 %69, i8* %73, i32 %74)
store i32 %75, i32* %11
%76 = load i32, i32* %11
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %86
78: ; preds = %68
%79 = load i32, i32* @stderr
%80 = load i32, i32* %6
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [1 x i8*], [1 x i8*]* %1, i64 0, i64 %81
%83 = load i8*, i8** %82
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0), i8* %16, i8* %83)
%85 = call i32 @exit(i32 1)
unreachable
86: ; preds = %68
%87 = load i32, i32* %11
%88 = call i32 @setns(i32 %87, i32 0)
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %98
90: ; preds = %86
%91 = load i32, i32* @stderr
%92 = load i32, i32* %6
%93 = sext i32 %92 to i64
%94 = getelementptr inbounds [1 x i8*], [1 x i8*]* %1, i64 0, i64 %93
%95 = load i8*, i8** %94
%96 = call i32 (i32, i8*, ...) @fprintf(i32 %91, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i8* %95)
%97 = call i32 @exit(i32 1)
unreachable
98: ; preds = %86
%99 = load i32, i32* %11
%100 = call i32 @close(i32 %99)
br label %101
101: ; preds = %98, %66
%102 = load i32, i32* %6
%103 = add nsw i32 %102, 1
store i32 %103, i32* %6
br label %50
104: ; preds = %50
store i32 0, i32* %9
br label %105
105: ; preds = %104, %20
%106 = load i8*, i8** %3
%107 = load i32, i32* %9
switch i32 %107, label %109 [
i32 0, label %108
i32 1, label %108
]
108: ; preds = %105, %105
ret void
109: ; preds = %105
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [4 x i8] c"mnt\00"
@PATH_MAX = global i32 0
@.str.1 = [12 x i8] c"_DOCKER_PID\00"
@.str.2 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.3 = [42 x i8] c"nsenter: Unable to parse _DOCKER_PID: %m\0A\00"
@.str.4 = [12 x i8] c"/proc/%d/ns\00"
@O_DIRECTORY = global i32 0
@O_RDONLY = global i32 0
@.str.5 = [34 x i8] c"nsenter: Failed to open \22%s\22: %m\0A\00"
@AT_SYMLINK_NOFOLLOW = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.6 = [50 x i8] c"nsenter: Failed to open ns file %s for ns %s: %m\0A\00"
@.str.7 = [37 x i8] c"nsenter: Failed to setns for %s: %m\0A\00"
define void @joinmnt() {
%1 = alloca %struct.stat
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %56, label %7
7: ; preds = %0
%8 = tail call i32 @atoi(i8* %5)
%9 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = call i64 @strcmp(i8* %5, i8* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %7
%17 = load i32, i32* @PATH_MAX
%18 = add nsw i32 %17, -1
%19 = call i32 @snprintf(i8* %4, i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %8)
%20 = load i32, i32* @O_DIRECTORY
%21 = load i32, i32* @O_RDONLY
%22 = or i32 %21, %20
%23 = call i32 @open(i8* %4, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %4)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %16
%30 = bitcast %struct.stat* %1 to i8*
%31 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%32 = call i32 @fstatat(i32 %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), %struct.stat* %1, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i64, i64* @errno
%36 = load i64, i64* @ENOENT
%37 = icmp eq i64 %35, %36
br i1 %37, label %55, label %38
38: ; preds = %34, %29
%39 = load i32, i32* @O_RDONLY
%40 = call i32 @openat(i32 %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.6, i64 0, i64 0), i8* %4, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %38
%47 = call i32 @setns(i32 %40, i32 0)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i32, i32* @stderr
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %46
%54 = call i32 @close(i32 %40)
br label %55
55: ; preds = %34, %53
br label %56
56: ; preds = %0, %55
ret void
}