#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ geteuid();
scalar_t__ getuid();
int /*<<< orphan*/ invalid_filename(char const *, int /*<<< orphan*/);
scalar_t__ lstat(char *, struct stat *);
char *netns_control_file(char const *);
int /*<<< orphan*/ stderr;
scalar_t__ strchr(char const *, char);
char *strerror(int /*<<< orphan*/);
scalar_t__ strstr(char const *, char *);
void check_netns(const char *nsname) {
  if (strchr(nsname, '/') || strstr(nsname, "..")) {
    fprintf(stderr, "Error: invalid netns name %s\n", nsname);
    exit(1);
  }
  invalid_filename(nsname, 0);
  char *control_file = netns_control_file(nsname);
  {
    if (getuid() != 0)
      assert(geteuid() != 0);
  };
  struct stat st;
  if (lstat(control_file, &st)) {
    fprintf(stderr, "Error: invalid netns '%s' (%s: %s)\n", nsname,
            control_file, strerror(errno));
    exit(1);
  }
  if (!S_ISREG(st.st_mode)) {
    fprintf(stderr, "Error: invalid netns '%s' (%s: not a regular file)\n",
            nsname, control_file);
    exit(1);
  }
  free(control_file);
}


x86 assembly (clang, -O0):
<check_netns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
jne    120e <check_netns+0x3e>
mov    -0x8(%rbp),%rdi
lea    0xe01(%rip),%rsi
call   10f0 <strstr@plt>
cmp    $0x0,%rax
je     1233 <check_netns+0x63>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   10c0 <invalid_filename@plt>
mov    -0x8(%rbp),%rdi
call   1040 <netns_control_file@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%rax
je     1274 <check_netns+0xa4>
mov    $0x0,%al
call   1070 <geteuid@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <lstat@plt>
cmp    $0x0,%rax
je     12db <check_netns+0x10b>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    %rax,%r8
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
call   10b0 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1315 <check_netns+0x145>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd41(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_netns>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
jne    1263 <check_netns+0x93>
lea    0xe0f(%rip),%rsi
mov    %rbx,%rdi
call   10f0 <strstr@plt>
test   %rax,%rax
jne    1263 <check_netns+0x93>
mov    %rbx,%rdi
xor    %esi,%esi
call   10c0 <invalid_filename@plt>
mov    %rbx,%rdi
call   1040 <netns_control_file@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
je     1234 <check_netns+0x64>
xor    %eax,%eax
call   1070 <geteuid@plt>
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1090 <assert@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1080 <lstat@plt>
test   %rax,%rax
jne    1287 <check_netns+0xb7>
mov    0x8(%rsp),%edi
call   10b0 <S_ISREG@plt>
test   %eax,%eax
je     12c1 <check_netns+0xf1>
mov    %r14,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <free@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%ebp
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd7c(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xd74(%rip),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"..\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Error: invalid netns name %s\0A\00"
@.str.2 = [36 x i8] c"Error: invalid netns '%s' (%s: %s)\0A\00"
@errno = global i32 0
@.str.3 = [52 x i8] c"Error: invalid netns '%s' (%s: not a regular file)\0A\00"
define void @check_netns(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @strchr(i8* %5, i8 signext 47)
%7 = icmp ne i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = call i64 @strstr(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %8, %1
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %2
%15 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %8
%18 = load i8*, i8** %2
%19 = call i32 @invalid_filename(i8* %18, i32 0)
%20 = load i8*, i8** %2
%21 = call i8* @netns_control_file(i8* %20)
store i8* %21, i8** %3
%22 = call i64 (...) @getuid()
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %17
%25 = call i64 (...) @geteuid()
%26 = icmp ne i64 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
br label %29
29: ; preds = %24, %17
%30 = load i8*, i8** %3
%31 = call i64 @lstat(i8* %30, %struct.stat* %4)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %2
%36 = load i8*, i8** %3
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %35, i8* %36, i8* %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %29
%42 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @S_ISREG(i32 %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %52, label %46
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %2
%49 = load i8*, i8** %3
%50 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i8* %48, i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %41
%53 = load i8*, i8** %3
%54 = call i32 @free(i8* %53)
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"..\00"
@stderr = global i32 0
@.str.1 = [30 x i8] c"Error: invalid netns name %s\0A\00"
@.str.2 = [36 x i8] c"Error: invalid netns '%s' (%s: %s)\0A\00"
@errno = global i32 0
@.str.3 = [52 x i8] c"Error: invalid netns '%s' (%s: not a regular file)\0A\00"
define void @check_netns(i8* %0) {
%2 = alloca %struct.stat
%3 = tail call i64 @strchr(i8* %0, i8 signext 47)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i64 @strstr(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %5, %1
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %5
%13 = tail call i32 @invalid_filename(i8* %0, i32 0)
%14 = tail call i8* @netns_control_file(i8* %0)
%15 = tail call i64 (...) @getuid()
%16 = icmp eq i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %12
%18 = tail call i64 (...) @geteuid()
%19 = icmp ne i64 %18, 0
%20 = zext i1 %19 to i32
%21 = tail call i32 @assert(i32 %20)
br label %22
22: ; preds = %17, %12
%23 = bitcast %struct.stat* %2 to i8*
%24 = call i64 @lstat(i8* %14, %struct.stat* %2)
%25 = icmp eq i64 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %14, i8* %29)
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %22
%33 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%34 = load i32, i32* %33
%35 = tail call i32 @S_ISREG(i32 %34)
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %14)
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %32
%42 = tail call i32 @free(i8* %14)
ret void
}