// File: /workspace/github_c_makefile_star2_O3/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 0x29f2(%rip), %rdx      # 0x5028
callq 0x2400
xorl %r15d, %r15d
testl %eax, %eax
jle 0x270e
leaq 0x29db(%rip), %r14      # 0x5028
leaq 0x29b0(%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 0x269e
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq 0x5944(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, 0x18(%rsp)
jmp 0x26e7
cmpl $0x75, %eax
je 0x26d8
cmpl $0x63, %eax
jne 0x28b2
incl %r15d
jmp 0x26e7
movq 0x5920(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, 0x20(%rsp)
jmp 0x26e7
incl %r12d
jmp 0x26e7
movq 0x590a(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0x26e7
movq 0x58f9(%rip), %rax      # 0x7fd8
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2400
testl %eax, %eax
jg 0x267b
testl %r12d, %r12d
sete %r13b
testl %r15d, %r15d
sete 0x7(%rsp)
movq 0x20(%rsp), %r15
jmp 0x2734
movb $0x1, %r13b
movq $0x0, 0x10(%rsp)
movq $0x0, 0x8(%rsp)
xorl %r15d, %r15d
movb $0x1, 0x7(%rsp)
movq $0x0, 0x18(%rsp)
movq 0x5895(%rip), %r12      # 0x7fd0
cmpl %ebp, (%r12)
jge 0x28c6
callq 0x2100
movl %eax, %r14d
callq 0x24c0
testl %eax, %eax
je 0x28ce
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x28ba
callq 0x2300
movl %eax, %edi
callq 0x23a0
testl %eax, %eax
js 0x29f0
callq 0x21a0
movl %eax, %edi
callq 0x2450
testl %eax, %eax
js 0x29f0
movl $0x4, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x22e0
movl $0x10000000, %edi       # imm = 0x10000000
callq 0x20d0
testl %eax, %eax
js 0x291d
movl $0x2000000, %edi        # imm = 0x2000000
callq 0x20d0
testl %eax, %eax
js 0x2929
movl $0x8000000, %edi        # imm = 0x8000000
callq 0x20d0
testl %eax, %eax
js 0x2935
testb %r13b, %r13b
je 0x27eb
movl $0x40000000, %edi       # imm = 0x40000000
callq 0x20d0
testl %eax, %eax
js 0x2a03
movl $0x20000, %edi          # imm = 0x20000
callq 0x20d0
testl %eax, %eax
js 0x2941
movl $0x80, %edi
callq 0x20d0
testl %eax, %eax
js 0x294d
movl $0x4000000, %edi        # imm = 0x4000000
callq 0x20d0
testl %eax, %eax
js 0x2959
movl %ebp, %edi
callq 0x421b
movl %ebp, %edi
movl $0x12, %esi
callq 0x22f0
movl %ebp, %edi
callq 0x41e7
xorl %edi, %edi
callq 0x23a0
xorl %edi, %edi
xorl %esi, %esi
callq 0x2220
xorl %edi, %edi
callq 0x2450
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpb $0x0, 0x7(%rsp)
je 0x2865
callq 0x2a78
movl %eax, %ebp
movslq (%r12), %rax
movq (%rbx,%rax,8), %rdi
movl %ebp, %esi
movq %r15, %rdx
callq 0x3ad4
movl $0x20000000, %edi       # imm = 0x20000000
callq 0x20d0
testl %eax, %eax
js 0x2965
callq 0x24c0
testl %eax, %eax
je 0x2971
cmpl $-0x1, %eax
je 0x28ba
movl %eax, %edi
movl %ebp, %esi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2bec
movq %rbx, %rdi
callq 0x2504
leaq 0x2774(%rip), %rsi      # 0x5035
jmp 0x2a20
movq %rbx, %rdi
callq 0x24e0
movl $0x13, %edi
callq 0x2060
callq 0x21e0
testl %eax, %eax
je 0x28e9
movl %r14d, %edi
callq 0x304c
movl %r14d, %edi
xorl %esi, %esi
movq 0x18(%rsp), %rdx
callq 0x30b8
movl %r14d, %edi
movl $0x1, %esi
movq 0x10(%rsp), %rdx
callq 0x30b8
cmpq $0x0, 0x8(%rsp)
jne 0x29d0
xorl %edi, %edi
callq 0x2440
leaq 0x2743(%rip), %rsi      # 0x5067
jmp 0x29f7
leaq 0x2758(%rip), %rsi      # 0x5088
jmp 0x29f7
leaq 0x276f(%rip), %rsi      # 0x50ab
jmp 0x29f7
leaq 0x27a7(%rip), %rsi      # 0x50ef
jmp 0x29f7
leaq 0x27bd(%rip), %rsi      # 0x5111
jmp 0x29f7
leaq 0x27d2(%rip), %rsi      # 0x5132
jmp 0x29f7
leaq 0x27e6(%rip), %rsi      # 0x5152
jmp 0x29f7
callq 0x3e34
testb %r13b, %r13b
je 0x2980
callq 0x3e8e
callq 0x3d89
testl %ebp, %ebp
js 0x299c
movl %ebp, %edi
callq 0x2210
leaq 0x27db(%rip), %rdi      # 0x5172
callq 0x2b3d
callq 0x2150
leaq 0x27d7(%rip), %rdi      # 0x517f
callq 0x2250
movslq (%r12), %rax
movq 0x8(%rbx,%rax,8), %rdi
testq %rdi, %rdi
jne 0x2a0c
leaq 0x2692(%rip), %rdi      # 0x5054
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2480
jmp 0x2a19
callq 0x2300
movl %eax, %edi
callq 0x23a0
testl %eax, %eax
js 0x29f0
callq 0x21a0
movl %eax, %edi
callq 0x2450
testl %eax, %eax
jns 0x2a2c
leaq 0x2643(%rip), %rsi      # 0x503a
movl $0x1, %edi
xorl %eax, %eax
callq 0x21b0
leaq 0x26c1(%rip), %rsi      # 0x50cb
jmp 0x29f7
leaq (%rbx,%rax,8), %rsi
addq $0x8, %rsi
callq 0x24b0
leaq 0x2771(%rip), %rsi      # 0x5191
movl $0x1, %edi
xorl %eax, %eax
callq 0x2310
movl $0x4, %edi
movl $0x1, %esi
xorl %eax, %eax
callq 0x22e0
leaq 0x2610(%rip), %rdi      # 0x5054
leaq 0x2611(%rip), %rdx      # 0x505c
movq %rdi, %rsi
movq 0x8(%rsp), %rbx
movq %rbx, %rcx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x2470
leaq 0x25f8(%rip), %rsi      # 0x505f
movl $0x1, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2310
nop