// File: /workspace/github_c_makefile_star2_O1/arachsys[P]containers/inject.c
// Function: main
int main(int argc, char **argv, char **envp) {
  char *end, *item = NULL, *path;
  pid_t child = -1, parent, pid;
  size_t size;
  struct dirent *entry;
  DIR *dir;
  FILE *file;

  seal(argv, envp);
  if (argc < 2)
    usage(argv[0]);

  parent = strtol(argv[1], &end, 10);
  if (end == argv[1] || *end)
    usage(argv[0]);

  if (geteuid() != getuid())
    errx(EXIT_FAILURE, "setuid installation is unsafe");
  else if (getegid() != getgid())
    errx(EXIT_FAILURE, "setgid installation is unsafe");

  join(parent, "user");
  setgid(0);
  setgroups(0, NULL);
  setuid(0);

  if (!(dir = opendir("/proc")))
    errx(EXIT_FAILURE, "Failed to list processes");
  while (child < 0 && (entry = readdir(dir))) {
    pid = strtol(entry->d_name, &end, 10);
    if (end == entry->d_name || *end)
      continue;
    if (getparent(pid) == parent) {
      path = string("/proc/%u/environ", pid);
      if ((file = fopen(path, "r"))) {
        while (getdelim(&item, &size, '\0', file) >= 0)
          if (strcmp(item, "container=contain") == 0)
            child = pid;
        fclose(file);
      }
      free(path);
    }
  }
  closedir(dir);
  if (item)
    free(item);

  if (child < 0)
    errx(EXIT_FAILURE, "PID %u is not a container supervisor", parent);

  join(child, "cgroup");
  join(child, "ipc");
  join(child, "net");
  join(child, "pid");
  join(child, "time");
  join(child, "uts");
  join(child, "mnt");

  if (chdir("/") < 0)
    errx(EXIT_FAILURE, "Failed to enter container root directory");

  switch (child = fork()) {
    case -1:
      err(EXIT_FAILURE, "fork");
    case 0:
      if (argv[2])
        execvp(argv[2], argv + 2);
      else if (getenv("SHELL"))
        execl(getenv("SHELL"), getenv("SHELL"), NULL);
      else
        execl(SHELL, SHELL, NULL);
      err(EXIT_FAILURE, "exec");
  }

  waitforexit(child);
  return EXIT_SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movl %edi, %ebp
movq $0x0, 0x8(%rsp)
movq %rsi, %rdi
movq %rdx, %rsi
callq 0x3520
cmpl $0x1, %ebp
jle 0x27d8
movq 0x8(%rbx), %rdi
leaq 0x28(%rsp), %r14
movq %r14, %rsi
movl $0xa, %edx
callq 0x21d0
movq %rax, %r15
movq (%r14), %rax
cmpq 0x8(%rbx), %rax
je 0x27d0
cmpb $0x0, (%rax)
jne 0x27d0
callq 0x2130
movl %eax, %ebp
callq 0x2100
cmpl %eax, %ebp
jne 0x27e0
callq 0x2240
movl %eax, %ebp
callq 0x21e0
cmpl %eax, %ebp
jne 0x27e9
movq %rbx, 0x18(%rsp)
leaq 0x1b21(%rip), %rsi      # 0x4040
movq %r15, 0x20(%rsp)
movl %r15d, %edi
callq 0x288c
xorl %edi, %edi
callq 0x2280
xorl %edi, %edi
xorl %esi, %esi
callq 0x2150
xorl %edi, %edi
callq 0x22f0
leaq 0x1afb(%rip), %rdi      # 0x4045
callq 0x20c0
testq %rax, %rax
je 0x27f2
movq %rax, %r15
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movb $0x1, %bl
leaq 0x1b0c(%rip), %r12      # 0x4075
movq %r15, %rdi
callq 0x21f0
testq %rax, %rax
je 0x2714
movq %rax, %r14
addq $0x13, %r14
movq %r14, %rdi
leaq 0x28(%rsp), %rsi
movl $0xa, %edx
callq 0x21d0
movq %rax, %r13
movq 0x28(%rsp), %rax
cmpq %r14, %rax
je 0x25a5
cmpb $0x0, (%rax)
je 0x25af
testb $0x1, %bl
jne 0x2569
jmp 0x2714
movq $0x0, 0x10(%rsp)
leaq 0x1be4(%rip), %rdi      # 0x41a3
movl %r13d, %esi
xorl %eax, %eax
callq 0x3612
movq %rax, %rbx
movq %rax, %rdi
leaq 0x1c1c(%rip), %rsi      # 0x41f2
callq 0x22b0
movq %rax, %r14
movq %rbx, %rdi
callq 0x2040
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testq %r14, %r14
je 0x2656
leaq 0x10(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x22d0
testq %rax, %rax
js 0x2656
movq 0x10(%rsp), %rdi
movl $0x29, %esi
callq 0x2120
testq %rax, %rax
je 0x2656
movq %rax, (%rsp)
movq %rax, %rdi
callq 0x20e0
cmpq $0x4, %rax
jb 0x2656
movq (%rsp), %rax
leaq 0x4(%rax), %rdi
leaq 0x38(%rsp), %rsi
movl $0xa, %edx
callq 0x21d0
movq 0x38(%rsp), %rcx
cmpq (%rsp), %rcx
je 0x2656
cmpb $0x20, (%rcx)
jne 0x2656
movl %eax, %ebx
testq %r14, %r14
je 0x2663
movq %r14, %rdi
callq 0x20b0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2672
callq 0x2040
cmpl 0x20(%rsp), %ebx
jne 0x2707
leaq 0x19e1(%rip), %rdi      # 0x4064
movl %r13d, %esi
xorl %eax, %eax
callq 0x3612
movq %rax, %rbx
movq %rax, %rdi
leaq 0x1b58(%rip), %rsi      # 0x41f2
callq 0x22b0
testq %rax, %rax
je 0x26ff
movq %rax, %r14
movq %rbx, (%rsp)
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x2320
testq %rax, %rax
js 0x26f3
leaq 0x30(%rsp), %rbx
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x2180
testl %eax, %eax
cmovel %r13d, %ebp
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %r14, %rcx
callq 0x2320
testq %rax, %rax
jns 0x26c9
movq %r14, %rdi
callq 0x20b0
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x2040
movl %ebp, %ebx
shrl $0x1f, %ebx
testl %ebp, %ebp
js 0x2569
movq %r15, %rdi
callq 0x2160
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x272b
callq 0x2040
testb $0x1, %bl
jne 0x27fb
leaq 0x1971(%rip), %rsi      # 0x40ac
movl %ebp, %edi
callq 0x288c
leaq 0x196a(%rip), %rsi      # 0x40b3
movl %ebp, %edi
callq 0x288c
leaq 0x1960(%rip), %rsi      # 0x40b7
movl %ebp, %edi
callq 0x288c
leaq 0x1c95(%rip), %rsi      # 0x43fa
movl %ebp, %edi
callq 0x288c
leaq 0x1948(%rip), %rsi      # 0x40bb
movl %ebp, %edi
callq 0x288c
leaq 0x193f(%rip), %rsi      # 0x40c0
movl %ebp, %edi
callq 0x288c
leaq 0x1935(%rip), %rsi      # 0x40c4
movl %ebp, %edi
callq 0x288c
leaq 0x192b(%rip), %rdi      # 0x40c8
callq 0x20f0
testl %eax, %eax
js 0x2813
callq 0x2330
testl %eax, %eax
je 0x2826
cmpl $-0x1, %eax
je 0x2851
movl %eax, %edi
callq 0x3713
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x2350
movq %rbx, %rdi
callq 0x2374
leaq 0x181d(%rip), %rsi      # 0x4004
jmp 0x281a
leaq 0x1832(%rip), %rsi      # 0x4022
jmp 0x281a
leaq 0x1852(%rip), %rsi      # 0x404b
jmp 0x281a
leaq 0x1885(%rip), %rsi      # 0x4087
movl $0x1, %edi
movq 0x20(%rsp), %rdx
xorl %eax, %eax
callq 0x2110
leaq 0x18b0(%rip), %rsi      # 0x40ca
movl $0x1, %edi
xorl %eax, %eax
callq 0x2110
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rdi
testq %rdi, %rdi
jne 0x285a
leaq 0x18bd(%rip), %rdi      # 0x40f8
callq 0x2030
testq %rax, %rax
jne 0x286a
leaq 0x18b2(%rip), %rdi      # 0x40fe
movq %rdi, %rsi
jmp 0x2870
leaq 0x189b(%rip), %rsi      # 0x40f3
jmp 0x2880
movq 0x18(%rsp), %rsi
addq $0x10, %rsi
callq 0x22c0
jmp 0x2879
movq %rax, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x2310
leaq 0x1886(%rip), %rsi      # 0x4106
movl $0x1, %edi
xorl %eax, %eax
callq 0x2200