// File: /workspace/github_cpp_makefile_star2_O3/leobru[P]ACPUfont/splitter.cc
// Function: main
int main(int argc, char**argv) {
    int full, full_thr, cir_thr;
    char p2[3];
    int top_heavy_corr = 0;
    int opt;
    const char * postproc = NULL;
    bool do_dump_marked = false;
    while ((opt = getopt(argc, argv, "v:h:t:m")) != -1) {
        switch (opt) {
        case 'v':
            vsize = atoi(optarg);
            break;
        case 'h':
            hsize = atoi(optarg);
            break;
        case 't':
            top_heavy_corr = atoi(optarg);
            break;
        case 'm':
            do_dump_marked = true;
            break;
        case '?':
            fprintf(stderr, "Usage: splitter [-t N] [-v N] [-h N] [-m] [postprocessing command]\n");
            exit(1);
        }
    }
    if (optind < argc) {
        postproc = argv[optind];
        if (!*postproc) postproc = NULL;
    }
    Picture array(NULL, postproc);
    // Find local minima (darkest spots)
    vector<int> horcenters = centers(smooth(array.average_lines(0, array.width-1), vsize/4), vsize);
    vector<int> leftcenters = centers(smooth(array.average_lines(0, array.width/2), vsize/4), vsize);
    vector<int> rightcenters = centers(smooth(array.average_lines(array.width/2+1, array.width-1), vsize/4), vsize);

    double skew = 0;
    for (int i = 0; i < horcenters.size(); ++i) {
        skew += rightcenters[i]-leftcenters[i];
    }
    skew /= horcenters.size();

    // Find average interval
    double interv = 0;
    for (uint i = 1; i < horcenters.size(); ++i) interv += horcenters[i]-horcenters[i-1];
    interv /= horcenters.size()-1;
    printf("Found %d lines, average line interval = %.2f\n", horcenters.size(), interv);

    vector<int> vercenters = centers(smooth(array.average_columns(0, array.height-1), hsize/4), hsize);
    interv = 0;
    for (uint i = 1; i < vercenters.size(); ++i) interv += vercenters[i]-vercenters[i-1];
    interv /= vercenters.size()-1;
    printf("Found %d columns, average pitch = %.2f\n", vercenters.size(), interv);
    if (do_dump_marked) {
        array.dump_marked(&horcenters, &vercenters);
        exit(0);
    }
    
    for (int line = 0; line < (int)horcenters.size(); ++line) {
        // Looking from the right, find the column of the apostrophe (that's when whiteness drops)
        uint col = vercenters.size()-1;
        double avg = array.rectavg(horcenters[line], vercenters[col], hsize/2, vsize/2);
        for (; --col; ) {
            double cur = array.rectavg(horcenters[line], vercenters[col], hsize/2, vsize/2);
            if (cur < avg*0.95) break;
            avg = cur;
        }
        // 'col' is the column number of GOST 0137
        col -= 0137;
        fprintf(stderr, "Dumping line %d starting from column %d\n", line, col);
	for (int gost = 0; gost <= 0137; ++gost) {
            int hci = horcenters[line];
            int v4 = (vercenters.size()+2)/4, v2 = (vercenters.size()+1)/2;
            int curcol = gost + col;
            int hc = curcol < v4  ? hci-skew : curcol < v2 ? hci-skew/2 : curcol < v2+v4 ? hci+skew/2 : hci+skew;
            int vc = vercenters[curcol];
            // improve(array, hc, vc);
            array.dump(hc+top_heavy_corr, vc, line, col, gost);
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rsi, %r14
movl %edi, %ebp
xorl %r12d, %r12d
leaq 0x1a21(%rip), %r15      # 0x3050
movq 0x3982(%rip), %r13      # 0x4fb8
xorl %ebx, %ebx
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1060
cmpl $0x6c, %eax
jle 0x1660
cmpl $0x76, %eax
jne 0x1676
movq (%r13), %rdi
callq 0x1190
movl %eax, 0x3aae(%rip)      # 0x510c
jmp 0x1638
cmpl $0x68, %eax
jne 0x168d
movq (%r13), %rdi
callq 0x1190
movl %eax, 0x3a94(%rip)      # 0x5108
jmp 0x1638
cmpl $0x6d, %eax
je 0x169c
cmpl $0x74, %eax
jne 0x1638
movq (%r13), %rdi
callq 0x1190
movl %eax, %ebx
jmp 0x1638
cmpl $-0x1, %eax
je 0x16a1
cmpl $0x3f, %eax
jne 0x1638
jmp 0x1c81
movb $0x1, %r12b
jmp 0x1638
movq 0x3930(%rip), %rax      # 0x4fd8
movslq (%rax), %rax
cmpl %ebp, %eax
jge 0x16be
movq (%r14,%rax,8), %rax
xorl %edx, %edx
cmpb $0x0, (%rax)
cmovneq %rax, %rdx
jmp 0x16c0
xorl %edx, %edx
leaq 0xa8(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x1e98
movl 0x41c(%r14), %ecx
decl %ecx
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2078
movl 0x3a1c(%rip), %eax      # 0x510c
leal 0x3(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x2, %edx
leaq 0x68(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x1436
movl 0x39fc(%rip), %edx      # 0x510c
leaq 0x38(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1529
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x1736
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x174d
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0xa8(%rsp), %rsi
movl 0x41c(%rsi), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movq %rsp, %rdi
xorl %edx, %edx
callq 0x2078
movl 0x3998(%rip), %eax      # 0x510c
leal 0x3(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x2, %edx
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x1436
movl 0x397a(%rip), %edx      # 0x510c
leaq 0x68(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x1529
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x17b8
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x17ce
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0xa8(%rsp), %rsi
movl 0x41c(%rsi), %ecx
movl %ecx, %edx
shrl $0x1f, %edx
addl %ecx, %edx
sarl %edx
incl %edx
decl %ecx
leaq 0x50(%rsp), %rdi
callq 0x2078
movl 0x3913(%rip), %eax      # 0x510c
leal 0x3(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x2, %edx
movq %rsp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x1436
movl 0x38f5(%rip), %edx      # 0x510c
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x1529
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x183a
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x1851
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, %r14
subq %rax, %r14
sarq $0x2, %r14
xorpd %xmm0, %xmm0
xorpd %xmm2, %xmm2
cmpq %rax, %rcx
je 0x18a5
movq 0x18(%rsp), %rcx
movq 0x68(%rsp), %rdx
cmpq $0x1, %r14
movq %r14, %rsi
adcq $0x0, %rsi
xorl %edi, %edi
movl (%rcx,%rdi,4), %r8d
subl (%rdx,%rdi,4), %r8d
xorps %xmm1, %xmm1
cvtsi2sd %r8d, %xmm1
addsd %xmm1, %xmm2
incq %rdi
cmpq %rdi, %rsi
jne 0x1889
movsd %xmm2, 0x30(%rsp)
cmpq $0x2, %r14
jb 0x18d8
xorpd %xmm0, %xmm0
movl $0x1, %edx
xorl %ecx, %ecx
movl (%rax,%rdx,4), %edx
subl (%rax,%rcx,4), %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
addsd %xmm1, %xmm0
leal 0x2(%rcx), %edx
incq %rcx
cmpq %rdx, %r14
ja 0x18bc
leaq -0x1(%r14), %rax
movq %rax, %xmm1
punpckldq 0x1727(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x172f(%rip), %xmm1     # 0x3020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x1794(%rip), %rdi      # 0x309c
movq %r14, %rsi
movb $0x1, %al
callq 0x1040
leaq 0xa8(%rsp), %rsi
movl 0x420(%rsi), %ecx
decl %ecx
leaq 0x90(%rsp), %rdi
xorl %edx, %edx
callq 0x211e
movl 0x37d1(%rip), %eax      # 0x5108
leal 0x3(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x2, %edx
leaq 0x50(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1436
movl 0x37ae(%rip), %edx      # 0x5108
movq %rsp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x1529
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x197e
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x199b
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq (%rsp), %rax
movq 0x8(%rsp), %rsi
subq %rax, %rsi
sarq $0x2, %rsi
xorpd %xmm0, %xmm0
cmpq $0x2, %rsi
jb 0x19d8
movl $0x1, %edx
xorl %ecx, %ecx
movl (%rax,%rdx,4), %edx
subl (%rax,%rcx,4), %edx
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
addsd %xmm1, %xmm0
leal 0x2(%rcx), %edx
incq %rcx
cmpq %rdx, %rsi
ja 0x19bc
leaq -0x1(%rsi), %rax
movq %rax, %xmm1
punpckldq 0x1627(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x162f(%rip), %xmm1     # 0x3020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x16c2(%rip), %rdi      # 0x30ca
movb $0x1, %al
callq 0x1040
testb $0x1, %r12b
jne 0x1c86
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
testl %ecx, %ecx
movsd 0x30(%rsp), %xmm1
jle 0x1c05
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
divsd %xmm0, %xmm1
movsd 0x15f4(%rip), %xmm0     # 0x3040
mulsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
leaq 0xa8(%rsp), %r14
xorl %r12d, %r12d
movsd %xmm1, 0x30(%rsp)
movq (%rsp), %rcx
movq 0x8(%rsp), %rbp
subq %rcx, %rbp
shrq $0x2, %rbp
leal -0x1(%rbp), %edx
movl (%rax,%r12,4), %esi
movl (%rcx,%rdx,4), %edx
movl 0x367e(%rip), %eax      # 0x5108
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movl 0x3673(%rip), %eax      # 0x510c
movl %eax, %r8d
shrl $0x1f, %r8d
addl %eax, %r8d
sarl %r8d
movq %r14, %rdi
callq 0x234a
leal -0x61(%rbp), %eax
addl $-0x2, %ebp
movl %eax, %r13d
testl %ebp, %ebp
je 0x1b1c
movq 0x38(%rsp), %rax
movl (%rax,%r12,4), %esi
movq (%rsp), %rax
movl (%rax,%rbp,4), %edx
movl 0x3637(%rip), %eax      # 0x5108
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movl 0x362c(%rip), %eax      # 0x510c
movl %eax, %r8d
shrl $0x1f, %r8d
addl %eax, %r8d
sarl %r8d
movq %r14, %rdi
movsd %xmm0, 0x88(%rsp)
callq 0x234a
movsd 0x88(%rsp), %xmm1
mulsd 0x1539(%rip), %xmm1     # 0x3048
leal -0x1(%r13), %eax
decq %rbp
ucomisd %xmm0, %xmm1
jbe 0x1ab4
movq 0x34ad(%rip), %rax      # 0x4fd0
movq (%rax), %rdi
leaq 0x15c5(%rip), %rsi      # 0x30f2
movl %r12d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x1170
movl %r13d, %r15d
xorl %ebp, %ebp
movsd 0x30(%rsp), %xmm1
movq 0x38(%rsp), %rax
movl (%rax,%r12,4), %ecx
movq (%rsp), %rax
movq 0x8(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
leaq 0x2(%rdx), %rsi
shrq $0x2, %rsi
cmpl %esi, %r15d
jge 0x1b78
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
subsd %xmm1, %xmm0
jmp 0x1bb2
incq %rdx
shrq %rdx
cmpl %edx, %r15d
jge 0x1b95
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
subsd 0x80(%rsp), %xmm0
jmp 0x1bb2
addl %esi, %edx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
cmpl %edx, %r15d
jge 0x1bae
addsd 0x80(%rsp), %xmm0
jmp 0x1bb2
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %esi
movslq %r15d, %rcx
movl (%rax,%rcx,4), %edx
addl %ebx, %esi
movq %r14, %rdi
movl %r12d, %ecx
movl %r13d, %r8d
movl %ebp, %r9d
callq 0x2450
incq %rbp
incl %r15d
cmpq $0x60, %rbp
movsd 0x30(%rsp), %xmm1
jne 0x1b45
incq %r12
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r12
jl 0x1a6a
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1c1b
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1c32
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x1c49
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x1c60
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0xa8(%rsp), %rdi
callq 0x2740
xorl %eax, %eax
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1200
leaq 0xa8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movq %rsp, %rdx
callq 0x21dc
xorl %edi, %edi
callq 0x1150
jmp 0x1d97
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x1cc6
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
jmp 0x1cc6
movq %rax, %rbx
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x1db0
movq 0xa0(%rsp), %rsi
jmp 0x1da8
movq %rax, %rbx
jmp 0x1db0
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1d0a
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
jmp 0x1d0a
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x1dc7
movq 0x60(%rsp), %rsi
jmp 0x1dbf
movq %rax, %rbx
jmp 0x1dc7
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1d49
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
jmp 0x1d49
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1dde
movq 0x10(%rsp), %rsi
jmp 0x1dd6
movq %rax, %rbx
jmp 0x1dde
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x1d81
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
jmp 0x1d81
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1df5
movq 0x28(%rsp), %rsi
jmp 0x1ded
movq %rax, %rbx
jmp 0x1df5
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1db0
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1dc7
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x1dde
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x1df5
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0xa8(%rsp), %rdi
callq 0x2740
movq %rbx, %rdi
callq 0x11b0