// File: /workspace/github_c_makefile_star2_O1/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
leaq 0x29e2(%rip), %rdx      # 0x5028
callq 0x2410
xorl %r15d, %r15d
testl %eax, %eax
jle 0x271e
leaq 0x29cb(%rip), %r14      # 0x5028
leaq 0x29a0(%rip), %r13      # 0x5004
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x10(%rsp)
leal -0x67(%rax), %ecx
cmpl $0x8, %ecx
ja 0x26ae
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq 0x5934(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0x26f7
cmpl $0x75, %eax
je 0x26e8
cmpl $0x63, %eax
jne 0x28c2
incl %r15d
jmp 0x26f7
movq 0x5910(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x26f7
incl %r12d
jmp 0x26f7
movq 0x58fa(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0x26f7
movq 0x58e9(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2410
testl %eax, %eax
jg 0x268b
testl %r12d, %r12d
sete %r13b
testl %r15d, %r15d
sete 0x7(%rsp)
movq 0x20(%rsp), %r15
jmp 0x2744
movb $0x1, %r13b
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r15d, %r15d
movb $0x1, 0x7(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x5885(%rip), %r12      # 0x7fd0
cmpl %ebp, (%r12)
jge 0x28d6
callq 0x2100
movl %eax, %r14d
callq 0x24d0
testl %eax, %eax
je 0x28de
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x28ca
callq 0x2310
movl %eax, %edi
callq 0x23b0
testl %eax, %eax
js 0x2a00
callq 0x21a0
movl %eax, %edi
callq 0x2460
testl %eax, %eax
js 0x2a00
movl $0x4, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x22f0
movl $0x10000000, %edi       # imm = 0x10000000
callq 0x20d0
testl %eax, %eax
js 0x292d
movl $0x2000000, %edi        # imm = 0x2000000
callq 0x20d0
testl %eax, %eax
js 0x2939
movl $0x8000000, %edi        # imm = 0x8000000
callq 0x20d0
testl %eax, %eax
js 0x2945
testb %r13b, %r13b
je 0x27fb
movl $0x40000000, %edi       # imm = 0x40000000
callq 0x20d0
testl %eax, %eax
js 0x2a13
movl $0x20000, %edi          # imm = 0x20000
callq 0x20d0
testl %eax, %eax
js 0x2951
movl $0x80, %edi
callq 0x20d0
testl %eax, %eax
js 0x295d
movl $0x4000000, %edi        # imm = 0x4000000
callq 0x20d0
testl %eax, %eax
js 0x2969
movl %ebp, %edi
callq 0x428f
movl %ebp, %edi
movl $0x12, %esi
callq 0x2300
movl %ebp, %edi
callq 0x425b
xorl %edi, %edi
callq 0x23b0
xorl %edi, %edi
xorl %esi, %esi
callq 0x2220
xorl %edi, %edi
callq 0x2460
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x0, 0x7(%rsp)
je 0x2875
callq 0x2a88
movl %eax, %ebp
movslq (%r12), %rax
movq (%rbx,%rax,8), %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x3b48
movl $0x20000000, %edi       # imm = 0x20000000
callq 0x20d0
testl %eax, %eax
js 0x2975
callq 0x24d0
testl %eax, %eax
je 0x2981
cmpl $-0x1, %eax
je 0x28ca
movl %eax, %edi
movl %ebp, %esi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2bfc
movq %rbx, %rdi
callq 0x2514
leaq 0x2764(%rip), %rsi      # 0x5035
jmp 0x2a30
movq %rbx, %rdi
callq 0x24f0
movl $0x13, %edi
callq 0x2060
callq 0x21e0
testl %eax, %eax
je 0x28f9
movl %r14d, %edi
callq 0x3068
movl %r14d, %edi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
callq 0x30d4
movl %r14d, %edi
movl $0x1, %esi
movq 0x10(%rsp), %rdx
callq 0x30d4
cmpq $0x0, 0x8(%rsp)
jne 0x29e0
xorl %edi, %edi
callq 0x2450
leaq 0x2733(%rip), %rsi      # 0x5067
jmp 0x2a07
leaq 0x2748(%rip), %rsi      # 0x5088
jmp 0x2a07
leaq 0x275f(%rip), %rsi      # 0x50ab
jmp 0x2a07
leaq 0x2797(%rip), %rsi      # 0x50ef
jmp 0x2a07
leaq 0x27ad(%rip), %rsi      # 0x5111
jmp 0x2a07
leaq 0x27c2(%rip), %rsi      # 0x5132
jmp 0x2a07
leaq 0x27d6(%rip), %rsi      # 0x5152
jmp 0x2a07
callq 0x3ea8
testb %r13b, %r13b
je 0x2990
callq 0x3f02
callq 0x3dfd
testl %ebp, %ebp
js 0x29ac
movl %ebp, %edi
callq 0x2210
leaq 0x27cb(%rip), %rdi      # 0x5172
callq 0x2b4d
callq 0x2150
leaq 0x27c7(%rip), %rdi      # 0x517f
callq 0x2260
movslq (%r12), %rax
movq 0x8(%rbx,%rax,8), %rdi
testq %rdi, %rdi
jne 0x2a1c
leaq 0x2682(%rip), %rdi      # 0x5054
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2490
jmp 0x2a29
callq 0x2310
movl %eax, %edi
callq 0x23b0
testl %eax, %eax
js 0x2a00
callq 0x21a0
movl %eax, %edi
callq 0x2460
testl %eax, %eax
jns 0x2a3c
leaq 0x2633(%rip), %rsi      # 0x503a
movl $0x1, %edi
xorl %eax, %eax
callq 0x21b0
leaq 0x26b1(%rip), %rsi      # 0x50cb
jmp 0x2a07
leaq (%rbx,%rax,8), %rsi
addq $0x8, %rsi
callq 0x24c0
leaq 0x2761(%rip), %rsi      # 0x5191
movl $0x1, %edi
xorl %eax, %eax
callq 0x2320
movl $0x4, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x22f0
leaq 0x2600(%rip), %rdi      # 0x5054
leaq 0x2601(%rip), %rdx      # 0x505c
movq %rdi, %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x2480
leaq 0x25e8(%rip), %rsi      # 0x505f
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2320
nop