// File: /workspace/github_c_makefile_star2_O1/Cotix[P]cReddit/src/main.c
// Function: showSubreddit
void showSubreddit(const char *subreddit)
{
    LinkScreen *screen;
    RedditListType listType = REDDIT_HOT; // Default RedditListType for screen is 'hot'

    DEBUG_PRINT(L"Loading Subreddit %s\n", subreddit);
    screen = linkScreenNew();

    linkScreenSetup(screen, subreddit, listType);

    drawScreen(screen); //And print the screen!

    int c;
    while((c = wgetch(stdscr))) {
        switch(c) {
            case 'k': case KEY_UP:
                linkScreenUp(screen);
                drawScreen(screen);
                break;

            case 'j': case KEY_DOWN:
                linkScreenDown(screen);
                drawScreen(screen);
                break;
            case 'K':
                linkScreenTextScrollUp(screen);
                drawScreen(screen);
                break;
            case 'J':
                linkScreenTextScrollDown(screen);
                drawScreen(screen);
                break;
            case 'q':
                if (screen->linkOpen) {
                    linkScreenCloseLink(screen);
                    drawScreen(screen);
                } else {
                    goto cleanup;
                }
                break;
            case 'u':
                redditLinkListFreeLinks(screen->list);
                redditGetListing(screen->list);
                screen->offset = 0;
                screen->selected = 0;
                drawScreen(screen);
                break;
            case 'l': case '\n': case KEY_ENTER:
                if (screen->helpOpen)
                    linkScreenCloseHelp(screen);
                else
                    linkScreenToggleLink(screen);
                drawScreen(screen);
                break;
            case 'o':
                if (fork() == 0) {
                    char* const argv[]= {"xdg-open", 
                                        screen->list->links[screen->selected]->url,
                                        NULL};
                    execvp("xdg-open", argv);
                }

                redditLinkListFree(screen->list);
                linkScreenFree(screen);

                screen = linkScreenNew();
                linkScreenSetup(screen, subreddit, listType);

                drawScreen(screen);
                break;
            case 'L':
                redditGetListing(screen->list);
                drawScreen(screen);
                break;
            case 'c':
                showThread(screen->list->links[screen->selected]);
                drawScreen(screen);
                break;
            case '?':
                linkScreenToggleHelp(screen);
                if (screen->helpOpen)
                    linkScreenOpenLink(screen);
                drawScreen(screen);
                break;
            case '1':
                if (screen->list->type != REDDIT_HOT)
                {
                    listType = REDDIT_HOT;
                    redditLinkListFree(screen->list);
                    linkScreenFree(screen);

                    screen = linkScreenNew();
                    linkScreenSetup(screen, subreddit, listType);

                    drawScreen(screen);
                }
                break;
            case '2':
                if (screen->list->type != REDDIT_NEW)
                {
                    listType = REDDIT_NEW;
                    redditLinkListFree(screen->list);
                    linkScreenFree(screen);

                    screen = linkScreenNew();
                    linkScreenSetup(screen, subreddit, listType);

                    drawScreen(screen);
                }
                break;
            case '3':
                if (screen->list->type != REDDIT_RISING)
                {
                    listType = REDDIT_RISING;
                    redditLinkListFree(screen->list);
                    linkScreenFree(screen);

                    screen = linkScreenNew();
                    linkScreenSetup(screen, subreddit, listType);

                    drawScreen(screen);
                }
                break;
            case '4':
                if (screen->list->type != REDDIT_CONTR)
                {
                    listType = REDDIT_CONTR;
                    redditLinkListFree(screen->list);
                    linkScreenFree(screen);

                    screen = linkScreenNew();
                    linkScreenSetup(screen, subreddit, listType);

                    drawScreen(screen);
                }
                break;
            case '5':
                if (screen->list->type != REDDIT_TOP)
                {
                    listType = REDDIT_TOP;
                    redditLinkListFree(screen->list);
                    linkScreenFree(screen);

                    screen = linkScreenNew();
                    linkScreenSetup(screen, subreddit, listType);

                    drawScreen(screen);
                }
                break;
        }
    }

cleanup:;
    redditLinkListFree(screen->list);
    linkScreenFree(screen);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl $0x40, %edi
callq 0x2250
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
xorl %ebp, %ebp
movq %rax, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2616
movq %r12, %rdi
callq 0x371e
movq 0x53f1(%rip), %r13      # 0x8fe0
leaq 0x1482(%rip), %r15      # 0x5078
leaq 0x14b7(%rip), %r14      # 0x50b4
movq (%r13), %rdi
callq 0x2240
cmpl $0x62, %eax
jg 0x3c6e
cmpl $0x49, %eax
jg 0x3d4f
leal -0x31(%rax), %ecx
cmpl $0xe, %ecx
ja 0x3d85
movslq (%r15,%rcx,4), %rax
addq %r15, %rax
jmpq *%rax
movq (%r12), %rdi
cmpl $0x0, 0x10(%rdi)
je 0x3bfd
callq 0x20f0
movq %r12, %rdi
callq 0x25cf
movl $0x40, %edi
callq 0x2250
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
xorl %ebp, %ebp
movq %rax, %rdi
movq %rbx, %rsi
xorl %edx, %edx
jmp 0x3f54
cmpl $0x101, %eax            # imm = 0x101
jg 0x3d0a
leal -0x6a(%rax), %ecx
cmpl $0xb, %ecx
ja 0x3db0
movslq (%r14,%rcx,4), %rax
addq %r14, %rax
jmpq *%rax
callq 0x2050
testl %eax, %eax
jne 0x3ccd
leaq 0x28f2(%rip), %rdi      # 0x6590
movq %rdi, (%rsp)
movq (%r12), %rax
movq 0x18(%rax), %rax
movslq 0x10(%r12), %rcx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rax
movq %rax, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq %rsp, %rsi
callq 0x2190
movq (%r12), %rdi
callq 0x20f0
movq %r12, %rdi
callq 0x25cf
movl $0x40, %edi
callq 0x2250
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movq %rax, %rdi
movq %rbx, %rsi
movl %ebp, %edx
jmp 0x3f54
cmpl $0x102, %eax            # imm = 0x102
je 0x3d78
cmpl $0x157, %eax            # imm = 0x157
je 0x3d8e
cmpl $0x103, %eax            # imm = 0x103
jne 0x3bfd
movl 0xc(%r12), %eax
movl 0x10(%r12), %ecx
xorl %edx, %edx
testl %ecx, %ecx
setg %dl
subl %edx, %ecx
movl %ecx, 0x10(%r12)
cmpl %eax, %ecx
jge 0x3f8a
decl %eax
movl %eax, 0xc(%r12)
jmp 0x3f8a
cmpl $0x4a, %eax
je 0x3f82
cmpl $0x4b, %eax
je 0x3f78
cmpl $0x4c, %eax
jne 0x3bfd
movq (%r12), %rdi
callq 0x2140
jmp 0x3f8a
movq %r12, %rdi
callq 0x39fa
jmp 0x3f8a
cmpl $0xa, %eax
jne 0x3f97
movb 0x2c(%r12), %al
testb $0x2, %al
jne 0x3da4
movq %r12, %rdi
callq 0x3b74
jmp 0x3f8a
andb $-0x3, %al
movb %al, 0x2c(%r12)
jmp 0x3f8a
cmpl $0x63, %eax
jne 0x3bfd
movq (%r12), %rax
movq 0x18(%rax), %rax
movslq 0x10(%r12), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x30e0
jmp 0x3f8a
movb 0x2c(%r12), %al
testb $0x1, %al
je 0x3f9f
andb $-0x2, %al
movb %al, 0x2c(%r12)
movl 0x8(%r12), %eax
movl 0x28(%r12), %ecx
addl %ecx, %eax
decl %eax
movl %eax, 0x8(%r12)
jmp 0x3f8a
movq (%r12), %rdi
cmpl $0x4, 0x10(%rdi)
je 0x3bfd
callq 0x20f0
movq %r12, %rdi
callq 0x25cf
movl $0x40, %edi
callq 0x2250
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movl $0x4, %ebp
movq %rax, %rdi
movq %rbx, %rsi
movl $0x4, %edx
jmp 0x3f54
movq (%r12), %rdi
cmpl $0x2, 0x10(%rdi)
je 0x3bfd
callq 0x20f0
movq %r12, %rdi
callq 0x25cf
movl $0x40, %edi
callq 0x2250
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movl $0x2, %ebp
movq %rax, %rdi
movq %rbx, %rsi
movl $0x2, %edx
jmp 0x3f54
movq (%r12), %rdi
cmpl $0x3, 0x10(%rdi)
je 0x3bfd
callq 0x20f0
movq %r12, %rdi
callq 0x25cf
movl $0x40, %edi
callq 0x2250
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movl $0x3, %ebp
movq %rax, %rdi
movq %rbx, %rsi
movl $0x3, %edx
jmp 0x3f54
movq (%r12), %rdi
callq 0x2100
movq (%r12), %rdi
callq 0x2140
movq $0x0, 0xc(%r12)
jmp 0x3f8a
movq (%r12), %rdi
cmpl $0x1, 0x10(%rdi)
je 0x3bfd
callq 0x20f0
movq %r12, %rdi
callq 0x25cf
movl $0x40, %edi
callq 0x2250
movq %rax, %r12
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movups %xmm0, 0x10(%rax)
movups %xmm0, 0x20(%rax)
movups %xmm0, 0x30(%rax)
movl $0x1, %ebp
movq %rax, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x2616
jmp 0x3f8a
movb 0x2c(%r12), %al
movl %eax, %ecx
xorb $0x2, %cl
movb %cl, 0x2c(%r12)
testb $0x2, %al
jne 0x3f8a
movq %r12, %rdi
callq 0x3b31
jmp 0x3f8a
movq %r12, %rdi
callq 0x3abe
jmp 0x3f8a
movq %r12, %rdi
callq 0x3a49
movq %r12, %rdi
callq 0x371e
jmp 0x3bfd
testl %eax, %eax
jne 0x3bfd
movq (%r12), %rdi
callq 0x20f0
movq %r12, %rdi
callq 0x25cf
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq