#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_ISLNK(int /*<<< orphan*/);
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) && !S_ISLNK(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   1070 <strchr@plt>
cmp    $0x0,%rax
jne    121e <check_netns+0x3e>
mov    -0x8(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1100 <strstr@plt>
cmp    $0x0,%rax
je     1243 <check_netns+0x63>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   10d0 <invalid_filename@plt>
mov    -0x8(%rbp),%rdi
call   1050 <netns_control_file@plt>
mov    %rax,-0x10(%rbp)
mov    $0x0,%al
call   1060 <getuid@plt>
cmp    $0x0,%rax
je     1284 <check_netns+0xa4>
mov    $0x0,%al
call   1080 <geteuid@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   10a0 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <lstat@plt>
cmp    $0x0,%rax
je     12eb <check_netns+0x10b>
mov    0x2d3e(%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    0x2d1a(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    %rax,%r8
lea    0xd47(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x18(%rbp),%edi
call   10c0 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1336 <check_netns+0x156>
mov    -0x18(%rbp),%edi
call   1040 <S_ISLNK@plt>
cmp    $0x0,%eax
jne    1336 <check_netns+0x156>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd20(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <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   1070 <strchr@plt>
test   %rax,%rax
jne    1284 <check_netns+0xa4>
lea    0xdfb(%rip),%rsi
mov    %rbx,%rdi
call   1100 <strstr@plt>
test   %rax,%rax
jne    1284 <check_netns+0xa4>
mov    %rbx,%rdi
xor    %esi,%esi
call   10d0 <invalid_filename@plt>
mov    %rbx,%rdi
call   1050 <netns_control_file@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1060 <getuid@plt>
test   %rax,%rax
je     1248 <check_netns+0x68>
xor    %eax,%eax
call   1080 <geteuid@plt>
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   10a0 <assert@plt>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1090 <lstat@plt>
test   %rax,%rax
jne    12a8 <check_netns+0xc8>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   10c0 <S_ISREG@plt>
test   %eax,%eax
jne    1274 <check_netns+0x94>
mov    %ebp,%edi
call   1040 <S_ISLNK@plt>
test   %eax,%eax
je     12e2 <check_netns+0x102>
mov    %r14,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <free@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2d31(%rip),%rax
mov    (%rax),%ebp
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
lea    0xd5b(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %r14,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xd53(%rip),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <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 %57, label %46
46: ; preds = %41
%47 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @S_ISLNK(i32 %48)
%50 = icmp ne i32 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %46
%52 = load i32, i32* @stderr
%53 = load i8*, i8** %2
%54 = load i8*, i8** %3
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i8* %53, i8* %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %46, %41
%58 = load i8*, i8** %3
%59 = call i32 @free(i8* %58)
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 %44
37: ; preds = %32
%38 = tail call i32 @S_ISLNK(i32 %34)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %37
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %14)
%43 = tail call i32 @exit(i32 1)
unreachable
44: ; preds = %37, %32
%45 = tail call i32 @free(i8* %14)
ret void
}