// File: /workspace/github_c_makefile_star2_O2/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
callq 0x25b4
movq %rax, %r14
xorl %r15d, %r15d
movq %rax, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x25fe
movq %r14, %rdi
callq 0x3349
movq 0x4904(%rip), %r12      # 0x7fe0
leaq 0x985(%rip), %r13       # 0x4068
leaq 0x942(%rip), %rbp       # 0x402c
movq (%r12), %rdi
callq 0x2250
leal -0x31(%rax), %ecx
cmpl $0xe, %ecx
jbe 0x3752
leal -0x6a(%rax), %ecx
cmpl $0xb, %ecx
jbe 0x378a
cmpl $0xa, %eax
je 0x37a1
cmpl $0x4a, %eax
je 0x3943
cmpl $0x4b, %eax
je 0x3963
cmpl $0x4c, %eax
je 0x3939
cmpl $0x63, %eax
je 0x394d
cmpl $0x102, %eax            # imm = 0x102
je 0x3794
cmpl $0x103, %eax            # imm = 0x103
je 0x37b6
cmpl $0x157, %eax            # imm = 0x157
je 0x37a1
testl %eax, %eax
jne 0x36ea
jmp 0x3978
movslq (%rbp,%rcx,4), %rax
addq %rbp, %rax
jmpq *%rax
movq (%r14), %rdi
cmpl $0x0, 0x10(%rdi)
je 0x36ea
callq 0x20f0
movq %r14, %rdi
callq 0x25bf
callq 0x25b4
movq %rax, %r14
xorl %r15d, %r15d
movq %rax, %rdi
movq %rbx, %rsi
xorl %edx, %edx
jmp 0x391b
movslq (%r13,%rcx,4), %rax
addq %r13, %rax
jmpq *%rax
movq %r14, %rdi
callq 0x3508
jmp 0x396b
movb 0x2c(%r14), %al
testb $0x2, %al
jne 0x37c3
movq %r14, %rdi
callq 0x3686
jmp 0x396b
movq %r14, %rdi
callq 0x353b
jmp 0x396b
andb $-0x3, %al
movb %al, 0x2c(%r14)
jmp 0x396b
movq (%r14), %rdi
cmpl $0x4, 0x10(%rdi)
je 0x36ea
callq 0x20f0
movq %r14, %rdi
callq 0x25bf
callq 0x25b4
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
pushq $0x4
jmp 0x387f
movq (%r14), %rdi
cmpl $0x2, 0x10(%rdi)
je 0x36ea
callq 0x20f0
movq %r14, %rdi
callq 0x25bf
callq 0x25b4
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
pushq $0x2
jmp 0x387f
movq (%r14), %rdi
cmpl $0x3, 0x10(%rdi)
je 0x36ea
callq 0x20f0
movq %r14, %rdi
callq 0x25bf
callq 0x25b4
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
pushq $0x3
jmp 0x387f
movq (%r14), %rdi
cmpl $0x1, 0x10(%rdi)
je 0x36ea
callq 0x20f0
movq %r14, %rdi
callq 0x25bf
callq 0x25b4
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
pushq $0x1
popq %r15
jmp 0x3918
movb 0x2c(%r14), %al
movl %eax, %ecx
xorb $0x2, %cl
movb %cl, 0x2c(%r14)
testb $0x2, %al
jne 0x396b
movq %r14, %rdi
callq 0x3643
jmp 0x396b
testb $0x1, 0x2c(%r14)
je 0x3978
movq %r14, %rdi
callq 0x366c
jmp 0x396b
callq 0x2050
testl %eax, %eax
jne 0x38fa
leaq 0x1c74(%rip), %rdi      # 0x5544
movq %rdi, (%rsp)
movq (%r14), %rax
movq 0x18(%rax), %rax
movslq 0x10(%r14), %rcx
movq (%rax,%rcx,8), %rax
movq 0x20(%rax), %rax
movq %rax, 0x8(%rsp)
andq $0x0, 0x10(%rsp)
movq %rsp, %rsi
callq 0x21a0
movq (%r14), %rdi
callq 0x20f0
movq %r14, %rdi
callq 0x25bf
callq 0x25b4
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x25fe
jmp 0x396b
movq (%r14), %rdi
callq 0x2100
movq (%r14), %rdi
callq 0x2140
andq $0x0, 0xc(%r14)
jmp 0x396b
movq (%r14), %rdi
callq 0x2140
jmp 0x396b
movq %r14, %rdi
callq 0x3557
jmp 0x396b
movq (%r14), %rax
movq 0x18(%rax), %rax
movslq 0x10(%r14), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x2e6f
jmp 0x396b
movq %r14, %rdi
callq 0x35ca
movq %r14, %rdi
callq 0x3349
jmp 0x36ea
movq (%r14), %rdi
callq 0x20f0
movq %r14, %rdi
callq 0x25bf
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq