// File: /workspace/github_c_makefile_star2_O2/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, %r15d
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %rax
movups %xmm0, 0x9(%rax)
movaps %xmm0, (%rax)
leaq 0x50(%rsp), %rdi
movl $0x0, 0x1c(%rsp)
movl $0xc8, %edx
xorl %esi, %esi
callq 0x4010a8
leaq 0x20(%rsp), %rax
andw $0x0, 0x8(%rax)
andq $0x0, (%rax)
pushq $-0x1
popq %rcx
leaq 0xbc553(%rip), %r14     # 0x4be060
leaq 0xbc4f0(%rip), %r13     # 0x4be004
movl %ecx, 0x14(%rsp)
movl %ecx, 0x8(%rsp)
movl %ecx, %edx
movl %ecx, 0x4(%rsp)
movl %ecx, 0xc(%rsp)
movl %ecx, 0x18(%rsp)
movl %ecx, 0x10(%rsp)
movl $0x0, (%rsp)
xorl %r12d, %r12d
movl %edx, %ebp
cmpl $-0x1, %r12d
je 0x401d30
movl %r15d, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x45f480
movl %eax, %r12d
leal -0x63(%r12), %eax
cmpl $0x16, %eax
ja 0x401b88
movslq (%r13,%rax,4), %rax
addq %r13, %rax
jmpq *%rax
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
pushq $0x1
popq %rdx
testq %rdi, %rdi
je 0x401b38
callq 0x40af20
movl %eax, 0x8(%rsp)
movl %ebp, %edx
jmp 0x401b38
movl %ebp, %edx
cmpl $-0x1, %r12d
je 0x401b38
cmpl $0x3f, %r12d
je 0x401ccc
cmpl $0x49, %r12d
jne 0x401dce
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rcx
leaq 0x20(%rsp), %rdi
pushq $0x9
popq %rsi
leaq 0xd9a34(%rip), %rdx     # 0x4db5f1
xorl %eax, %eax
callq 0x40ccd0
movl %ebp, %edx
jmp 0x401b38
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rsi
movl $0xc8, %edx
leaq 0x50(%rsp), %rdi
jmp 0x401c0d
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40af20
movl %eax, 0x14(%rsp)
movl %ebp, %edx
jmp 0x401b38
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rsi
leaq 0x30(%rsp), %rdi
pushq $0x19
popq %rdx
callq 0x401088
movl %ebp, %edx
jmp 0x401b38
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40af20
movl %ebp, %edx
movl %eax, 0x4(%rsp)
testl %eax, %eax
je 0x401b38
movl %ebp, %edx
cmpl $0xb4, 0x4(%rsp)
je 0x401b38
jmp 0x401f10
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40af20
movl %eax, (%rsp)
movl %ebp, %edx
jmp 0x401b38
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40af20
movl %eax, 0xc(%rsp)
movl %ebp, %edx
jmp 0x401b38
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40af20
movl %eax, 0x10(%rsp)
movl %ebp, %edx
jmp 0x401b38
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40af20
movl %eax, 0x18(%rsp)
movl %ebp, %edx
jmp 0x401b38
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40af20
movl %eax, 0x1c(%rsp)
movl %ebp, %edx
jmp 0x401b38
movq $0x4f1098, %rax         # imm = 0x4F1098
movl (%rax), %esi
cmpl $0x69, %esi
je 0x401dac
cmpl $0x64, %esi
je 0x401dac
cmpl $0x66, %esi
je 0x401dac
cmpl $0x49, %esi
je 0x401de2
movl %esi, %ecx
andl $-0x2, %ecx
cmpl $0x6c, %ecx
je 0x401ded
cmpl $0x72, %esi
je 0x401df6
cmpl $0x6e, %esi
je 0x401e01
movl %ebp, %edx
cmpl $0x78, %ecx
jne 0x401b38
leaq 0xbc412(%rip), %rdi     # 0x4be13d
jmp 0x401e0b
movzbl 0x1c(%rsp), %ebx
movl %ebx, %edi
callq 0x401f2c
testb %al, %al
je 0x401d4e
leaq 0xbc41b(%rip), %rdi     # 0x4be162
movl %ebx, %esi
jmp 0x401e0b
cmpb $0x0, 0x20(%rsp)
je 0x401db5
leaq 0xbc427(%rip), %rsi     # 0x4be183
leaq 0x20(%rsp), %rdi
pushq $0x7
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x401dd5
leaq 0xbc416(%rip), %rsi     # 0x4be18a
leaq 0x20(%rsp), %rdi
pushq $0x7
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x401e27
leaq 0xbc401(%rip), %rsi     # 0x4be191
leaq 0x20(%rsp), %rdi
pushq $0x6
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x401f21
xorl %ebx, %ebx
jmp 0x401e36
leaq 0xbc2f9(%rip), %rdi     # 0x4be0ac
jmp 0x401e0b
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x4025f9
testb %al, %al
je 0x401e36
leaq 0xbc554(%rip), %rdi     # 0x4be31d
jmp 0x401f17
callq 0x401a18
jmp 0x401e12
pushq $0x40
jmp 0x401e29
callq 0x401a18
xorl %eax, %eax
jmp 0x401e15
leaq 0xbc290(%rip), %rdi     # 0x4be079
pushq $0x49
jmp 0x401e0a
leaq 0xbc2d9(%rip), %rdi     # 0x4be0cd
jmp 0x401e0b
leaq 0xbc31d(%rip), %rdi     # 0x4be11a
pushq $0x72
jmp 0x401e0a
leaq 0xbc2df(%rip), %rdi     # 0x4be0e7
pushq $0x6e
popq %rsi
xorl %eax, %eax
callq 0x40cc00
pushq $0x1
popq %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x20
popq %rdi
movl $0x80, %esi
callq 0x4022a9
movl %eax, %ebx
testl %ebp, %ebp
js 0x401e43
xorl %eax, %eax
callq 0x4025cb
jmp 0x401e53
movl 0x8(%rsp), %eax
testl %eax, %eax
js 0x401e57
movzbl %al, %edi
callq 0x402579
addb %bl, %al
movl %eax, %ebx
movl 0x4(%rsp), %eax
testl %eax, %eax
js 0x401e6b
movzbl %al, %edi
callq 0x40226e
addb %bl, %al
movl %eax, %ebx
movl 0xc(%rsp), %eax
testl %eax, %eax
js 0x401e7f
movzbl %al, %edi
callq 0x401fcb
addb %bl, %al
movl %eax, %ebx
movl 0x10(%rsp), %eax
testl %eax, %eax
js 0x401e93
movzbl %al, %edi
callq 0x401f7d
addb %bl, %al
movl %eax, %ebx
movl 0x18(%rsp), %edi
movl %edi, %eax
movl 0x14(%rsp), %esi
orl %esi, %eax
js 0x401eb1
movl $0xff, %eax
andl %eax, %edi
andl %eax, %esi
callq 0x402204
jmp 0x401ecd
testl %edi, %edi
js 0x401ec0
movzbl %dil, %edi
callq 0x40217e
jmp 0x401ecd
testl %esi, %esi
js 0x401ed1
movzbl %sil, %edi
callq 0x4021c4
addb %bl, %al
movl %eax, %ebx
cmpb $0x0, 0x50(%rsp)
je 0x401ee8
movzbl (%rsp), %edi
leaq 0x50(%rsp), %rsi
callq 0x4024a4
jmp 0x401efd
cmpb $0x0, 0x30(%rsp)
je 0x401f01
movzbl (%rsp), %edi
leaq 0x30(%rsp), %rsi
callq 0x4023c4
addb %bl, %al
movl %eax, %ebx
xorl %eax, %eax
callq 0x401f76
movzbl %bl, %eax
jmp 0x401e15
leaq 0xbc443(%rip), %rdi     # 0x4be35a
callq 0x41b7f0
jmp 0x401e12
pushq $0x30
popq %rdi
pushq $0x40
popq %rsi
jmp 0x401e2f