// File: AndreRenaud[P]simple_uart/./acutest.h
// Function: acutest_run_
static void
acutest_run_(const struct acutest_test_* test, int index, int master_index)
{
    enum acutest_state_ state = ACUTEST_STATE_FAILED;
    acutest_timer_type_ start, end;

    acutest_current_test_ = test;
    acutest_test_already_logged_ = 0;
    acutest_timer_get_time_(&start);

    if(!acutest_no_exec_) {

#if defined(ACUTEST_UNIX_)

        pid_t pid;
        int exit_code;

        /* Make sure the child starts with empty I/O buffers. */
        fflush(stdout);
        fflush(stderr);

        pid = fork();
        if(pid == (pid_t)-1) {
            acutest_error_("Cannot fork. %s [%d]", strerror(errno), errno);
        } else if(pid == 0) {
            /* Child: Do the test. */
            acutest_worker_ = 1;
            state = acutest_do_run_(test, index);
            acutest_exit_((int) state);
        } else {
            /* Parent: Wait until child terminates and analyze its exit code. */
            waitpid(pid, &exit_code, 0);
            if(WIFEXITED(exit_code)) {
                state = (enum acutest_state_) WEXITSTATUS(exit_code);
            } else if(WIFSIGNALED(exit_code)) {
                char tmp[32];
                const char* signame;
                switch(WTERMSIG(exit_code)) {
                    case SIGINT:  signame = "SIGINT"; break;
                    case SIGHUP:  signame = "SIGHUP"; break;
                    case SIGQUIT: signame = "SIGQUIT"; break;
                    case SIGABRT: signame = "SIGABRT"; break;
                    case SIGKILL: signame = "SIGKILL"; break;
                    case SIGSEGV: signame = "SIGSEGV"; break;
                    case SIGILL:  signame = "SIGILL"; break;
                    case SIGTERM: signame = "SIGTERM"; break;
                    default:      snprintf(tmp, sizeof(tmp), "signal %d", WTERMSIG(exit_code)); signame = tmp; break;
                }
                acutest_error_("Test interrupted by %s.", signame);
            } else {
                acutest_error_("Test ended in an unexpected way [%d].", exit_code);
            }
        }

#elif defined(ACUTEST_WIN_)

        char buffer[512] = {0};
        STARTUPINFOA startupInfo;
        PROCESS_INFORMATION processInfo;
        DWORD exitCode;

        /* Windows has no fork(). So we propagate all info into the child
         * through a command line arguments. */
        snprintf(buffer, sizeof(buffer),
                 "%s --worker=%d %s --no-exec --no-summary %s --verbose=%d --color=%s -- \"%s\"",
                 acutest_argv0_, index, acutest_timer_ ? "--time" : "",
                 acutest_tap_ ? "--tap" : "", acutest_verbose_level_,
                 acutest_colorize_ ? "always" : "never",
                 test->name);
        memset(&startupInfo, 0, sizeof(startupInfo));
        startupInfo.cb = sizeof(STARTUPINFO);
        if(CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo)) {
            WaitForSingleObject(processInfo.hProcess, INFINITE);
            GetExitCodeProcess(processInfo.hProcess, &exitCode);
            CloseHandle(processInfo.hThread);
            CloseHandle(processInfo.hProcess);
            switch(exitCode) {
                case 0:             state = ACUTEST_STATE_SUCCESS; break;
                case 1:             state = ACUTEST_STATE_FAILED; break;
                case 2:             state = ACUTEST_STATE_SKIPPED; break;
                case 3:             acutest_error_("Aborted."); break;
                case 0xC0000005:    acutest_error_("Access violation."); break;
                default:            acutest_error_("Test ended in an unexpected way [%lu].", exitCode); break;
            }
        } else {
            acutest_error_("Cannot create unit test subprocess [%ld].", GetLastError());
        }

#else

        /* A platform where we don't know how to run child process. */
        state = acutest_do_run_(test, index);

#endif

    } else {
        /* Child processes suppressed through --no-exec. */
        state = acutest_do_run_(test, index);
    }
    acutest_timer_get_time_(&end);

    acutest_current_test_ = NULL;

    acutest_test_data_[master_index].state = state;
    acutest_test_data_[master_index].duration = acutest_timer_diff_(start, end);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl $0x1, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, 0x8d31(%rip)      # 0xd328
movl $0x0, 0x8d23(%rip)      # 0xd324
leaq -0x28(%rbp), %rdi
callq 0x6190
cmpl $0x0, 0x8bfb(%rip)      # 0xd20c
jne 0x47bf
movq 0x89ba(%rip), %rax      # 0xcfd8
movq (%rax), %rdi
callq 0x2290
movq 0x89cb(%rip), %rax      # 0xcff8
movq (%rax), %rdi
callq 0x2290
callq 0x23a0
movl %eax, -0x3c(%rbp)
cmpl $-0x1, -0x3c(%rbp)
jne 0x4671
callq 0x2050
movl (%rax), %edi
callq 0x2390
movq %rax, -0x70(%rbp)
callq 0x2050
movq -0x70(%rbp), %rsi
movl (%rax), %edx
leaq 0x5b21(%rip), %rdi      # 0xa186
movb $0x0, %al
callq 0x61c0
jmp 0x47bd
cmpl $0x0, -0x3c(%rbp)
jne 0x4698
movl $0x1, 0x8dfb(%rip)      # 0xd47c
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x62c0
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %edi
callq 0x3300
movl -0x3c(%rbp), %edi
leaq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x22d0
movl -0x40(%rbp), %eax
andl $0x7f, %eax
cmpl $0x0, %eax
jne 0x46c4
movl -0x40(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
sarl $0x8, %eax
movl %eax, -0x14(%rbp)
jmp 0x47b9
movl -0x40(%rbp), %eax
andl $0x7f, %eax
addl $0x1, %eax
movsbl %al, %eax
sarl %eax
cmpl $0x0, %eax
jle 0x47a6
movl -0x40(%rbp), %eax
andl $0x7f, %eax
decl %eax
movl %eax, %ecx
movq %rcx, -0x78(%rbp)
subl $0xe, %eax
ja 0x476d
movq -0x78(%rbp), %rax
leaq 0x495b(%rip), %rcx      # 0x9054
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x5a92(%rip), %rax      # 0xa19b
movq %rax, -0x68(%rbp)
jmp 0x4792
leaq 0x5a89(%rip), %rax      # 0xa1a2
movq %rax, -0x68(%rbp)
jmp 0x4792
leaq 0x5a83(%rip), %rax      # 0xa1a9
movq %rax, -0x68(%rbp)
jmp 0x4792
leaq 0x5a7e(%rip), %rax      # 0xa1b1
movq %rax, -0x68(%rbp)
jmp 0x4792
leaq 0x5a79(%rip), %rax      # 0xa1b9
movq %rax, -0x68(%rbp)
jmp 0x4792
leaq 0x5a74(%rip), %rax      # 0xa1c1
movq %rax, -0x68(%rbp)
jmp 0x4792
leaq 0x5a6f(%rip), %rax      # 0xa1c9
movq %rax, -0x68(%rbp)
jmp 0x4792
leaq 0x5a69(%rip), %rax      # 0xa1d0
movq %rax, -0x68(%rbp)
jmp 0x4792
leaq -0x60(%rbp), %rdi
movl -0x40(%rbp), %ecx
andl $0x7f, %ecx
movl $0x20, %esi
leaq 0x5a55(%rip), %rdx      # 0xa1d8
movb $0x0, %al
callq 0x2170
leaq -0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rsi
leaq 0x5a45(%rip), %rdi      # 0xa1e2
movb $0x0, %al
callq 0x61c0
jmp 0x47b7
movl -0x40(%rbp), %esi
leaq 0x5a4a(%rip), %rdi      # 0xa1fa
movb $0x0, %al
callq 0x61c0
jmp 0x47b9
jmp 0x47bb
jmp 0x47bd
jmp 0x47ce
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0x62c0
movl %eax, -0x14(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6190
movq $0x0, 0x8b46(%rip) # 0xd328
movl -0x14(%rbp), %ecx
movq 0x8c7c(%rip), %rax      # 0xd468
movslq -0x10(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x5bb0
movq 0x8c53(%rip), %rax      # 0xd468
movslq -0x10(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd %xmm0, 0x8(%rax)
addq $0x80, %rsp
popq %rbp
retq
nop