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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ R_OK;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int xopen2(char *, int /*<<< orphan*/);
int xsetns(int, int /*<<< orphan*/);
int switch_mnt_ns(int pid) {
  char mnt[32];
  snprintf(mnt, sizeof(mnt), "/proc/%d/ns/mnt", pid);
  if (access(mnt, R_OK) == -1)
    return 1;
  int fd, ret;
  fd = xopen2(mnt, O_RDONLY);
  if (fd < 0)
    return 1;
  ret = xsetns(fd, 0);
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<switch_mnt_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x20,%esi
lea    0xe92(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0xffffffff,%eax
jne    119a <switch_mnt_ns+0x4a>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <switch_mnt_ns+0x90>
lea    -0x30(%rbp),%rdi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1030 <xopen2@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11c5 <switch_mnt_ns+0x75>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <switch_mnt_ns+0x90>
mov    -0x34(%rbp),%edi
xor    %esi,%esi
call   1070 <xsetns@plt>
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switch_mnt_ns>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ecx
lea    0xea1(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x20,%esi
call   1040 <snprintf@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <access@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
je     11b3 <switch_mnt_ns+0x63>
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1030 <xopen2@plt>
test   %eax,%eax
js     11b3 <switch_mnt_ns+0x63>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
call   1070 <xsetns@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/%d/ns/mnt\00"
@R_OK = global i32 0
@O_RDONLY = global i32 0
define i32 @switch_mnt_ns(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [32 x i8]
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @snprintf(i8* %7, i32 32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%11 = load i32, i32* @R_OK
%12 = call i32 @access(i8* %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 1, i32* %2
br label %28
15: ; preds = %1
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @xopen2(i8* %16, i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 1, i32* %2
br label %28
22: ; preds = %15
%23 = load i32, i32* %5
%24 = call i32 @xsetns(i32 %23, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %5
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %6
store i32 %27, i32* %2
br label %28
28: ; preds = %22, %21, %14
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/%d/ns/mnt\00"
@R_OK = global i32 0
@O_RDONLY = global i32 0
define i32 @switch_mnt_ns(i32 %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @R_OK
%6 = call i32 @access(i8* %3, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %15, label %8
8: ; preds = %1
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @xopen2(i8* %3, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = call i32 @xsetns(i32 %10, i32 0)
%14 = call i32 @close(i32 %10)
br label %15
15: ; preds = %12, %8, %1
%16 = phi i32 [ 1, %1 ], [ %13, %12 ], [ 1, %8 ]
ret i32 %16
}