// File: /workspace/github_c_makefile_star2_O1/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 0x1c42(%rip), %rdi      # 0x3026
callq 0x10b0
testq %rax, %rax
je 0x15cb
movq %rax, %rbx
movq %rax, %rdi
callq 0x11d0
testq %rax, %rax
je 0x15a7
movq %rax, %rbp
leaq 0x1c05(%rip), %r14      # 0x3015
leaq 0xa0(%rsp), %r13
leaq 0x30(%rsp), %r12
xorl %r15d, %r15d
addq $0x13, %rbp
movq %rbp, %rdi
movl $0x2e, %esi
callq 0x10c0
testq %rax, %rax
jne 0x1445
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1160
testl %eax, %eax
jne 0x145a
movq %rbx, %rdi
callq 0x11d0
movq %rax, %rbp
testq %rax, %rax
jne 0x1420
jmp 0x15aa
leaq 0x1bb9(%rip), %rdi      # 0x301a
movq %rbp, %rsi
xorl %eax, %eax
callq 0x10d0
movq 0x4b5e(%rip), %rax      # 0x5fd0
movq (%rax), %rdi
callq 0x11f0
movl $0x40, %esi
movq %r13, %rdi
leaq 0x1b9f(%rip), %rdx      # 0x3028
movq %rbp, %rcx
xorl %eax, %eax
callq 0x10e0
cmpl $-0x1, %eax
je 0x1654
movq %r13, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movaps 0x491f(%rip), %xmm0     # 0x5dd0
movaps %xmm0, 0x20(%rsp)
callq 0x1290
testl %eax, %eax
je 0x160f
movl %eax, %ebp
cmpl $-0x1, %eax
je 0x15fe
movb $0x0, 0x30(%rsp)
movl %ebp, %edi
leaq 0xc(%rsp), %rsi
xorl %edx, %edx
callq 0x1230
testl %eax, %eax
jle 0x15ed
movl 0xc(%rsp), %eax
movl %eax, %ecx
andl $0x7f, %ecx
je 0x151a
movl %ecx, %eax
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x1665
movl $0x63, %esi
movq %r12, %rdi
leaq 0x1bf6(%rip), %rdx      # 0x310e
jmp 0x1535
shrl $0x8, %eax
andl $0xff, %eax
je 0x153c
movl $0x63, %esi
movq %r12, %rdi
leaq 0x1bc5(%rip), %rdx      # 0x30f8
movl %eax, %ecx
xorl %eax, %eax
callq 0x10e0
movl %ebp, %edi
movl $0x9, %esi
callq 0x1200
testl %eax, %eax
je 0x155b
callq 0x1050
cmpl $0x3, (%rax)
je 0x1573
jmp 0x166a
movups 0x1bcb(%rip), %xmm0     # 0x312d
movups %xmm0, 0x3c(%rsp)
movups 0x1bb3(%rip), %xmm0     # 0x3121
movaps %xmm0, 0x30(%rsp)
cmpb $0x0, 0x30(%rsp)
je 0x1596
leaq 0x1bc3(%rip), %rdi      # 0x3144
movq %r12, %rsi
xorl %eax, %eax
callq 0x10d0
movl $0x1, %r15d
jmp 0x1445
leaq 0x1be2(%rip), %rdi      # 0x317f
callq 0x1070
jmp 0x1445
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x1130
testl %eax, %eax
jne 0x15dc
movl %r15d, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1a32(%rip), %rdi      # 0x3004
movl $0x8, %esi
callq 0x1680
leaq 0x1b6d(%rip), %rdi      # 0x3150
movl $0x4b, %esi
callq 0x1680
leaq 0x1adc(%rip), %rdi      # 0x30d0
movl $0x2d, %esi
callq 0x1680
leaq 0x1a7c(%rip), %rdi      # 0x3081
movl $0x21, %esi
callq 0x1680
callq 0x10a0
movl %eax, %ebx
callq 0x10a0
movl %ebx, %edi
movl %eax, %esi
callq 0x1260
testl %eax, %eax
jne 0x166f
leaq 0xa0(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x1150
testl %eax, %eax
jne 0x1665
leaq 0x1a68(%rip), %rdi      # 0x30b2
movl $0x25, %esi
callq 0x1680
leaq 0x19d2(%rip), %rdi      # 0x302d
movl $0x18, %esi
callq 0x1680
callq 0x1040
callq 0x12b0
leaq 0x1a1b(%rip), %rdi      # 0x3091
movl $0x24, %esi
callq 0x1680