#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 {
  scalar_t__ st_ino;
};
typedef int /*<<< orphan*/ newnspath;
typedef int /*<<< orphan*/ curnspath;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ __NR_setns;
scalar_t__ fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
scalar_t__ syscall(int /*<<< orphan*/, int, int /*<<< orphan*/);
int enter_mount_ns(int pid) {
  int oldns, newns;
  char curnspath[128], newnspath[128];
  struct stat oldns_stat, newns_stat;
  snprintf(curnspath, sizeof(curnspath), "/proc/self/ns/mnt");
  snprintf(newnspath, sizeof(newnspath), "/proc/%d/ns/mnt", pid);
  if ((oldns = open(curnspath, O_RDONLY)) < 0 ||
      ((newns = open(newnspath, O_RDONLY)) < 0)) {
    return 0;
  }
  if (fstat(oldns, &oldns_stat) < 0 || fstat(newns, &newns_stat) < 0) {
    return 0;
  }
  if (oldns_stat.st_ino == newns_stat.st_ino) {
    return 1;
  }
  return syscall(__NR_setns, newns, 0) < 0 ? 0 : 1;
}


x86 assembly (clang, -O0):
<enter_mount_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x90(%rbp),%rdi
mov    $0x80,%esi
lea    0xe9f(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x80,%esi
lea    0xe94(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jl     11c7 <enter_mount_ns+0x87>
lea    -0x110(%rbp),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    11d3 <enter_mount_ns+0x93>
movl   $0x0,-0x4(%rbp)
jmp    1258 <enter_mount_ns+0x118>
mov    -0xc(%rbp),%edi
lea    -0x118(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
jl     1205 <enter_mount_ns+0xc5>
mov    -0x10(%rbp),%edi
lea    -0x120(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
jge    1211 <enter_mount_ns+0xd1>
movl   $0x0,-0x4(%rbp)
jmp    1258 <enter_mount_ns+0x118>
mov    -0x118(%rbp),%rax
cmp    -0x120(%rbp),%rax
jne    1231 <enter_mount_ns+0xf1>
movl   $0x1,-0x4(%rbp)
jmp    1258 <enter_mount_ns+0x118>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%esi
xor    %edx,%edx
call   1040 <syscall@plt>
mov    %rax,%rdx
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    $0x0,%rdx
cmovl  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<enter_mount_ns>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %edi,%ebx
lea    0xeaa(%rip),%rdx
xor    %r14d,%r14d
lea    0x90(%rsp),%r15
mov    %r15,%rdi
mov    $0x80,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
lea    0xe9b(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x80,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    0x2e57(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %r15,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1203 <enter_mount_ns+0xc3>
mov    %eax,%ebx
mov    0x0(%rbp),%esi
lea    0x10(%rsp),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1203 <enter_mount_ns+0xc3>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1060 <fstat@plt>
test   %rax,%rax
js     1203 <enter_mount_ns+0xc3>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1060 <fstat@plt>
test   %rax,%rax
js     1203 <enter_mount_ns+0xc3>
mov    0x8(%rsp),%rax
mov    $0x1,%r14d
cmp    (%rsp),%rax
je     1203 <enter_mount_ns+0xc3>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    %ebp,%esi
xor    %edx,%edx
call   1040 <syscall@plt>
mov    %rax,%r14
shr    $0x3f,%r14
xor    $0x1,%r14d
mov    %r14d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [18 x i8] c"/proc/self/ns/mnt\00"
@.str.1 = [16 x i8] c"/proc/%d/ns/mnt\00"
@O_RDONLY = global i32 0
@__NR_setns = global i32 0
define i32 @enter_mount_ns(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca [128 x i8]
%8 = alloca %struct.stat
%9 = alloca %struct.stat
store i32 %0, i32* %3
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%11 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %10, i32 128, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%13 = load i32, i32* %3
%14 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %12, i32 128, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %15, i32 %16)
store i32 %17, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %1
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%21 = load i32, i32* @O_RDONLY
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %5
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19, %1
store i32 0, i32* %2
br label %48
25: ; preds = %19
%26 = load i32, i32* %4
%27 = call i64 @fstat(i32 %26, %struct.stat* %8)
%28 = icmp slt i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i32, i32* %5
%31 = call i64 @fstat(i32 %30, %struct.stat* %9)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29, %25
store i32 0, i32* %2
br label %48
34: ; preds = %29
%35 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%38 = load i64, i64* %37
%39 = icmp eq i64 %36, %38
br i1 %39, label %40, label %41
40: ; preds = %34
store i32 1, i32* %2
br label %48
41: ; preds = %34
%42 = load i32, i32* @__NR_setns
%43 = load i32, i32* %5
%44 = call i64 @syscall(i32 %42, i32 %43, i32 0)
%45 = icmp slt i64 %44, 0
%46 = zext i1 %45 to i64
%47 = select i1 %45, i32 0, i32 1
store i32 %47, i32* %2
br label %48
48: ; preds = %41, %40, %33, %24
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [18 x i8] c"/proc/self/ns/mnt\00"
@.str.1 = [16 x i8] c"/proc/%d/ns/mnt\00"
@O_RDONLY = global i32 0
@__NR_setns = global i32 0
define i32 @enter_mount_ns(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca [128 x i8]
%4 = alloca %struct.stat
%5 = alloca %struct.stat
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.stat* %4 to i8*
%9 = bitcast %struct.stat* %5 to i8*
%10 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %6, i32 128, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%11 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %7, i32 128, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %6, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %37, label %15
15: ; preds = %1
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* %7, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %37, label %19
19: ; preds = %15
%20 = call i64 @fstat(i32 %13, %struct.stat* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %37, label %22
22: ; preds = %19
%23 = call i64 @fstat(i32 %17, %struct.stat* %5)
%24 = icmp slt i64 %23, 0
br i1 %24, label %37, label %25
25: ; preds = %22
%26 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%27 = load i64, i64* %26
%28 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%29 = load i64, i64* %28
%30 = icmp eq i64 %27, %29
br i1 %30, label %37, label %31
31: ; preds = %25
%32 = load i32, i32* @__NR_setns
%33 = call i64 @syscall(i32 %32, i32 %17, i32 0)
%34 = lshr i64 %33, 63
%35 = trunc i64 %34 to i32
%36 = xor i32 %35, 1
br label %37
37: ; preds = %25, %19, %22, %1, %15, %31
%38 = phi i32 [ %36, %31 ], [ 0, %15 ], [ 0, %1 ], [ 0, %22 ], [ 0, %19 ], [ 1, %25 ]
ret i32 %38
}