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

/* Variables and functions */
int O_CLOEXEC;
int O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int);
int snprintf(char *, int, char *, int, char const *);
int /*<<< orphan*/ stderr;
int open_namespace(int pid_to_join, const char *ns_file) {
  char ns_path[PATH_MAX];
  int ret;
  ret = snprintf(ns_path, PATH_MAX, "/proc/%d/ns/%s", pid_to_join, ns_file);
  if (ret == PATH_MAX) {
    fprintf(stderr, "internal error: namespace path too long\n");
    return -1;
  }
  return open(ns_path, O_CLOEXEC | O_RDONLY);
}


x86 assembly (clang, -O0):
<open_namespace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2eaa(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%r8
lea    0xe7e(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e5c(%rip),%rcx
cmp    (%rcx),%eax
jne    11c4 <open_namespace+0x94>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
jmp    11e9 <open_namespace+0xb9>
mov    -0x30(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_namespace>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    %rsi,%r8
mov    %edi,%ecx
mov    0x2ead(%rip),%r14
mov    (%r14),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe9f(%rip),%rdx
mov    %rbx,%rdi
call   1030 <snprintf@plt>
cmp    (%r14),%eax
jne    118a <open_namespace+0x5a>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
lea    0xe91(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11a4 <open_namespace+0x74>
mov    0x2e2f(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/%d/ns/%s\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"internal error: namespace path too long\0A\00"
@O_CLOEXEC = global i32 0
@O_RDONLY = global i32 0
define i32 @open_namespace(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = load i32, i32* @PATH_MAX
%15 = load i32, i32* %4
%16 = load i8*, i8** %5
%17 = call i32 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %16)
store i32 %17, i32* %8
%18 = load i32, i32* %8
%19 = load i32, i32* @PATH_MAX
%20 = icmp eq i32 %18, %19
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %9
br label %29
24: ; preds = %2
%25 = load i32, i32* @O_CLOEXEC
%26 = load i32, i32* @O_RDONLY
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %13, i32 %27)
store i32 %28, i32* %3
store i32 1, i32* %9
br label %29
29: ; preds = %24, %21
%30 = load i8*, i8** %6
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [15 x i8] c"/proc/%d/ns/%s\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"internal error: namespace path too long\0A\00"
@O_CLOEXEC = global i32 0
@O_RDONLY = global i32 0
define i32 @open_namespace(i32 %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%7 = load i32, i32* @PATH_MAX
%8 = icmp eq i32 %6, %7
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %17
12: ; preds = %2
%13 = load i32, i32* @O_CLOEXEC
%14 = load i32, i32* @O_RDONLY
%15 = or i32 %14, %13
%16 = call i32 @open(i8* %5, i32 %15)
br label %17
17: ; preds = %12, %9
%18 = phi i32 [ -1, %9 ], [ %16, %12 ]
ret i32 %18
}