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

/* Variables and functions */
int MAXPATHLEN;
int /*<<< orphan*/ O_RDONLY;
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char const *);
char *strchr(char const *, char);
int get_netns_fd(const char *name) {
  char pathbuf[MAXPATHLEN];
  const char *path, *ptr;
  path = name;
  ptr = strchr(name, '/');
  if (!ptr) {
    snprintf(pathbuf, sizeof(pathbuf), "%s/%s", "/var/run/netns", name);
    path = pathbuf;
  }
  return open(path, O_RDONLY);
}


x86 assembly (clang, -O0):
<get_netns_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x38(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ba <get_netns_fd+0x8a>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x8(%rbp),%r8
lea    0xe5a(%rip),%rdx
lea    0xe59(%rip),%rcx
call   1040 <snprintf@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_netns_fd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e9c(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    $0x2f,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     116c <get_netns_fd+0x3c>
mov    %r14,%rbx
jmp    1188 <get_netns_fd+0x58>
lea    0xe8d(%rip),%rdx
lea    0xe8c(%rip),%rcx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%r8
call   1040 <snprintf@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [15 x i8] c"/var/run/netns\00"
@O_RDONLY = global i32 0
define i32 @get_netns_fd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i32, i32* @MAXPATHLEN
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %3
%10 = alloca i8, i64 %8
store i64 %8, i64* %4
%11 = load i8*, i8** %2
store i8* %11, i8** %5
%12 = load i8*, i8** %2
%13 = call i8* @strchr(i8* %12, i8 signext 47)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp ne i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %1
%17 = trunc i64 %8 to i32
%18 = load i8*, i8** %2
%19 = call i32 @snprintf(i8* %10, i32 %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %18)
store i8* %10, i8** %5
br label %20
20: ; preds = %16, %1
%21 = load i8*, i8** %5
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
%24 = load i8*, i8** %3
ret i32 %23
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [15 x i8] c"/var/run/netns\00"
@O_RDONLY = global i32 0
define i32 @get_netns_fd(i8* %0) {
%2 = load i32, i32* @MAXPATHLEN
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 47)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %1
%10 = phi i8* [ %0, %1 ], [ %4, %7 ]
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
ret i32 %12
}