// File: /workspace/github_c_makefile_star2_O1/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 $0x128, %rsp            # imm = 0x128
movq %rsi, %rbx
movl %edi, %r15d
xorps %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movups %xmm0, 0x9(%rax)
movaps %xmm0, (%rax)
leaq 0x60(%rsp), %rdi
movl $0x0, 0x3c(%rsp)
movl $0xc8, %edx
xorl %esi, %esi
callq 0x4010a0
leaq 0x10(%rsp), %rax
movw $0x0, 0x8(%rax)
movq $0x0, (%rax)
movl $0xffffffff, 0x30(%rsp) # imm = 0xFFFFFFFF
leaq 0xbc54a(%rip), %rbp     # 0x4be060
movq $0x4f7740, %r12         # imm = 0x4F7740
leaq 0xbc4e0(%rip), %r13     # 0x4be004
movl $0x0, 0xc(%rsp)
movl $0xffffffff, 0x2c(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x28(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x20(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x24(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x38(%rsp) # imm = 0xFFFFFFFF
movl $0xffffffff, 0x34(%rsp) # imm = 0xFFFFFFFF
movl %r15d, %edi
movq %rbx, %rsi
movq %rbp, %rdx
callq 0x45f500
movl %eax, %r14d
cmpl $0x62, %eax
jle 0x401ba4
leal -0x63(%r14), %eax
cmpl $0x16, %eax
ja 0x401db3
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movq (%r12), %rdi
testq %rdi, %rdi
je 0x401cf1
callq 0x40afa0
movl %eax, 0x24(%rsp)
jmp 0x401cf9
cmpl $-0x1, %r14d
je 0x401cf9
cmpl $0x3f, %r14d
je 0x401c94
cmpl $0x49, %r14d
jne 0x401db3
movq (%r12), %rcx
movl $0x9, %esi
leaq 0x10(%rsp), %rdi
leaq 0xd9a1a(%rip), %rdx     # 0x4db5f1
xorl %eax, %eax
callq 0x40cd50
jmp 0x401cf9
movq (%r12), %rdi
callq 0x40afa0
movl %eax, 0x2c(%rsp)
jmp 0x401cf9
movq (%r12), %rdi
callq 0x40afa0
movl %eax, 0x30(%rsp)
jmp 0x401cf9
movq (%r12), %rdi
callq 0x40afa0
movl %eax, 0x34(%rsp)
jmp 0x401cf9
movq (%r12), %rdi
callq 0x40afa0
movl %eax, 0x3c(%rsp)
jmp 0x401cf9
movq (%r12), %rdi
callq 0x40afa0
movl %eax, 0xc(%rsp)
jmp 0x401cf9
movq (%r12), %rsi
movl $0x19, %edx
leaq 0x40(%rsp), %rdi
jmp 0x401c5b
movq (%r12), %rsi
movl $0xc8, %edx
leaq 0x60(%rsp), %rdi
callq 0x401080
jmp 0x401cf9
movq (%r12), %rdi
callq 0x40afa0
movl %eax, 0x38(%rsp)
jmp 0x401cf9
movq (%r12), %rdi
callq 0x40afa0
testl %eax, %eax
movl %eax, 0x28(%rsp)
je 0x401cf9
cmpl $0xb4, %eax
je 0x401cf9
jmp 0x401ef6
movq $0x4f1098, %rax         # imm = 0x4F1098
movl (%rax), %esi
cmpl $0x65, %esi
jg 0x401cb5
cmpl $0x49, %esi
je 0x401dba
cmpl $0x64, %esi
jne 0x401cc7
jmp 0x401d7f
cmpl $0x66, %esi
je 0x401d7f
cmpl $0x69, %esi
je 0x401d7f
movl %esi, %eax
andl $-0x2, %eax
cmpl $0x6c, %eax
je 0x401dc8
cmpl $0x72, %esi
je 0x401ddf
cmpl $0x6e, %esi
je 0x401dd1
cmpl $0x78, %eax
jne 0x401cf9
jmp 0x401ded
movl $0x1, 0x20(%rsp)
cmpl $-0x1, %r14d
jne 0x401b5c
movzbl 0x3c(%rsp), %ebp
movl %ebp, %edi
callq 0x401f08
testb %al, %al
je 0x401d21
leaq 0xbc448(%rip), %rdi     # 0x4be162
movl %ebp, %esi
jmp 0x401df4
movl %eax, %ebx
cmpb $0x0, 0x10(%rsp)
je 0x401d88
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 0x401da3
xorl 0x10(%rsp), %eax
movl $0x323378, %ecx         # imm = 0x323378
xorl 0x13(%rsp), %ecx
orl %eax, %ecx
je 0x401e12
movl $0x34783436, %eax       # imm = 0x34783436
xorl 0x10(%rsp), %eax
movzwl 0x14(%rsp), %ecx
xorl $0x38, %ecx
orl %eax, %ecx
jne 0x401e23
movl $0x30, %edi
movl $0x40, %esi
jmp 0x401e1c
leaq 0xbc326(%rip), %rdi     # 0x4be0ac
jmp 0x401df4
xorl %eax, %eax
callq 0x40267d
testb %al, %al
je 0x401e23
leaq 0xbc57f(%rip), %rdi     # 0x4be31d
jmp 0x401efd
movl $0x40, %edi
jmp 0x401e17
callq 0x401a18
xorl %eax, %eax
jmp 0x401e00
callq 0x401a18
jmp 0x401dfb
leaq 0xbc2b8(%rip), %rdi     # 0x4be079
movl $0x49, %esi
jmp 0x401df4
leaq 0xbc2fe(%rip), %rdi     # 0x4be0cd
jmp 0x401df4
leaq 0xbc30f(%rip), %rdi     # 0x4be0e7
movl $0x6e, %esi
jmp 0x401df4
leaq 0xbc334(%rip), %rdi     # 0x4be11a
movl $0x72, %esi
jmp 0x401df4
leaq 0xbc349(%rip), %rdi     # 0x4be13d
xorl %eax, %eax
callq 0x40cc80
movl $0x1, %eax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x20, %edi
movl $0x80, %esi
callq 0x4022a6
movl %eax, %ebx
cmpl $0x0, 0x20(%rsp)
js 0x401e33
xorl %eax, %eax
callq 0x402642
jmp 0x401e43
movl 0x24(%rsp), %eax
testl %eax, %eax
js 0x401e45
movzbl %al, %edi
callq 0x4025f0
addb %al, %bl
movl 0x28(%rsp), %eax
testl %eax, %eax
js 0x401e57
movzbl %al, %edi
callq 0x402269
addb %al, %bl
movl 0x2c(%rsp), %eax
testl %eax, %eax
js 0x401e69
movzbl %al, %edi
callq 0x401fa8
addb %al, %bl
movl 0x30(%rsp), %eax
testl %eax, %eax
js 0x401e7b
movzbl %al, %edi
callq 0x401f56
addb %al, %bl
movl 0x34(%rsp), %edi
movl %edi, %eax
movl 0x38(%rsp), %esi
orl %esi, %eax
js 0x401e99
movl $0xff, %eax
andl %eax, %edi
andl %eax, %esi
callq 0x4021fd
jmp 0x401eb5
testl %edi, %edi
js 0x401ea8
movzbl %dil, %edi
callq 0x402173
jmp 0x401eb5
testl %esi, %esi
js 0x401eb7
movzbl %sil, %edi
callq 0x4021bb
addb %al, %bl
cmpb $0x0, 0x60(%rsp)
je 0x401ecf
movzbl 0xc(%rsp), %edi
leaq 0x60(%rsp), %rsi
callq 0x40250a
jmp 0x401ee5
cmpb $0x0, 0x40(%rsp)
je 0x401ee7
movzbl 0xc(%rsp), %edi
leaq 0x40(%rsp), %rsi
callq 0x4023c3
addb %al, %bl
xorl %eax, %eax
callq 0x401f4f
movzbl %bl, %eax
jmp 0x401e00
leaq 0xbc45d(%rip), %rdi     # 0x4be35a
callq 0x41b870
jmp 0x401dfb
nop