// File: /workspace/github_c_makefile_star2_O0/adsr[P]phpspy/top.c
// Function: main_top
int main_top(int argc, char **argv) {
    fd_set readfds;
    int ttyfd, outfd, errfd, maxfd;
    pid_t pid;
    struct timeval timeout;
    struct timespec ts, last_display;
    struct tb_event event;
    int rc, i;
    /* TODO consider doing this by aggregating in-process instead of fork/stdout */

    outfd = errfd = ttyfd = pid = -1;

    if (opt_pid == -1 && opt_pgrep_args == NULL && optind >= argc) {
        /* TODO DRY with main() */
        log_error("Expected pid (-p), pgrep (-P), or command\n\n");
        usage(stderr, 1);
        return 1;
    }

    /* TODO refactor this function */
    filter_child_args(argc, argv);
    snprintf(phpspy_args, BUF_SIZE, "phpspy ");
    for (i = 1; i < argc; i++) {
        snprintf(phpspy_args+strlen(phpspy_args), BUF_SIZE-strlen(phpspy_args), "%s ", argv[i]);
    }

    if ((ttyfd = open("/dev/tty", O_RDONLY)) < 0) {
        perror("open");
        return 1;
    }
    if ((fork_child(argc, argv, &pid, &outfd, &errfd)) < 0) {
        return 1;
    }
    maxfd = PHPSPY_MAX(PHPSPY_MAX(ttyfd, outfd), errfd);

    last_display.tv_sec = 0;
    last_display.tv_nsec = 0;

    tb_init(); /* TODO support for ncurses for portability sake? */
    while (!done) {
        clock_gettime(CLOCK_MONOTONIC, &ts);
        if (last_display.tv_sec == 0 || ts.tv_sec - last_display.tv_sec >= 1) {
            display();
            last_display = ts;
        }
        FD_ZERO(&readfds);
        FD_SET(ttyfd, &readfds);
        FD_SET(outfd, &readfds);
        FD_SET(errfd, &readfds);
        timeout.tv_sec = 1;
        timeout.tv_usec = 0;
        rc = select(maxfd + 1, &readfds, NULL, NULL, &timeout);
        if (rc < 0) {
            if (errno == EINTR) {
                /* Probably due to SIGWINCH (terminal resize) */
                /* Call tb_peek_event to let termbox update dimensions */
                tb_peek_event(&event, 0);
                last_display.tv_sec = 0; /* Immediately re-display */
                continue;
            }
            perror("select");
            break;
        }
        if (FD_ISSET(outfd, &readfds)) {
            read_child_out(outfd);
        }
        if (FD_ISSET(errfd, &readfds)) {
            read_child_err(errfd);
        }
        if (FD_ISSET(ttyfd, &readfds)) {
            event.type = 0;
            tb_peek_event(&event, 0);
            handle_event(&event);
        }
    }
    tb_shutdown();

    close(outfd);
    close(errfd);
    close(ttyfd);
    kill(pid, SIGTERM);
    waitpid(pid, NULL, 0);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0xffffffff, -0xa4(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x94(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x9c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x98(%rbp) # imm = 0xFFFFFFFF
leaq 0xfe7f(%rip), %rax      # 0x3a320
cmpl $-0x1, (%rax)
jne 0x2a4ef
leaq 0x12023(%rip), %rax     # 0x3c4d0
cmpq $0x0, (%rax)
jne 0x2a4ef
movq 0xfb0e(%rip), %rax      # 0x39fc8
movl (%rax), %eax
cmpl -0x8(%rbp), %eax
jl 0x2a4ef
leaq 0x8661(%rip), %rdi      # 0x32b29
movb $0x0, %al
callq 0x9cc0
movq 0xfb1a(%rip), %rax      # 0x39ff0
movq (%rax), %rdi
movl $0x1, %esi
callq 0x9d70
movl $0x1, -0x4(%rbp)
jmp 0x2a9ed
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
callq 0x2aa00
leaq 0x1215e(%rip), %rdi     # 0x3c660
movl $0x200, %esi            # imm = 0x200
leaq 0xabfe(%rip), %rdx      # 0x3510c
movb $0x0, %al
callq 0x61d0
movl $0x1, -0xf8(%rbp)
movl -0xf8(%rbp), %eax
cmpl -0x8(%rbp), %eax
jge 0x2a593
leaq 0x1212f(%rip), %rdi     # 0x3c660
callq 0x6160
movq %rax, %rcx
leaq 0x12120(%rip), %rax     # 0x3c660
addq %rcx, %rax
movq %rax, -0x110(%rbp)
leaq 0x1210f(%rip), %rdi     # 0x3c660
callq 0x6160
movq -0x110(%rbp), %rdi
movl $0x200, %esi            # imm = 0x200
subq %rax, %rsi
movq -0x10(%rbp), %rax
movslq -0xf8(%rbp), %rcx
movq (%rax,%rcx,8), %rcx
leaq 0xab99(%rip), %rdx      # 0x35114
movb $0x0, %al
callq 0x61d0
movl -0xf8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf8(%rbp)
jmp 0x2a51f
leaq 0x841e(%rip), %rdi      # 0x329b8
xorl %esi, %esi
movb $0x0, %al
callq 0x64e0
movl %eax, -0x94(%rbp)
cmpl $0x0, %eax
jge 0x2a5c6
leaq 0xb079(%rip), %rdi      # 0x3562e
callq 0x6520
movl $0x1, -0x4(%rbp)
jmp 0x2a9ed
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq -0xa4(%rbp), %rdx
leaq -0x98(%rbp), %rcx
leaq -0x9c(%rbp), %r8
callq 0x2ab50
cmpl $0x0, %eax
jge 0x2a5f8
movl $0x1, -0x4(%rbp)
jmp 0x2a9ed
movl -0x94(%rbp), %eax
cmpl -0x98(%rbp), %eax
jle 0x2a614
movl -0x94(%rbp), %eax
movl %eax, -0x114(%rbp)
jmp 0x2a620
movl -0x98(%rbp), %eax
movl %eax, -0x114(%rbp)
movl -0x114(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jle 0x2a664
movl -0x94(%rbp), %eax
cmpl -0x98(%rbp), %eax
jle 0x2a64a
movl -0x94(%rbp), %eax
movl %eax, -0x118(%rbp)
jmp 0x2a656
movl -0x98(%rbp), %eax
movl %eax, -0x118(%rbp)
movl -0x118(%rbp), %eax
movl %eax, -0x11c(%rbp)
jmp 0x2a670
movl -0x9c(%rbp), %eax
movl %eax, -0x11c(%rbp)
movl -0x11c(%rbp), %eax
movl %eax, -0xa0(%rbp)
movq $0x0, -0xd8(%rbp)
movq $0x0, -0xd0(%rbp)
callq 0x6710
leaq 0x11ea6(%rip), %rax     # 0x3c544
cmpl $0x0, (%rax)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x2a6af
jmp 0x2a99f
movl $0x1, %edi
leaq -0xc8(%rbp), %rsi
callq 0x6110
cmpq $0x0, -0xd8(%rbp)
je 0x2a6de
movq -0xc8(%rbp), %rax
subq -0xd8(%rbp), %rax
cmpq $0x1, %rax
jl 0x2a6ff
callq 0x2c0a0
movq -0xc8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x2a701
leaq -0x90(%rbp), %rax
movq %rax, -0x108(%rbp)
movl $0x0, -0xfc(%rbp)
movl -0xfc(%rbp), %eax
cmpq $0x10, %rax
jae 0x2a74b
movq -0x108(%rbp), %rax
movl -0xfc(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0xfc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xfc(%rbp)
jmp 0x2a719
jmp 0x2a74d
movl -0x94(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x94(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x90(%rbp,%rax,8), %rcx
movq %rcx, -0x90(%rbp,%rax,8)
movl -0x98(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x98(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x90(%rbp,%rax,8), %rcx
movq %rcx, -0x90(%rbp,%rax,8)
movl -0x9c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x9c(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0x90(%rbp,%rax,8), %rcx
movq %rcx, -0x90(%rbp,%rax,8)
movq $0x1, -0xb8(%rbp)
movq $0x0, -0xb0(%rbp)
movl -0xa0(%rbp), %edi
addl $0x1, %edi
leaq -0x90(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0xb8(%rbp), %r8
movq %rcx, %rdx
callq 0x6390
movl %eax, -0xf4(%rbp)
cmpl $0x0, -0xf4(%rbp)
jge 0x2a885
callq 0x6060
cmpl $0x4, (%rax)
jne 0x2a874
leaq -0xf0(%rbp), %rdi
xorl %esi, %esi
callq 0x81c0
movq $0x0, -0xd8(%rbp)
jmp 0x2a697
leaq 0xa89d(%rip), %rdi      # 0x35118
callq 0x6520
jmp 0x2a99f
movl -0x98(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x90(%rbp,%rax,8), %rax
movq %rax, -0x128(%rbp)
movl -0x98(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x128(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2a8da
movl -0x98(%rbp), %edi
callq 0x2ac70
movl -0x9c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x90(%rbp,%rax,8), %rax
movq %rax, -0x130(%rbp)
movl -0x9c(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x130(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2a92f
movl -0x9c(%rbp), %edi
callq 0x2ada0
movl -0x94(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0x90(%rbp,%rax,8), %rax
movq %rax, -0x138(%rbp)
movl -0x94(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x138(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x2a99a
movb $0x0, -0xf0(%rbp)
leaq -0xf0(%rbp), %rdi
xorl %esi, %esi
callq 0x81c0
leaq -0xf0(%rbp), %rdi
callq 0x2aed0
jmp 0x2a697
callq 0x7110
movl -0x98(%rbp), %edi
callq 0x6260
movl -0x9c(%rbp), %edi
callq 0x6260
movl -0x94(%rbp), %edi
callq 0x6260
movl -0xa4(%rbp), %edi
movl $0xf, %esi
callq 0x6370
movl -0xa4(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x64a0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
nopl (%rax)