// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movb $0x0, -0x11(%rbp)
movl $0xffffffff, -0x18(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x19, %edx
callq 0x4010a0
leaq -0x110(%rbp), %rdi
xorl %esi, %esi
movl $0xc8, %edx
callq 0x4010a0
leaq -0x11a(%rbp), %rdi
xorl %esi, %esi
movl $0xa, %edx
callq 0x4010a0
movl $0xffffffff, -0x120(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x124(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x128(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x12c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x130(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x134(%rbp)
movl $0x0, -0x138(%rbp)
cmpl $-0x1, -0x138(%rbp)
je 0x401ea5
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0xbc811(%rip), %rdx     # 0x4be383
callq 0x45fec0
movl %eax, -0x138(%rbp)
movl -0x138(%rbp), %eax
incl %eax
movl %eax, %ecx
movq %rcx, -0x148(%rbp)
subl $0x7a, %eax
ja 0x401e8f
movq -0x148(%rbp), %rax
leaq 0xbc45f(%rip), %rcx     # 0x4be004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x11a(%rbp), %rdi
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rcx
movl $0x9, %esi
leaq 0xd9ba6(%rip), %rdx     # 0x4db771
movb $0x0, %al
callq 0x40d8c0
jmp 0x401ea0
movq $0x4f7740, %rax         # imm = 0x4F7740
cmpq $0x0, (%rax)
je 0x401bfb
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40bb10
movl %eax, -0x120(%rbp)
jmp 0x401c05
movl $0x1, -0x124(%rbp)
jmp 0x401ea0
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40bb10
movl %eax, -0x130(%rbp)
jmp 0x401ea0
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40bb10
movl %eax, -0x134(%rbp)
jmp 0x401ea0
callq 0x4019f0
movl $0x0, -0x4(%rbp)
jmp 0x402193
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40bb10
movl %eax, -0x12c(%rbp)
jmp 0x401ea0
leaq -0x40(%rbp), %rdi
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rsi
movl $0x19, %edx
callq 0x401080
jmp 0x401ea0
leaq -0x110(%rbp), %rdi
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rsi
movl $0xc8, %edx
callq 0x401080
jmp 0x401ea0
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40bb10
movb %al, -0x11(%rbp)
jmp 0x401ea0
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40bb10
movl %eax, -0x128(%rbp)
cmpl $0x0, -0x128(%rbp)
je 0x401d01
cmpl $0xb4, -0x128(%rbp)
je 0x401d01
leaq 0xbc6ae(%rip), %rdi     # 0x4be39c
movb $0x0, %al
callq 0x40d7f0
movl $0x1, -0x4(%rbp)
jmp 0x402193
jmp 0x401ea0
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40bb10
movl %eax, -0x18(%rbp)
jmp 0x401ea0
movq $0x4f7740, %rax         # imm = 0x4F7740
movq (%rax), %rdi
callq 0x40bb10
movl %eax, -0x1c(%rbp)
jmp 0x401ea0
jmp 0x401ea0
movq $0x4f1098, %rax         # imm = 0x4F1098
cmpl $0x49, (%rax)
jne 0x401d68
movq $0x4f1098, %rax         # imm = 0x4F1098
movl (%rax), %esi
leaq 0xbc66b(%rip), %rdi     # 0x4be3c0
movb $0x0, %al
callq 0x40d7f0
movl $0x1, -0x4(%rbp)
jmp 0x402193
movq $0x4f1098, %rax         # imm = 0x4F1098
cmpl $0x64, (%rax)
je 0x401d8c
movq $0x4f1098, %rax         # imm = 0x4F1098
cmpl $0x66, (%rax)
je 0x401d8c
movq $0x4f1098, %rax         # imm = 0x4F1098
cmpl $0x69, (%rax)
jne 0x401daf
movq $0x4f1098, %rax         # imm = 0x4F1098
movl (%rax), %esi
leaq 0xbc657(%rip), %rdi     # 0x4be3f3
movb $0x0, %al
callq 0x40d7f0
movl $0x1, -0x4(%rbp)
jmp 0x402193
movq $0x4f1098, %rax         # imm = 0x4F1098
cmpl $0x6c, (%rax)
je 0x401dc7
movq $0x4f1098, %rax         # imm = 0x4F1098
cmpl $0x6d, (%rax)
jne 0x401dea
movq $0x4f1098, %rax         # imm = 0x4F1098
movl (%rax), %esi
leaq 0xbc63d(%rip), %rdi     # 0x4be414
movb $0x0, %al
callq 0x40d7f0
movl $0x1, -0x4(%rbp)
jmp 0x402193
movq $0x4f1098, %rax         # imm = 0x4F1098
cmpl $0x6e, (%rax)
jne 0x401e19
movq $0x4f1098, %rax         # imm = 0x4F1098
movl (%rax), %esi
leaq 0xbc628(%rip), %rdi     # 0x4be42e
movb $0x0, %al
callq 0x40d7f0
movl $0x1, -0x4(%rbp)
jmp 0x402193
movq $0x4f1098, %rax         # imm = 0x4F1098
cmpl $0x72, (%rax)
jne 0x401e48
movq $0x4f1098, %rax         # imm = 0x4F1098
movl (%rax), %esi
leaq 0xbc62c(%rip), %rdi     # 0x4be461
movb $0x0, %al
callq 0x40d7f0
movl $0x1, -0x4(%rbp)
jmp 0x402193
movq $0x4f1098, %rax         # imm = 0x4F1098
cmpl $0x78, (%rax)
je 0x401e60
movq $0x4f1098, %rax         # imm = 0x4F1098
cmpl $0x79, (%rax)
jne 0x401e83
movq $0x4f1098, %rax         # imm = 0x4F1098
movl (%rax), %esi
leaq 0xbc614(%rip), %rdi     # 0x4be484
movb $0x0, %al
callq 0x40d7f0
movl $0x1, -0x4(%rbp)
jmp 0x402193
jmp 0x401e85
jmp 0x401e87
jmp 0x401e89
jmp 0x401e8b
jmp 0x401e8d
jmp 0x401ea0
callq 0x4019f0
movl $0x1, -0x4(%rbp)
jmp 0x402193
jmp 0x401b57
movb $0x0, -0x139(%rbp)
movzbl -0x11(%rbp), %edi
callq 0x4021a0
movb %al, -0x139(%rbp)
movzbl -0x139(%rbp), %eax
cmpl $0x0, %eax
je 0x401ee5
movzbl -0x11(%rbp), %esi
leaq 0xbc5d7(%rip), %rdi     # 0x4be4a9
movb $0x0, %al
callq 0x40d7f0
movl $0x1, -0x4(%rbp)
jmp 0x402193
movsbl -0x11a(%rbp), %eax
cmpl $0x0, %eax
je 0x401faa
leaq -0x11a(%rbp), %rdi
leaq 0xbc5c7(%rip), %rsi     # 0x4be4ca
callq 0x4010b0
cmpl $0x0, %eax
jne 0x401f30
movl $0x40, %edi
movl $0x80, %esi
callq 0x402850
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
jmp 0x401fa8
leaq -0x11a(%rbp), %rdi
leaq 0xbc593(%rip), %rsi     # 0x4be4d1
callq 0x4010b0
cmpl $0x0, %eax
jne 0x401f6b
movl $0x20, %edi
movl $0x80, %esi
callq 0x402850
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
jmp 0x401fa6
leaq -0x11a(%rbp), %rdi
leaq 0xbc55f(%rip), %rsi     # 0x4be4d8
callq 0x4010b0
cmpl $0x0, %eax
jne 0x401fa4
movl $0x30, %edi
movl $0x40, %esi
callq 0x402850
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
jmp 0x401fa6
jmp 0x401fa8
jmp 0x401fd5
movb $0x0, %al
callq 0x403120
movzbl %al, %eax
cmpl $0x0, %eax
je 0x401fd3
leaq 0xbc51e(%rip), %rdi     # 0x4be4de
movb $0x0, %al
callq 0x40d7f0
movl $0x1, -0x4(%rbp)
jmp 0x402193
jmp 0x401fd5
cmpl $-0x1, -0x124(%rbp)
jle 0x401ff9
movb $0x0, %al
callq 0x4030c0
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
jmp 0x402024
cmpl $-0x1, -0x120(%rbp)
jle 0x402022
movl -0x120(%rbp), %eax
movzbl %al, %edi
callq 0x403020
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
jmp 0x402024
cmpl $-0x1, -0x128(%rbp)
jle 0x40204d
movl -0x128(%rbp), %eax
movzbl %al, %edi
callq 0x4027c0
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
cmpl $-0x1, -0x12c(%rbp)
jle 0x402076
movl -0x12c(%rbp), %eax
movzbl %al, %edi
callq 0x4022c0
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
cmpl $-0x1, -0x130(%rbp)
jle 0x40209f
movl -0x130(%rbp), %eax
movzbl %al, %edi
callq 0x402220
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
cmpl $-0x1, -0x18(%rbp)
jle 0x4020d2
cmpl $-0x1, -0x1c(%rbp)
jle 0x4020d2
movl -0x18(%rbp), %eax
movb %al, %cl
movl -0x1c(%rbp), %eax
movzbl %cl, %edi
movzbl %al, %esi
callq 0x402700
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
jmp 0x40211e
cmpl $-0x1, -0x18(%rbp)
jle 0x4020f7
movl -0x18(%rbp), %eax
movzbl %al, %edi
callq 0x402600
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
jmp 0x40211c
cmpl $-0x1, -0x1c(%rbp)
jle 0x40211a
movl -0x1c(%rbp), %eax
movzbl %al, %edi
callq 0x402680
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
jmp 0x40211c
jmp 0x40211e
movsbl -0x110(%rbp), %eax
cmpl $0x0, %eax
je 0x402153
movl -0x134(%rbp), %eax
leaq -0x110(%rbp), %rsi
movzbl %al, %edi
callq 0x402ee0
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
jmp 0x402182
movsbl -0x40(%rbp), %eax
cmpl $0x0, %eax
je 0x402180
movl -0x134(%rbp), %eax
leaq -0x40(%rbp), %rsi
movzbl %al, %edi
callq 0x402d10
movzbl %al, %ecx
movzbl -0x139(%rbp), %eax
addl %ecx, %eax
movb %al, -0x139(%rbp)
jmp 0x402182
movb $0x0, %al
callq 0x402210
movzbl -0x139(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nop