#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 */
scalar_t__ EEXIST;
int /*<<< orphan*/ MS_BIND;
int O_CREAT;
int O_EXCL;
int O_RDONLY;
int PATH_MAX;
int S_IRGRP;
int S_IROTH;
int S_IRWXU;
int S_IXGRP;
int S_IXOTH;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpid();
int mkdir(char *, int);
int mount(char *, char *, char *, int /*<<< orphan*/, int /*<<< orphan*/ *);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int);
int /*<<< orphan*/ stderr;
void _nsntrace_set_name() {
  char netns_path[PATH_MAX];
  int fd;
  int ret = 0;
  if ((ret = mkdir("/run/netns",
                   S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) < 0) {
    if (errno != EEXIST) {
      goto out;
    }
  }
  snprintf(netns_path, PATH_MAX, "%s/nsntrace-%d", "/run/netns", getpid());
  fd = open(netns_path, O_RDONLY | O_CREAT | O_EXCL, 0);
  if (fd < 0) {
    ret = fd;
    goto out;
  }
  close(fd);
  ret = mount("/proc/self/ns/net", netns_path, "none", MS_BIND, NULL);
out:
  if (ret < 0) {
    fprintf(stderr, "failed to set namespace name\n");
  }
}


x86 assembly (clang, -O0):
<_nsntrace_set_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x8(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
or     (%rax),%esi
mov    0x2df4(%rip),%rax
or     (%rax),%esi
mov    0x2db3(%rip),%rax
or     (%rax),%esi
mov    0x2dfa(%rip),%rax
or     (%rax),%esi
lea    0xe21(%rip),%rdi
call   1030 <mkdir@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1214 <_nsntrace_set_name+0xa4>
mov    0x2d99(%rip),%rax
mov    (%rax),%rax
mov    0x2da7(%rip),%rcx
cmp    (%rcx),%rax
je     120f <_nsntrace_set_name+0x9f>
jmp    12b2 <_nsntrace_set_name+0x142>
jmp    1214 <_nsntrace_set_name+0xa4>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdi
mov    %eax,%r8d
lea    0xdd3(%rip),%rdx
lea    0xdc1(%rip),%rcx
call   1060 <snprintf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
or     (%rax),%esi
mov    0x2d37(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1282 <_nsntrace_set_name+0x112>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    12b2 <_nsntrace_set_name+0x142>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rsi
mov    0x2d2b(%rip),%rax
mov    (%rax),%ecx
lea    0xd7c(%rip),%rdi
lea    0xd87(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1050 <mount@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12d1 <_nsntrace_set_name+0x161>
mov    0x2cdd(%rip),%rax
mov    (%rax),%edi
lea    0xd65(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_nsntrace_set_name>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
mov    0x2e6a(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e3f(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e0d(%rip),%rax
or     (%rax),%esi
mov    0x2dcc(%rip),%rax
or     (%rax),%esi
mov    0x2e13(%rip),%rax
or     (%rax),%esi
lea    0xe3a(%rip),%rdi
call   1030 <mkdir@plt>
test   %eax,%eax
jns    11e5 <_nsntrace_set_name+0x75>
mov    0x2dba(%rip),%rax
mov    (%rax),%rax
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%rax
jne    1261 <_nsntrace_set_name+0xf1>
mov    (%r14),%r14d
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe15(%rip),%rdx
lea    0xe03(%rip),%rcx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %eax,%r8d
call   1060 <snprintf@plt>
mov    0x2db6(%rip),%rax
mov    0x2ddf(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d74(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1261 <_nsntrace_set_name+0xf1>
mov    %eax,%edi
call   1070 <close@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%ecx
lea    0xdcf(%rip),%rdi
lea    0xdda(%rip),%rdx
mov    %rbx,%rsi
xor    %r8d,%r8d
call   1050 <mount@plt>
test   %eax,%eax
jns    1276 <_nsntrace_set_name+0x106>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/run/netns\00"
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@S_IXGRP = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [15 x i8] c"%s/nsntrace-%d\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.2 = [18 x i8] c"/proc/self/ns/net\00"
@.str.3 = [5 x i8] c"none\00"
@MS_BIND = global i32 0
@stderr = global i32 0
@.str.4 = [30 x i8] c"failed to set namespace name\0A\00"
define void @_nsntrace_set_name() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %1
%8 = alloca i8, i64 %6
store i64 %6, i64* %2
store i32 0, i32* %4
%9 = load i32, i32* @S_IRWXU
%10 = load i32, i32* @S_IRGRP
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IXGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IROTH
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IXOTH
%17 = or i32 %15, %16
%18 = call i32 @mkdir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %0
%21 = load i64, i64* @errno
%22 = load i64, i64* @EEXIST
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %20
br label %45
25: ; preds = %20
br label %26
26: ; preds = %25, %0
%27 = load i32, i32* @PATH_MAX
%28 = call i32 (...) @getpid()
%29 = call i32 @snprintf(i8* %8, i32 %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @O_RDONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = load i32, i32* @O_EXCL
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %8, i32 %34, i32 0)
store i32 %35, i32* %3
%36 = load i32, i32* %3
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %26
%39 = load i32, i32* %3
store i32 %39, i32* %4
br label %45
40: ; preds = %26
%41 = load i32, i32* %3
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* @MS_BIND
%44 = call i32 @mount(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %43, i32* null)
store i32 %44, i32* %4
br label %45
45: ; preds = %40, %38, %24
%46 = load i32, i32* %4
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %48, %45
%52 = load i8*, i8** %1
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [11 x i8] c"/run/netns\00"
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@S_IXGRP = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [15 x i8] c"%s/nsntrace-%d\00"
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.2 = [18 x i8] c"/proc/self/ns/net\00"
@.str.3 = [5 x i8] c"none\00"
@MS_BIND = global i32 0
@stderr = global i32 0
@.str.4 = [30 x i8] c"failed to set namespace name\0A\00"
define void @_nsntrace_set_name() {
%1 = load i32, i32* @PATH_MAX
%2 = zext i32 %1 to i64
%3 = alloca i8, i64 %2
%4 = load i32, i32* @S_IRWXU
%5 = load i32, i32* @S_IRGRP
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IXGRP
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IROTH
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IXOTH
%12 = or i32 %10, %11
%13 = tail call i32 @mkdir(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %0
%16 = load i64, i64* @errno
%17 = load i64, i64* @EEXIST
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %35
19: ; preds = %15, %0
%20 = load i32, i32* @PATH_MAX
%21 = tail call i32 (...) @getpid()
%22 = call i32 @snprintf(i8* %3, i32 %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @O_RDONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @O_EXCL
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %3, i32 %27, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %19
%31 = call i32 @close(i32 %28)
%32 = load i32, i32* @MS_BIND
%33 = call i32 @mount(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %32, i32* null)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %19, %15, %30
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %35, %30
ret void
}