// File: /workspace/github_cpp_makefile_star2_O1/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 $0x4e8, %rsp            # imm = 0x4E8
movq %rsi, %r14
movl %edi, %ebp
xorl %r12d, %r12d
leaq 0x1a27(%rip), %r15      # 0x3050
movq 0x3988(%rip), %r13      # 0x4fb8
xorl %ebx, %ebx
movl %ebp, %edi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1060
cmpl $0x6c, %eax
jle 0x165a
cmpl $0x76, %eax
jne 0x1670
movq (%r13), %rdi
callq 0x1190
movl %eax, 0x3ab4(%rip)      # 0x510c
jmp 0x1632
cmpl $0x68, %eax
jne 0x1687
movq (%r13), %rdi
callq 0x1190
movl %eax, 0x3a9a(%rip)      # 0x5108
jmp 0x1632
cmpl $0x6d, %eax
je 0x1696
cmpl $0x74, %eax
jne 0x1632
movq (%r13), %rdi
callq 0x1190
movl %eax, %ebx
jmp 0x1632
cmpl $-0x1, %eax
je 0x169b
cmpl $0x3f, %eax
jne 0x1632
jmp 0x1ccc
movb $0x1, %r12b
jmp 0x1632
movq 0x3936(%rip), %rax      # 0x4fd8
movslq (%rax), %rax
cmpl %ebp, %eax
jge 0x16b8
movq (%r14,%rax,8), %rax
xorl %edx, %edx
cmpb $0x0, (%rax)
cmovneq %rax, %rdx
jmp 0x16ba
xorl %edx, %edx
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x1ee0
movl 0x41c(%r14), %ecx
decl %ecx
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x20b6
movl 0x3a22(%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 0x3a02(%rip), %edx      # 0x510c
leaq 0x38(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1529
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x1730
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1747
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0xb8(%rsp), %rsi
movl 0x41c(%rsi), %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movq %rsp, %rdi
xorl %edx, %edx
callq 0x20b6
movl 0x399e(%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 0x3980(%rip), %edx      # 0x510c
leaq 0x68(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x1529
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x17b2
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x17c8
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0xb8(%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 0x20b6
movl 0x3919(%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 0x38fb(%rip), %edx      # 0x510c
leaq 0x18(%rsp), %rdi
movq %rsp, %rsi
callq 0x1529
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1834
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x184b
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 0x189f
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 0x1883
movsd %xmm2, 0x30(%rsp)
cmpq $0x2, %r14
jb 0x18d2
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 0x18b6
leaq -0x1(%r14), %rax
movq %rax, %xmm1
punpckldq 0x172d(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1735(%rip), %xmm1     # 0x3020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x179a(%rip), %rdi      # 0x309c
movq %r14, %rsi
movb $0x1, %al
callq 0x1040
leaq 0xb8(%rsp), %rsi
movl 0x420(%rsi), %ecx
decl %ecx
leaq 0x88(%rsp), %rdi
xorl %edx, %edx
callq 0x215c
movl 0x37d7(%rip), %eax      # 0x5108
leal 0x3(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
sarl $0x2, %edx
leaq 0x50(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x1436
movl 0x37b4(%rip), %edx      # 0x5108
movq %rsp, %rdi
leaq 0x50(%rsp), %rsi
callq 0x1529
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x1978
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x1995
movq 0x98(%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 0x19d2
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 0x19b6
leaq -0x1(%rsi), %rax
movq %rax, %xmm1
punpckldq 0x162d(%rip), %xmm1 # xmm1 = xmm1[0],mem[0],xmm1[1],mem[1]
subpd 0x1635(%rip), %xmm1     # 0x3020
movapd %xmm1, %xmm2
unpckhpd %xmm1, %xmm2            # xmm2 = xmm2[1],xmm1[1]
addsd %xmm1, %xmm2
divsd %xmm2, %xmm0
leaq 0x16c8(%rip), %rdi      # 0x30ca
movb $0x1, %al
callq 0x1040
testb $0x1, %r12b
jne 0x1cd1
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
testl %ecx, %ecx
movsd 0x30(%rsp), %xmm2
jle 0x1c50
movq %r14, %xmm0
punpckldq 0x15d1(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x15d9(%rip), %xmm0     # 0x3020
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd 0x15e1(%rip), %xmm0     # 0x3040
mulsd %xmm2, %xmm0
movsd %xmm0, 0x80(%rsp)
leaq 0xb8(%rsp), %r14
xorl %r12d, %r12d
movsd %xmm2, 0x30(%rsp)
movq (%rsp), %rcx
movq 0x8(%rsp), %r13
subq %rcx, %r13
shrq $0x2, %r13
leal -0x1(%r13), %edx
movl (%rax,%r12,4), %esi
movl (%rcx,%rdx,4), %edx
movl 0x366a(%rip), %eax      # 0x5108
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movl 0x365f(%rip), %eax      # 0x510c
movl %eax, %r8d
shrl $0x1f, %r8d
addl %eax, %r8d
sarl %r8d
movq %r14, %rdi
callq 0x2382
leal -0x2(%r13), %ebp
leal -0x61(%r13), %eax
movl %eax, %r15d
testl %ebp, %ebp
je 0x1b60
movq 0x38(%rsp), %rax
movl (%rax,%r12,4), %esi
movq (%rsp), %rax
movl (%rax,%rbp,4), %edx
movl 0x361d(%rip), %eax      # 0x5108
movl %eax, %ecx
shrl $0x1f, %ecx
addl %eax, %ecx
sarl %ecx
movl 0x3612(%rip), %eax      # 0x510c
movl %eax, %r8d
shrl $0x1f, %r8d
addl %eax, %r8d
sarl %r8d
movq %r14, %rdi
movapd %xmm0, 0xa0(%rsp)
callq 0x2382
movapd 0xa0(%rsp), %xmm3
movapd %xmm3, %xmm1
mulsd 0x151b(%rip), %xmm1     # 0x3048
decl %r13d
decq %rbp
ucomisd %xmm0, %xmm1
leal -0x1(%r15), %eax
movapd %xmm0, %xmm2
cmpltsd %xmm1, %xmm2
movapd %xmm2, %xmm1
andnpd %xmm0, %xmm1
andpd %xmm3, %xmm2
orpd %xmm1, %xmm2
movapd %xmm2, %xmm0
jbe 0x1aca
jmp 0x1b66
movl $0x1, %r13d
addl $-0x60, %r13d
movq 0x345f(%rip), %rax      # 0x4fd0
movq (%rax), %rdi
leaq 0x1577(%rip), %rsi      # 0x30f2
movl %r12d, %edx
movl %r13d, %ecx
xorl %eax, %eax
callq 0x1170
xorl %ebp, %ebp
movsd 0x30(%rsp), %xmm2
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 0x1bc3
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
subsd %xmm2, %xmm0
jmp 0x1bfd
incq %rdx
shrq %rdx
cmpl %edx, %r15d
jge 0x1be0
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
subsd 0x80(%rsp), %xmm0
jmp 0x1bfd
addl %esi, %edx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
cmpl %edx, %r15d
jge 0x1bf9
addsd 0x80(%rsp), %xmm0
jmp 0x1bfd
addsd %xmm2, %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 0x2488
incq %rbp
incl %r15d
cmpq $0x60, %rbp
movsd 0x30(%rsp), %xmm2
jne 0x1b90
incq %r12
movq 0x38(%rsp), %rax
movq 0x40(%rsp), %rcx
subq %rax, %rcx
shrq $0x2, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r12
jl 0x1a7d
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1c66
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1c7d
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x1c94
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x1cab
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0xb8(%rsp), %rdi
callq 0x277a
xorl %eax, %eax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x1200
leaq 0xb8(%rsp), %rdi
leaq 0x38(%rsp), %rsi
movq %rsp, %rdx
callq 0x221a
xorl %edi, %edi
callq 0x1150
jmp 0x1de2
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x1d11
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
jmp 0x1d11
movq %rax, %rbx
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x1dfb
movq 0x98(%rsp), %rsi
jmp 0x1df3
movq %rax, %rbx
jmp 0x1dfb
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1d55
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
jmp 0x1d55
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x1e12
movq 0x60(%rsp), %rsi
jmp 0x1e0a
movq %rax, %rbx
jmp 0x1e12
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1d94
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
jmp 0x1d94
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1e29
movq 0x10(%rsp), %rsi
jmp 0x1e21
movq %rax, %rbx
jmp 0x1e29
movq %rax, %rbx
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x1dcc
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
jmp 0x1dcc
movq %rax, %rbx
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1e40
movq 0x28(%rsp), %rsi
jmp 0x1e38
movq %rax, %rbx
jmp 0x1e40
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x1dfb
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x18(%rsp), %rdi
testq %rdi, %rdi
je 0x1e12
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x1e29
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x1e40
movq 0x48(%rsp), %rsi
subq %rdi, %rsi
callq 0x1120
leaq 0xb8(%rsp), %rdi
callq 0x277a
movq %rbx, %rdi
callq 0x11b0
nop