#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_dev;
  scalar_t__ st_ino;
};
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int EBADF;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int preserve_ns(int /*<<< orphan*/, char const *);
int in_same_namespace(int ns_fd1, pid_t pid2, const char *ns) {
  int ns_fd2 = -EBADF;
  int ret = -1;
  struct stat ns_st1, ns_st2;
  ret = fstat(ns_fd1, &ns_st1);
  if (ret < 0)
    return -1;
  ns_fd2 = preserve_ns(pid2, ns);
  if (ns_fd2 < 0)
    return -1;
  ret = fstat(ns_fd2, &ns_st2);
  close(ns_fd2);
  if (ret < 0)
    return -1;
  if ((ns_st1.st_dev == ns_st2.st_dev) && (ns_st1.st_ino == ns_st2.st_ino))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<in_same_namespace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
xor    %eax,%eax
mov    0x2e95(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1050 <fstat@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    117c <in_same_namespace+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <in_same_namespace+0xcd>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <preserve_ns@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a1 <in_same_namespace+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <in_same_namespace+0xcd>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1050 <fstat@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x20(%rbp)
jge    11ce <in_same_namespace+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fd <in_same_namespace+0xcd>
mov    -0x30(%rbp),%rax
cmp    -0x40(%rbp),%rax
jne    11f6 <in_same_namespace+0xc6>
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jne    11f6 <in_same_namespace+0xc6>
movl   $0x1,-0x4(%rbp)
jmp    11fd <in_same_namespace+0xcd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<in_same_namespace>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
lea    0x10(%rsp),%rsi
call   1050 <fstat@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1198 <in_same_namespace+0x68>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <preserve_ns@plt>
test   %eax,%eax
js     1198 <in_same_namespace+0x68>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1050 <fstat@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
test   %ebp,%ebp
js     1198 <in_same_namespace+0x68>
mov    0x10(%rsp),%rax
cmp    (%rsp),%rax
jne    1195 <in_same_namespace+0x65>
mov    0x18(%rsp),%rax
mov    $0x1,%r14d
cmp    0x8(%rsp),%rax
je     1198 <in_same_namespace+0x68>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@EBADF = global i32 0
define i32 @in_same_namespace(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca %struct.stat
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%12 = load i32, i32* @EBADF
%13 = sub nsw i32 0, %12
store i32 %13, i32* %8
store i32 -1, i32* %9
%14 = load i32, i32* %5
%15 = call i32 @fstat(i32 %14, %struct.stat* %10)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %48
19: ; preds = %3
%20 = load i32, i32* %6
%21 = load i8*, i8** %7
%22 = call i32 @preserve_ns(i32 %20, i8* %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 -1, i32* %4
br label %48
26: ; preds = %19
%27 = load i32, i32* %8
%28 = call i32 @fstat(i32 %27, %struct.stat* %11)
store i32 %28, i32* %9
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %9
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %26
store i32 -1, i32* %4
br label %48
34: ; preds = %26
%35 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%36 = load i64, i64* %35
%37 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%38 = load i64, i64* %37
%39 = icmp eq i64 %36, %38
br i1 %39, label %40, label %47
40: ; preds = %34
%41 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 1
%42 = load i64, i64* %41
%43 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 1
%44 = load i64, i64* %43
%45 = icmp eq i64 %42, %44
br i1 %45, label %46, label %47
46: ; preds = %40
store i32 1, i32* %4
br label %48
47: ; preds = %40, %34
store i32 0, i32* %4
br label %48
48: ; preds = %47, %46, %33, %25, %18
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@EBADF = global i32 0
define i32 @in_same_namespace(i32 %0, i32 %1, i8* %2) {
%4 = alloca %struct.stat
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %4 to i8*
%7 = bitcast %struct.stat* %5 to i8*
%8 = call i32 @fstat(i32 %0, %struct.stat* %4)
%9 = icmp slt i32 %8, 0
br i1 %9, label %30, label %10
10: ; preds = %3
%11 = tail call i32 @preserve_ns(i32 %1, i8* %2)
%12 = icmp slt i32 %11, 0
br i1 %12, label %30, label %13
13: ; preds = %10
%14 = call i32 @fstat(i32 %11, %struct.stat* %5)
%15 = tail call i32 @close(i32 %11)
%16 = icmp slt i32 %14, 0
br i1 %16, label %30, label %17
17: ; preds = %13
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%19 = load i64, i64* %18
%20 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%21 = load i64, i64* %20
%22 = icmp eq i64 %19, %21
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%25 = load i64, i64* %24
%26 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 1
%27 = load i64, i64* %26
%28 = icmp eq i64 %25, %27
br i1 %28, label %30, label %29
29: ; preds = %23, %17
br label %30
30: ; preds = %23, %13, %10, %3, %29
%31 = phi i32 [ 0, %29 ], [ -1, %3 ], [ -1, %10 ], [ -1, %13 ], [ 1, %23 ]
ret i32 %31
}