#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 ENOENT;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int do_netns_switch(int);
int errno;
int open(char *, int /*<<< orphan*/);
int netns_switch_root(void) {
  int fd, res;
  fd = open("/proc/1/ns/net", O_RDONLY);
  if (fd < 0) {
    res = errno;
    if (res == ENOENT)
      res = -1;
    return res;
  }
  res = do_netns_switch(fd);
  close(fd);
  if (res == ENOENT)
    return -1;
  return res;
}


x86 assembly (clang, -O0):
<netns_switch_root>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118a <netns_switch_root+0x5a>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2e58(%rip),%rcx
cmp    (%rcx),%eax
jne    117f <netns_switch_root+0x4f>
movl   $0xffffffff,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c1 <netns_switch_root+0x91>
mov    -0x8(%rbp),%edi
call   1040 <do_netns_switch@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <close@plt>
mov    -0xc(%rbp),%eax
mov    0x2e21(%rip),%rcx
cmp    (%rcx),%eax
jne    11bb <netns_switch_root+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c1 <netns_switch_root+0x91>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netns_switch_root>:
push   %rbp
push   %rbx
push   %rax
mov    0x2eae(%rip),%rax
mov    (%rax),%esi
lea    0xebd(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1160 <netns_switch_root+0x30>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <do_netns_switch@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
jmp    1169 <netns_switch_root+0x39>
mov    0x2e71(%rip),%rax
mov    (%rax),%ebx
mov    0x2e58(%rip),%rax
cmp    (%rax),%ebx
mov    $0xffffffff,%eax
cmovne %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/1/ns/net\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
define i32 @netns_switch_root() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %16
8: ; preds = %0
%9 = load i32, i32* @errno
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = load i32, i32* @ENOENT
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %14
13: ; preds = %8
store i32 -1, i32* %3
br label %14
14: ; preds = %13, %8
%15 = load i32, i32* %3
store i32 %15, i32* %1
br label %27
16: ; preds = %0
%17 = load i32, i32* %2
%18 = call i32 @do_netns_switch(i32 %17)
store i32 %18, i32* %3
%19 = load i32, i32* %2
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %3
%22 = load i32, i32* @ENOENT
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %25
24: ; preds = %16
store i32 -1, i32* %1
br label %27
25: ; preds = %16
%26 = load i32, i32* %3
store i32 %26, i32* %1
br label %27
27: ; preds = %25, %24, %14
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/1/ns/net\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
define i32 @netns_switch_root() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = load i32, i32* @errno
br label %9
6: ; preds = %0
%7 = tail call i32 @do_netns_switch(i32 %2)
%8 = tail call i32 @close(i32 %2)
br label %9
9: ; preds = %6, %4
%10 = phi i32 [ %7, %6 ], [ %5, %4 ]
%11 = load i32, i32* @ENOENT
%12 = icmp eq i32 %10, %11
%13 = select i1 %12, i32 -1, i32 %10
ret i32 %13
}