// File: /workspace/github_c_makefile_star2_O2/arachsys[P]containers/contain.c
// Function: main
int main(int argc, char **argv) {
  char *gidmap = NULL, *inside = NULL, *outside = NULL, *uidmap = NULL;
  int hostnet = 0, master, option, stdio = 0;
  pid_t child, parent;

  while ((option = getopt(argc, argv, "+:cg:i:no:u:")) > 0)
    switch (option) {
      case 'c':
        stdio++;
        break;
      case 'g':
        gidmap = optarg;
        break;
      case 'i':
        inside = optarg;
        break;
      case 'n':
        hostnet++;
        break;
      case 'o':
        outside = optarg;
        break;
      case 'u':
        uidmap = optarg;
        break;
      default:
        usage(argv[0]);
    }

  if (argc <= optind)
    usage(argv[0]);

  parent = getpid();
  switch (child = fork()) {
    case -1:
      err(EXIT_FAILURE, "fork");
    case 0:
      raise(SIGSTOP);
      if (geteuid() != 0)
        denysetgroups(parent);
      writemap(parent, GID, gidmap);
      writemap(parent, UID, uidmap);

      if (outside) {
        if (setgid(getgid()) < 0 || setuid(getuid()) < 0)
          errx(EXIT_FAILURE, "Failed to drop privileges");
        prctl(PR_SET_DUMPABLE, 1);
        execlp(SHELL, SHELL, "-c", outside, NULL);
        err(EXIT_FAILURE, "exec %s", outside);
      }

      exit(EXIT_SUCCESS);
  }

  if (setgid(getgid()) < 0 || setuid(getuid()) < 0)
    errx(EXIT_FAILURE, "Failed to drop privileges");
  prctl(PR_SET_DUMPABLE, 1);

  if (unshare(CLONE_NEWUSER) < 0)
    errx(EXIT_FAILURE, "Failed to unshare user namespace");

#ifdef CLONE_NEWCGROUP
  if (unshare(CLONE_NEWCGROUP) < 0)
    errx(EXIT_FAILURE, "Failed to unshare cgroup namespace");
#endif

  if (unshare(CLONE_NEWIPC) < 0)
    errx(EXIT_FAILURE, "Failed to unshare IPC namespace");

  if (!hostnet && unshare(CLONE_NEWNET) < 0)
    errx(EXIT_FAILURE, "Failed to unshare network namespace");

  if (unshare(CLONE_NEWNS) < 0)
    errx(EXIT_FAILURE, "Failed to unshare mount namespace");

#ifdef CLONE_NEWTIME
  if (unshare(CLONE_NEWTIME) < 0)
    errx(EXIT_FAILURE, "Failed to unshare time namespace");
#endif

  if (unshare(CLONE_NEWUTS) < 0)
    errx(EXIT_FAILURE, "Failed to unshare UTS namespace");

  waitforstop(child);
  kill(child, SIGCONT);
  waitforexit(child);

  setgid(0);
  setgroups(0, NULL);
  setuid(0);

  master = stdio ? -1 : getconsole();
  createroot(argv[optind], master, inside);

  if (unshare(CLONE_NEWPID) < 0)
    errx(EXIT_FAILURE, "Failed to unshare PID namespace");

  switch (child = fork()) {
    case -1:
      err(EXIT_FAILURE, "fork");
    case 0:
      mountproc();
      if (!hostnet)
        mountsys();
      enterroot();

      if (master >= 0) {
        close(master);
        setconsole("/dev/console");
      }

      clearenv();
      putenv("container=contain");

      if (argv[optind + 1])
        execv(argv[optind + 1], argv + optind + 1);
      else
        execl(SHELL, SHELL, NULL);
      err(EXIT_FAILURE, "exec");
  }

  return supervise(child, master);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movq $0x0, 0x18(%rsp)
leaq 0x1a39(%rip), %r15      # 0x4028
movq 0x49e2(%rip), %r14      # 0x6fd8
leaq 0x1a07(%rip), %r12      # 0x4004
movl $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl $0x0, 0xc(%rsp)
xorl %r13d, %r13d
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2400
testl %eax, %eax
jle 0x2681
leal -0x67(%rax), %ecx
cmpl $0x8, %ecx
ja 0x2649
movslq (%r12,%rcx,4), %rax
addq %r12, %rax
jmpq *%rax
movq (%r14), %r13
jmp 0x2622
cmpl $0x75, %eax
je 0x2677
cmpl $0x63, %eax
jne 0x27fb
incl 0xc(%rsp)
jmp 0x2622
incl 0x8(%rsp)
jmp 0x2622
movq (%r14), %rax
movq %rax, 0x20(%rsp)
jmp 0x2622
movq (%r14), %rax
movq %rax, 0x10(%rsp)
jmp 0x2622
movq (%r14), %rax
movq %rax, 0x18(%rsp)
jmp 0x2622
movq 0x4948(%rip), %r14      # 0x6fd0
cmpl %ebp, (%r14)
jge 0x27fb
callq 0x2100
movl %eax, %r15d
callq 0x24c0
testl %eax, %eax
je 0x280f
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x2803
callq 0x2300
movl %eax, %edi
callq 0x23a0
testl %eax, %eax
js 0x292c
callq 0x21a0
movl %eax, %edi
callq 0x2450
testl %eax, %eax
js 0x292c
pushq $0x4
popq %rdi
pushq $0x1
popq %rsi
xorl %eax, %eax
callq 0x22e0
movl $0x10000000, %edi       # imm = 0x10000000
callq 0x20d0
testl %eax, %eax
js 0x2858
movl $0x2000000, %edi        # imm = 0x2000000
callq 0x20d0
testl %eax, %eax
js 0x2864
movl $0x8000000, %edi        # imm = 0x8000000
callq 0x20d0
testl %eax, %eax
js 0x2870
cmpl $0x0, 0x8(%rsp)
jne 0x2735
movl $0x40000000, %edi       # imm = 0x40000000
callq 0x20d0
testl %eax, %eax
js 0x295b
movl $0x20000, %edi          # imm = 0x20000
callq 0x20d0
testl %eax, %eax
js 0x287c
movl $0x80, %edi
callq 0x20d0
testl %eax, %eax
js 0x2888
movl $0x4000000, %edi        # imm = 0x4000000
callq 0x20d0
testl %eax, %eax
js 0x2894
movl %ebp, %edi
callq 0x3f84
pushq $0x12
popq %rsi
movl %ebp, %edi
callq 0x22f0
movl %ebp, %edi
callq 0x3f52
xorl %edi, %edi
callq 0x23a0
xorl %edi, %edi
xorl %esi, %esi
callq 0x2220
xorl %edi, %edi
callq 0x2450
cmpl $0x0, 0xc(%rsp)
je 0x27a6
pushq $-0x1
popq %rbp
jmp 0x27ad
callq 0x29d0
movl %eax, %ebp
movslq (%r14), %rax
movq (%rbx,%rax,8), %rdi
movl %ebp, %esi
movq 0x20(%rsp), %rdx
callq 0x386c
movl $0x20000000, %edi       # imm = 0x20000000
callq 0x20d0
testl %eax, %eax
js 0x28a0
callq 0x24c0
testl %eax, %eax
je 0x28ac
cmpl $-0x1, %eax
je 0x2803
movl %eax, %edi
movl %ebp, %esi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2b23
movq (%rbx), %rdi
callq 0x29a9
leaq 0x182b(%rip), %rsi      # 0x4035
jmp 0x2951
pushq $0x13
popq %rdi
callq 0x2060
callq 0x21e0
testl %eax, %eax
je 0x2828
movl %r15d, %edi
callq 0x2f10
movl %r15d, %edi
xorl %esi, %esi
movq %r13, %rdx
callq 0x2f76
pushq $0x1
popq %rsi
movl %r15d, %edi
movq 0x18(%rsp), %rdx
callq 0x2f76
cmpq $0x0, 0x10(%rsp)
jne 0x290c
xorl %edi, %edi
callq 0x2440
leaq 0x1808(%rip), %rsi      # 0x4067
jmp 0x2933
leaq 0x181d(%rip), %rsi      # 0x4088
jmp 0x2933
leaq 0x1834(%rip), %rsi      # 0x40ab
jmp 0x2933
leaq 0x186c(%rip), %rsi      # 0x40ef
jmp 0x2933
leaq 0x1882(%rip), %rsi      # 0x4111
jmp 0x2933
leaq 0x1897(%rip), %rsi      # 0x4132
jmp 0x2933
leaq 0x18ab(%rip), %rsi      # 0x4152
jmp 0x2933
callq 0x3bb4
cmpl $0x0, 0x8(%rsp)
jne 0x28bd
callq 0x3c0c
callq 0x3b15
testl %ebp, %ebp
js 0x28d9
movl %ebp, %edi
callq 0x2210
leaq 0x189e(%rip), %rdi      # 0x4172
callq 0x2a7e
callq 0x2150
leaq 0x189a(%rip), %rdi      # 0x417f
callq 0x2250
movslq (%r14), %rax
movq 0x8(%rbx,%rax,8), %rdi
testq %rdi, %rdi
jne 0x293d
leaq 0x1756(%rip), %rdi      # 0x4054
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2480
jmp 0x294a
callq 0x2300
movl %eax, %edi
callq 0x23a0
testl %eax, %eax
js 0x292c
callq 0x21a0
movl %eax, %edi
callq 0x2450
testl %eax, %eax
jns 0x2964
leaq 0x1707(%rip), %rsi      # 0x403a
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x21b0
leaq (%rbx,%rax,8), %rsi
addq $0x8, %rsi
callq 0x24b0
leaq 0x1840(%rip), %rsi      # 0x4191
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x2310
leaq 0x1769(%rip), %rsi      # 0x40cb
jmp 0x2933
pushq $0x4
popq %rdi
pushq $0x1
popq %rbx
movl %ebx, %esi
xorl %eax, %eax
callq 0x22e0
leaq 0x16da(%rip), %rdi      # 0x4054
leaq 0x16db(%rip), %rdx      # 0x405c
movq %rdi, %rsi
movq 0x10(%rsp), %r14
movq %r14, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x2470
leaq 0x16c2(%rip), %rsi      # 0x405f
movl %ebx, %edi
movq %r14, %rdx
xorl %eax, %eax
callq 0x2310