#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__ ENOENT;
scalar_t__ EPERM;
int O_CLOEXEC;
int O_RDONLY;
int asprintf(char **, char *, char *, char *);
scalar_t__ bad_nsname(char *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int setns(int, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int lookup_and_setns(char *ns, char *nsdir, int nstype, int allow_fail) {
  int nsfd, perm_issue;
  char *nspath;
  if (bad_nsname(ns)) {
    fprintf(
        stderr,
        "nsdo"
        ": namespace names can't contain '/', be empty, or be '.' or '..'.\n");
    return 0;
  }
  if (asprintf(&nspath, "%s/%s", nsdir, ns) == -1) {
    perror("nsdo"
           ": asprintf");
    return 0;
  }
  if ((nsfd = open(nspath, O_RDONLY | O_CLOEXEC)) == -1) {
    if (errno == ENOENT && allow_fail) {
      return 1;
    } else {
      fprintf(stderr,
              "nsdo"
              ": open(\"%s\"): %s\n",
              nspath, strerror(errno));
      free(nspath);
      return 0;
    }
  }
  free(nspath);
  if (setns(nsfd, nstype) == -1) {
    perm_issue = errno == EPERM;
    perror("nsdo"
           ": setns");
    if (perm_issue)
      fprintf(stderr, "\nis the "
                      "nsdo"
                      " binary missing the setuid bit?\n");
    return 0;
  } else {
    return 1;
  }
}


x86 assembly (clang, -O0):
<lookup_and_setns>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <bad_nsname@plt>
cmp    $0x0,%rax
je     11cc <lookup_and_setns+0x4c>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1328 <lookup_and_setns+0x1a8>
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x30(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    1205 <lookup_and_setns+0x85>
lea    0xe59(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1328 <lookup_and_setns+0x1a8>
mov    -0x30(%rbp),%rdi
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2d9f(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    12ac <lookup_and_setns+0x12c>
mov    0x2d9d(%rip),%rax
mov    (%rax),%rax
mov    0x2d83(%rip),%rcx
cmp    (%rcx),%rax
jne    125c <lookup_and_setns+0xdc>
cmpl   $0x0,-0x20(%rbp)
je     125c <lookup_and_setns+0xdc>
movl   $0x1,-0x4(%rbp)
jmp    1328 <lookup_and_setns+0x1a8>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d59(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1328 <lookup_and_setns+0x1a8>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1040 <setns@plt>
cmp    $0xffffffff,%eax
jne    1321 <lookup_and_setns+0x1a1>
mov    0x2d00(%rip),%rax
mov    (%rax),%rax
mov    0x2cd6(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x28(%rbp)
lea    0xd83(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0x0,-0x28(%rbp)
je     1315 <lookup_and_setns+0x195>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1328 <lookup_and_setns+0x1a8>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_and_setns>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%rbp
call   1060 <bad_nsname@plt>
test   %rax,%rax
je     11bb <lookup_and_setns+0x3b>
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    12bb <lookup_and_setns+0x13b>
lea    0xe85(%rip),%rsi
mov    %rsp,%rdi
mov    %rbx,%rdx
mov    %rbp,%rcx
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
je     12ad <lookup_and_setns+0x12d>
mov    (%rsp),%rdi
mov    0x2e04(%rip),%rax
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1258 <lookup_and_setns+0xd8>
mov    %eax,%ebx
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%edi
mov    %r15d,%esi
call   1040 <setns@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
jne    12bb <lookup_and_setns+0x13b>
mov    0x2dad(%rip),%rax
mov    (%rax),%r14
mov    0x2d83(%rip),%rax
mov    (%rax),%rbp
lea    0xe3b(%rip),%rdi
call   1090 <perror@plt>
xor    %ebx,%ebx
cmp    %rbp,%r14
jne    12bb <lookup_and_setns+0x13b>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
jmp    11ad <lookup_and_setns+0x2d>
mov    0x2d71(%rip),%rax
mov    (%rax),%rdi
test   %r14d,%r14d
je     1278 <lookup_and_setns+0xf8>
mov    0x2d52(%rip),%rax
mov    $0x1,%ebx
cmp    (%rax),%rdi
je     12bb <lookup_and_setns+0x13b>
mov    0x2d59(%rip),%rax
mov    (%rax),%ebp
mov    (%rsp),%r14
call   10a0 <strerror@plt>
lea    0xdcb(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    12bb <lookup_and_setns+0x13b>
lea    0xd99(%rip),%rdi
call   1090 <perror@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [71 x i8] c"nsdo: namespace names can't contain '/', be empty, or be '.' or '..'.\0A\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [15 x i8] c"nsdo: asprintf\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.3 = [22 x i8] c"nsdo: open(\22%s\22): %s\0A\00"
@EPERM = global i64 0
@.str.4 = [12 x i8] c"nsdo: setns\00"
@.str.5 = [45 x i8] c"\0Ais the nsdo binary missing the setuid bit?\0A\00"
define i32 @lookup_and_setns(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %6
%14 = call i64 @bad_nsname(i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %69
19: ; preds = %4
%20 = load i8*, i8** %7
%21 = load i8*, i8** %6
%22 = call i32 @asprintf(i8** %12, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %5
br label %69
26: ; preds = %19
%27 = load i8*, i8** %12
%28 = load i32, i32* @O_RDONLY
%29 = load i32, i32* @O_CLOEXEC
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %27, i32 %30)
store i32 %31, i32* %10
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %49
33: ; preds = %26
%34 = load i64, i64* @errno
%35 = load i64, i64* @ENOENT
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = load i32, i32* %9
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %37
store i32 1, i32* %5
br label %69
41: ; preds = %37, %33
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %12
%44 = load i64, i64* @errno
%45 = call i8* @strerror(i64 %44)
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %43, i8* %45)
%47 = load i8*, i8** %12
%48 = call i32 @free(i8* %47)
store i32 0, i32* %5
br label %69
49: ; preds = %26
%50 = load i8*, i8** %12
%51 = call i32 @free(i8* %50)
%52 = load i32, i32* %10
%53 = load i32, i32* %8
%54 = call i32 @setns(i32 %52, i32 %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %68
56: ; preds = %49
%57 = load i64, i64* @errno
%58 = load i64, i64* @EPERM
%59 = icmp eq i64 %57, %58
%60 = zext i1 %59 to i32
store i32 %60, i32* %11
%61 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%62 = load i32, i32* %11
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %56
%65 = load i32, i32* @stderr
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
br label %67
67: ; preds = %64, %56
store i32 0, i32* %5
br label %69
68: ; preds = %49
store i32 1, i32* %5
br label %69
69: ; preds = %68, %67, %41, %40, %24, %16
%70 = load i32, i32* %5
ret i32 %70
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [71 x i8] c"nsdo: namespace names can't contain '/', be empty, or be '.' or '..'.\0A\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [15 x i8] c"nsdo: asprintf\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.3 = [22 x i8] c"nsdo: open(\22%s\22): %s\0A\00"
@EPERM = global i64 0
@.str.4 = [12 x i8] c"nsdo: setns\00"
@.str.5 = [45 x i8] c"\0Ais the nsdo binary missing the setuid bit?\0A\00"
define i32 @lookup_and_setns(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = bitcast i8** %5 to i8*
%7 = tail call i64 @bad_nsname(i8* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0))
br label %50
12: ; preds = %4
%13 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %50
17: ; preds = %12
%18 = load i8*, i8** %5
%19 = load i32, i32* @O_RDONLY
%20 = load i32, i32* @O_CLOEXEC
%21 = or i32 %20, %19
%22 = call i32 @open(i8* %18, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %37
24: ; preds = %17
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOENT
%27 = icmp eq i64 %25, %26
%28 = icmp ne i32 %3, 0
%29 = and i1 %28, %27
br i1 %29, label %50, label %30
30: ; preds = %24
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %5
%33 = call i8* @strerror(i64 %25)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %32, i8* %33)
%35 = load i8*, i8** %5
%36 = call i32 @free(i8* %35)
br label %50
37: ; preds = %17
%38 = load i8*, i8** %5
%39 = call i32 @free(i8* %38)
%40 = call i32 @setns(i32 %22, i32 %2)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %50
42: ; preds = %37
%43 = load i64, i64* @errno
%44 = load i64, i64* @EPERM
%45 = icmp eq i64 %43, %44
%46 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br i1 %45, label %47, label %50
47: ; preds = %42
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
br label %50
50: ; preds = %37, %42, %47, %24, %30, %15, %9
%51 = phi i32 [ 0, %9 ], [ 0, %15 ], [ 0, %30 ], [ 1, %24 ], [ 0, %47 ], [ 0, %42 ], [ 1, %37 ]
ret i32 %51
}