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

/* Variables and functions */
int EIO;
int O_CLOEXEC;
int O_RDONLY;
int open(char *, int);
int snprintf(char *, int, char *, int const, char const *);
int preserve_ns(const int pid, const char *ns) {
  int ret;
  char path[50];
  ret = snprintf(path, sizeof(path), "/proc/%d/ns/%s", pid, ns);
  if (ret < 0 || (size_t)ret >= sizeof(path))
    return -EIO;
  return open(path, O_RDONLY | O_CLOEXEC);
}


x86 assembly (clang, -O0):
<preserve_ns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    $0x32,%esi
lea    0xeba(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1166 <preserve_ns+0x46>
movslq -0x14(%rbp),%rax
cmp    $0x32,%rax
jb     1179 <preserve_ns+0x59>
xor    %eax,%eax
mov    0x2e69(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1197 <preserve_ns+0x77>
lea    -0x50(%rbp),%rdi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<preserve_ns>:
sub    $0x38,%rsp
mov    %rsi,%r8
mov    %edi,%ecx
lea    0xed0(%rip),%rdx
mov    %rsp,%rdi
mov    $0x32,%esi
call   1030 <snprintf@plt>
cmp    $0x32,%eax
jb     1152 <preserve_ns+0x32>
mov    0x2e8f(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x38,%rsp
ret
mov    0x2e8f(%rip),%rax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rsp,%rdi
call   1040 <open@plt>
add    $0x38,%rsp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/%d/ns/%s\00"
@EIO = global i32 0
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
define i32 @preserve_ns(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [50 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = call i32 @snprintf(i8* %8, i32 50, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = icmp uge i64 %16, 50
br i1 %17, label %18, label %21
18: ; preds = %14, %2
%19 = load i32, i32* @EIO
%20 = sub nsw i32 0, %19
store i32 %20, i32* %3
br label %27
21: ; preds = %14
%22 = getelementptr inbounds [50 x i8], [50 x i8]* %7, i64 0, i64 0
%23 = load i32, i32* @O_RDONLY
%24 = load i32, i32* @O_CLOEXEC
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %22, i32 %25)
store i32 %26, i32* %3
br label %27
27: ; preds = %21, %18
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/%d/ns/%s\00"
@EIO = global i32 0
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
define i32 @preserve_ns(i32 %0, i8* %1) {
%3 = alloca [50 x i8]
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%5 = call i32 @snprintf(i8* %4, i32 50, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1)
%6 = icmp ugt i32 %5, 49
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @EIO
%9 = sub nsw i32 0, %8
br label %15
10: ; preds = %2
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_CLOEXEC
%13 = or i32 %12, %11
%14 = call i32 @open(i8* %4, i32 %13)
br label %15
15: ; preds = %10, %7
%16 = phi i32 [ %9, %7 ], [ %14, %10 ]
ret i32 %16
}