// File: /workspace/github_c_makefile_star2_O2/motet-a[P]libforks/tests/main.c
// Function: main
int main() {
  DIR *dir = opendir(".");
  check(dir);

  int exit_status = 0;

  const struct dirent *entry;
  while ((entry = readdir(dir)) != NULL) {
    if (strchr(entry->d_name, '.')) {
      continue;
    }
    if (strcmp(entry->d_name, "main") == 0) {
      continue;
    }

    printf("testing %s...", entry->d_name);
    fflush(stdout);
    char exec_path[64];
    check(snprintf(exec_path, sizeof exec_path, "./%s", entry->d_name) != -1);

    char *argv[] = {exec_path, NULL};
    char *envp[] = {
      "MALLOC_OPTIONS=S", // security options for OpenBSD
      NULL
    };

    pid_t child_pid = fork();
    check(child_pid != -1);

    if (child_pid == 0) {
      check(setpgid(getpid(), getpid()) == 0);
      check(execve(exec_path, argv, envp));
      abort();
    }

    char failure[99];
    failure[0] = '\0';

    int wait_status;
    check(waitpid(child_pid, &wait_status, 0) > 0);
    if (WIFEXITED(wait_status)) {
      if (WEXITSTATUS(wait_status) != 0) {
        snprintf(failure, sizeof failure, "exited with status %d", WEXITSTATUS(wait_status));
      }
    } else if (WIFSIGNALED(wait_status)) {
      snprintf(failure, sizeof failure, "received signal %d", WTERMSIG(wait_status));
    } else {
      abort();
    }

    // kill the whole process group
    int kill_res = killpg(child_pid, SIGKILL);
    if (kill_res == 0) {
      snprintf(failure, sizeof failure, "children were still running");
    } else {
      if (errno != ESRCH) {
        perror("killpg");
        abort();
      }
    }

    if (*failure) {
      exit_status = 1;
      printf(" error: %s\n", failure);
    } else {
      printf(" ok\n");
    }
  }

  check(closedir(dir) == 0);

  return exit_status;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
leaq 0x1c72(%rip), %rdi      # 0x3026
callq 0x10b0
testq %rax, %rax
je 0x1579
movq %rax, %rbx
pushq $0x2e
popq %rbp
leaq 0x1c46(%rip), %r14      # 0x3015
leaq 0xa0(%rsp), %r13
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x11d0
testq %rax, %rax
je 0x1558
movq %rax, %r15
addq $0x13, %r15
movq %r15, %rdi
movl %ebp, %esi
callq 0x10c0
testq %rax, %rax
jne 0x13da
movq %r15, %rdi
movq %r14, %rsi
callq 0x1160
testl %eax, %eax
je 0x13da
leaq 0x1c03(%rip), %rdi      # 0x301a
movq %r15, %rsi
xorl %eax, %eax
callq 0x10d0
movq 0x4ba8(%rip), %rax      # 0x5fd0
movq (%rax), %rdi
callq 0x11f0
movq %r13, %rdi
pushq $0x40
popq %rsi
leaq 0x1beb(%rip), %rdx      # 0x3028
movq %r15, %rcx
xorl %eax, %eax
callq 0x10e0
cmpl $-0x1, %eax
je 0x15e7
movq %r13, 0x10(%rsp)
andq $0x0, 0x18(%rsp)
movaps 0x496e(%rip), %xmm0     # 0x5dd0
movaps %xmm0, 0x20(%rsp)
callq 0x1290
testl %eax, %eax
je 0x15a8
movl %eax, %r15d
cmpl $-0x1, %eax
je 0x159d
movb $0x0, 0x30(%rsp)
movl %r15d, %edi
leaq 0xc(%rsp), %rsi
xorl %edx, %edx
callq 0x1230
testl %eax, %eax
jle 0x1592
movl 0xc(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x14cd
movl %ecx, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x15f2
leaq 0x30(%rsp), %rdi
pushq $0x63
popq %rsi
leaq 0x1c43(%rip), %rdx      # 0x310e
jmp 0x14e8
shrl $0x8, %eax
andl $0xff, %eax
je 0x14ef
leaq 0x30(%rsp), %rdi
pushq $0x63
popq %rsi
leaq 0x1c12(%rip), %rdx      # 0x30f8
movl %eax, %ecx
xorl %eax, %eax
callq 0x10e0
movl %r15d, %edi
pushq $0x9
popq %rsi
callq 0x1200
testl %eax, %eax
je 0x1524
callq 0x1050
cmpl $0x3, (%rax)
jne 0x15f7
cmpb $0x0, 0x30(%rsp)
jne 0x153c
leaq 0x1c65(%rip), %rdi      # 0x317f
callq 0x1070
jmp 0x13da
movups 0x1c02(%rip), %xmm0     # 0x312d
movups %xmm0, 0x3c(%rsp)
movups 0x1bea(%rip), %xmm0     # 0x3121
movaps %xmm0, 0x30(%rsp)
leaq 0x1c01(%rip), %rdi      # 0x3144
leaq 0x30(%rsp), %rsi
xorl %eax, %eax
callq 0x10d0
pushq $0x1
popq %r12
jmp 0x13da
movq %rbx, %rdi
callq 0x1130
testl %eax, %eax
jne 0x1587
movl %r12d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a84(%rip), %rdi      # 0x3004
pushq $0x8
jmp 0x1611
leaq 0x1bc2(%rip), %rdi      # 0x3150
pushq $0x4b
jmp 0x1611
leaq 0x1b37(%rip), %rdi      # 0x30d0
pushq $0x2d
jmp 0x1611
leaq 0x1add(%rip), %rdi      # 0x3081
pushq $0x21
jmp 0x1611
callq 0x10a0
movl %eax, %ebx
callq 0x10a0
movl %ebx, %edi
movl %eax, %esi
callq 0x1260
testl %eax, %eax
jne 0x1608
leaq 0xa0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x1150
testl %eax, %eax
jne 0x15f2
leaq 0x1acf(%rip), %rdi      # 0x30b2
pushq $0x25
jmp 0x1611
leaq 0x1a3f(%rip), %rdi      # 0x302d
pushq $0x18
jmp 0x1611
callq 0x1040
leaq 0x1b3f(%rip), %rdi      # 0x313d
callq 0x1250
callq 0x1040
leaq 0x1a82(%rip), %rdi      # 0x3091
pushq $0x24
popq %rsi
callq 0x1617