// File: /workspace/github_c_makefile_star2_O3/armlabs[P]ssd1306_linux/main.c
// Function: main
int main(int argc, char **argv)
{
    uint8_t i2c_node_address = 0;
    int x = -1;
    int y = -1;
    char line[25] = {0};
    char msg[200] = {0};
    char oled_type[10] = {0};
    int clear_line = -1;
    int clear_all = -1;
    int orientation = -1;
    int inverted = -1;
    int display = -1;
    int font = 0;
    
    int cmd_opt = 0;
    
    while(cmd_opt != -1) 
    {
        cmd_opt = getopt(argc, argv, "I:c::d:f:hi:l:m:n:r:x:y:");

        /* Lets parse */
        switch (cmd_opt) {
            case 'I':
                snprintf(oled_type, sizeof (oled_type) - 1, "%s", optarg);
                break;
            case 'c':
                if (optarg)
                {
                    clear_line = atoi(optarg);
                }
                else
                {
                    clear_all = 1;
                }
                break;
            case 'd':
                display = atoi(optarg);
                break;
            case 'f':
                font = atoi(optarg);
                break;
            case 'h':
                print_help();
                return 0;
            case 'i':
                inverted = atoi(optarg);
                break;
            case 'l':
                strncpy(line, optarg, sizeof(line));
                break;
            case 'm':
                strncpy(msg, optarg, sizeof(msg));
                break;    
            case 'n':
                i2c_node_address = (uint8_t)atoi(optarg);
                break;
            case 'r':
                orientation = atoi(optarg);
                if (orientation != 0 && orientation != 180)
                {
                    printf("orientation value must be 0 or 180\n");
                    return 1;
                }
                break;
            case 'x':
                x = atoi(optarg);
                break;
            case 'y':
                y = atoi(optarg);
                break;
            case -1:
                // just ignore
                break;
            /* Error handle: Mainly missing arg or illegal option */
            case '?':
                if (optopt == 'I')
                {
                    printf("prams -%c missing oled type (128x64/128x32/64x48)\n", optopt);
                    return 1;
                }
                else if (optopt == 'd' || optopt == 'f' || optopt == 'i')
                {
                    printf("prams -%c missing 0 or 1 fields\n", optopt);
                    return 1;
                }
                else if (optopt == 'l' || optopt == 'm')
                {
                    printf("prams -%c missing string\n", optopt);
                    return 1;
                }
                else if (optopt == 'n')
                {
                    printf("prams -%c missing 0,1,2... I2C device node number\n", optopt);
                    return 1;
                }
                else if (optopt == 'r')
                {
                    printf("prams -%c missing 0 or 180 fields\n", optopt);
                    return 1;
                }
                else if (optopt == 'x' || optopt == 'y')
                {
                    printf("prams -%c missing coordinate values\n", optopt);
                    return 1;
                }
                break;
            default:
                print_help();
                return 1;
        }
    }
    
    uint8_t rc = 0;
    
    // open the I2C device node
    rc = ssd1306_init(i2c_node_address);
    
    if (rc != 0)
    {
        printf("no oled attached to /dev/i2c-%d\n", i2c_node_address);
        return 1;
    }
    
    // init oled module
    if (oled_type[0] != 0)
    {
        if (strcmp(oled_type, "128x64") == 0)
            rc += ssd1306_oled_default_config(64, 128);
        else if (strcmp(oled_type, "128x32") == 0)
            rc += ssd1306_oled_default_config(32, 128);
        else if (strcmp(oled_type, "64x48") == 0)
            rc += ssd1306_oled_default_config(48, 64);
    }
    else if (ssd1306_oled_load_resolution() != 0)
    {
        printf("please do init oled module with correction resolution first!\n");
        return 1;
    }
    
    // clear display
    if (clear_all > -1)
    {
        rc += ssd1306_oled_clear_screen();
    }
    else if (clear_line > -1)
    {
        rc += ssd1306_oled_clear_line(clear_line);
    }
    
    // set rotate orientation
    if (orientation > -1)
    {
        rc += ssd1306_oled_set_rotate(orientation);
    }
    
    // set oled inverted
    if (inverted > -1)
    {
        rc += ssd1306_oled_display_flip(inverted);
    }
    
    // set display on off
    if (display > -1)
    {
        rc += ssd1306_oled_onoff(display);
    }
    
    // set cursor XY
    if (x > -1 && y > -1)
    {
        rc += ssd1306_oled_set_XY(x, y);
    }
    else if (x > -1)
    {
        rc += ssd1306_oled_set_X(x);
    }
    else if (y > -1)
    {
        rc += ssd1306_oled_set_Y(y);
    }
    
    // print text
    if (msg[0] != 0)
    {
        rc += ssd1306_oled_write_string(font, msg);
    }
    else if (line[0] != 0)
    {
        rc += ssd1306_oled_write_line(font, line);
    }    
    
    // close the I2C device node
    ssd1306_end();
    
    return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rsi, %rbx
movl %edi, %ebp
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rax
movups %xmm0, 0x9(%rax)
movaps %xmm0, (%rax)
leaq 0x50(%rsp), %rdi
movl $0x0, 0x2c(%rsp)
movl $0xc8, %edx
xorl %esi, %esi
callq 0x4010a0
leaq 0x10(%rsp), %rax
movw $0x0, 0x8(%rax)
movq $0x0, (%rax)
movl $0xffffffff, 0x28(%rsp) # imm = 0xFFFFFFFF
leaq 0xbc54b(%rip), %r12     # 0x4be060
movq $0x4f7740, %r15         # imm = 0x4F7740
leaq 0xbc4e1(%rip), %r13     # 0x4be004
movl $0x0, (%rsp)
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movl $0xffffffff, 0x20(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movl %edx, %r14d
movl %ebp, %edi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x45f4c0
cmpl $0x62, %eax
jle 0x401b9d
addl $-0x63, %eax
cmpl $0x16, %eax
ja 0x401d1b
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movq (%r15), %rdi
movl $0x1, %edx
testq %rdi, %rdi
je 0x401b57
callq 0x40af40
movl %eax, 0x20(%rsp)
movl %r14d, %edx
jmp 0x401b57
cmpl $0x3f, %eax
je 0x401c9c
cmpl $0x49, %eax
jne 0x401cf2
movq (%r15), %rcx
movl $0x9, %esi
leaq 0x10(%rsp), %rdi
leaq 0xd9a2e(%rip), %rdx     # 0x4db5f1
xorl %eax, %eax
callq 0x40ccf0
movl %r14d, %edx
jmp 0x401b57
movq (%r15), %rdi
callq 0x40af40
movl %eax, 0x4(%rsp)
movl %r14d, %edx
jmp 0x401b57
movq (%r15), %rdi
callq 0x40af40
movl %eax, 0x28(%rsp)
movl %r14d, %edx
jmp 0x401b57
movq (%r15), %rsi
movl $0xc8, %edx
leaq 0x50(%rsp), %rdi
jmp 0x401c67
movq (%r15), %rdi
callq 0x40af40
movl %r14d, %edx
movl %eax, 0xc(%rsp)
testl %eax, %eax
je 0x401b57
movl %r14d, %edx
cmpl $0xb4, 0xc(%rsp)
je 0x401b57
jmp 0x401d71
movq (%r15), %rdi
callq 0x40af40
movl %eax, 0x2c(%rsp)
movl %r14d, %edx
jmp 0x401b57
movq (%r15), %rdi
callq 0x40af40
movl %eax, (%rsp)
movl %r14d, %edx
jmp 0x401b57
movq (%r15), %rsi
movl $0x19, %edx
leaq 0x30(%rsp), %rdi
callq 0x401080
movl %r14d, %edx
jmp 0x401b57
movq (%r15), %rdi
callq 0x40af40
movl %eax, 0x24(%rsp)
movl %r14d, %edx
jmp 0x401b57
movq (%r15), %rdi
callq 0x40af40
movl %eax, 0x8(%rsp)
movl %r14d, %edx
jmp 0x401b57
movq $0x4f1098, %rax         # imm = 0x4F1098
movl (%rax), %esi
cmpl $0x65, %esi
jg 0x401cb6
cmpl $0x49, %esi
je 0x401d22
cmpl $0x64, %esi
jne 0x401cc0
jmp 0x401ce9
cmpl $0x66, %esi
je 0x401ce9
cmpl $0x69, %esi
je 0x401ce9
movl %esi, %ecx
andl $-0x2, %ecx
cmpl $0x6c, %ecx
je 0x401d30
cmpl $0x72, %esi
je 0x401d39
cmpl $0x6e, %esi
je 0x401d47
movl %r14d, %edx
cmpl $0x78, %ecx
jne 0x401b57
leaq 0xbc456(%rip), %rdi     # 0x4be13d
jmp 0x401d53
leaq 0xbc3bc(%rip), %rdi     # 0x4be0ac
jmp 0x401d53
cmpl $-0x1, %eax
jne 0x401d1b
movzbl 0x2c(%rsp), %ebx
movl %ebx, %edi
callq 0x401efc
testb %al, %al
je 0x401d7f
leaq 0xbc454(%rip), %rdi     # 0x4be162
movl %ebx, %esi
jmp 0x401d53
callq 0x401a18
xorl %eax, %eax
jmp 0x401d5f
callq 0x401a18
jmp 0x401d5a
leaq 0xbc350(%rip), %rdi     # 0x4be079
movl $0x49, %esi
jmp 0x401d53
leaq 0xbc396(%rip), %rdi     # 0x4be0cd
jmp 0x401d53
leaq 0xbc3da(%rip), %rdi     # 0x4be11a
movl $0x72, %esi
jmp 0x401d53
leaq 0xbc399(%rip), %rdi     # 0x4be0e7
movl $0x6e, %esi
xorl %eax, %eax
callq 0x40cc20
movl $0x1, %eax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbc5e2(%rip), %rdi     # 0x4be35a
callq 0x41b810
jmp 0x401d5a
cmpb $0x0, 0x10(%rsp)
je 0x401dcc
movl $0x78383231, %eax       # imm = 0x78383231
movl 0x10(%rsp), %ecx
xorl %eax, %ecx
movl $0x343678, %edx         # imm = 0x343678
xorl 0x13(%rsp), %edx
orl %ecx, %edx
je 0x401de2
xorl 0x10(%rsp), %eax
movl $0x323378, %ecx         # imm = 0x323378
xorl 0x13(%rsp), %ecx
orl %eax, %ecx
je 0x401de9
movl $0x34783436, %eax       # imm = 0x34783436
xorl 0x10(%rsp), %eax
movzwl 0x14(%rsp), %ecx
xorl $0x38, %ecx
orl %eax, %ecx
je 0x401eeb
xorl %ebx, %ebx
jmp 0x401dfa
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x402615
testb %al, %al
je 0x401dfa
leaq 0xbc53d(%rip), %rdi     # 0x4be31d
jmp 0x401d78
movl $0x40, %edi
jmp 0x401dee
movl $0x20, %edi
movl $0x80, %esi
callq 0x40229a
movl %eax, %ebx
testl %r14d, %r14d
js 0x401e15
xorl %eax, %eax
callq 0x4025da
addb %bl, %al
movl %eax, %ebx
movl 0x8(%rsp), %ebp
movl 0x4(%rsp), %r14d
jmp 0x401e32
movl 0x20(%rsp), %eax
testl %eax, %eax
movl 0x8(%rsp), %ebp
movl 0x4(%rsp), %r14d
js 0x401e32
movzbl %al, %edi
callq 0x402588
addb %bl, %al
movl %eax, %ebx
movl 0xc(%rsp), %eax
testl %eax, %eax
js 0x401e46
movzbl %al, %edi
callq 0x40225d
addb %bl, %al
movl %eax, %ebx
movl 0x24(%rsp), %eax
testl %eax, %eax
js 0x401e5a
movzbl %al, %edi
callq 0x401f9c
addb %bl, %al
movl %eax, %ebx
movl 0x28(%rsp), %eax
testl %eax, %eax
js 0x401e6e
movzbl %al, %edi
callq 0x401f4a
addb %bl, %al
movl %eax, %ebx
movl %r14d, %eax
orl %ebp, %eax
js 0x401e8b
movl $0xff, %eax
andl %eax, %r14d
andl %eax, %ebp
movl %r14d, %edi
movl %ebp, %esi
callq 0x4021f1
jmp 0x401ea8
testl %r14d, %r14d
js 0x401e9b
movzbl %r14b, %edi
callq 0x402167
jmp 0x401ea8
testl %ebp, %ebp
js 0x401eac
movzbl %bpl, %edi
callq 0x4021af
addb %bl, %al
movl %eax, %ebx
cmpb $0x0, 0x50(%rsp)
je 0x401ec3
movzbl (%rsp), %edi
leaq 0x50(%rsp), %rsi
callq 0x4024b0
jmp 0x401ed8
cmpb $0x0, 0x30(%rsp)
je 0x401edc
movzbl (%rsp), %edi
leaq 0x30(%rsp), %rsi
callq 0x4023b7
addb %bl, %al
movl %eax, %ebx
xorl %eax, %eax
callq 0x401f43
movzbl %bl, %eax
jmp 0x401d5f
movl $0x30, %edi
movl $0x40, %esi
jmp 0x401df3
nop