// File: /workspace/github_cpp_makefile_star2_O2/iibrahimli[P]adsa_project/src/main.cpp
// Function: main
int main(int argc, char *argv[]){

    // main just parses arguments and calls needed functions

    unsigned int resolution = 10;  // thickness of horizontal strips

    if(argc < 5){
        print_help(argv[0]);
        return 1;
    }

    if(!strcmp(argv[1], "--psf")){
        if(argc != 1+4) print_help(argv[0]);

        BMP_image a(argv[2]);
        BMP_image b(argv[3]);
        int maxshift = 0;

        auto psf = calc_psf(a, b, &maxshift, resolution);

        // psf file format: n_strips \n thickness \n maxshift \n psf1 \n ... psfN \n
        write_psf(argv[4], psf, resolution, maxshift);
    }
    else if(!strcmp(argv[1], "--scale")){
        if(argc != 1+4) print_help(argv[0]);
        
        BMP_image a(argv[2]);
        int maxshift = 0;

        auto psf = read_psf(argv[3], &resolution, &maxshift);

        cut_strip(psf_resize(a, psf, resolution), maxshift).save_as(argv[4]);
        
    }
    else if(!strcmp(argv[1], "--merge")){
        vector<const BMP_image*> images;
        images.reserve(argc-3);

        for(int arg=3; arg<argc; ++arg){
            images.push_back(new BMP_image(argv[arg]));
        }
        merge(images).save_as(argv[2]);
        
        // free memory
        for(int i=0; i<images.size(); ++i) delete images[i];
    }
    else{
        print_help(argv[0]);
        return 1;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rsi, %rbx
movl $0xa, (%rsp)
cmpl $0x4, %edi
jg 0x3dce
movq (%rbx), %rdi
callq 0x2829
pushq $0x1
popq %rax
jmp 0x3fe6
movl %edi, %r14d
movq 0x8(%rbx), %r15
leaq 0x272a(%rip), %rsi      # 0x6506
movq %r15, %rdi
callq 0x22a0
testl %eax, %eax
je 0x3e7b
leaq 0x2719(%rip), %rsi      # 0x650c
movq %r15, %rdi
callq 0x22a0
testl %eax, %eax
je 0x3f02
leaq 0x270a(%rip), %rsi      # 0x6514
movq %r15, %rdi
callq 0x22a0
testl %eax, %eax
jne 0x3dbe
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
leal -0x3(%r14), %esi
callq 0x4ce4
movl %r14d, %ebp
pushq $0x3
popq %r14
leaq 0x50(%rsp), %r15
leaq 0xc0(%rsp), %r12
cmpq %r14, %rbp
je 0x3ff8
pushq $0x68
popq %rdi
callq 0x21f0
movq %rax, %r13
movq (%rbx,%r14,8), %rsi
movq %rax, %rdi
callq 0x4a5c
movq %r13, 0xc0(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x5ad0
incq %r14
jmp 0x3e43
cmpl $0x5, %r14d
je 0x3e89
movq (%rbx), %rdi
callq 0x2829
movq 0x10(%rbx), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x4a5c
movq 0x18(%rbx), %rsi
leaq 0x50(%rsp), %rdi
callq 0x4a5c
leaq 0x8(%rsp), %rcx
andl $0x0, (%rcx)
leaq 0x128(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0x50(%rsp), %rdx
pushq $0xa
popq %r8
callq 0x32f2
movq 0x20(%rbx), %rdi
movl 0x8(%rsp), %ecx
leaq 0x128(%rsp), %rsi
pushq $0xa
popq %rdx
callq 0x3b57
leaq 0x128(%rsp), %rdi
callq 0x5680
leaq 0x50(%rsp), %rdi
callq 0x4a1a
jmp 0x3fd7
cmpl $0x5, %r14d
je 0x3f10
movq (%rbx), %rdi
callq 0x2829
movq 0x10(%rbx), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x4a5c
leaq 0x4(%rsp), %rcx
andl $0x0, (%rcx)
movq 0x18(%rbx), %rsi
leaq 0x8(%rsp), %rdi
movq %rsp, %rdx
callq 0x3c71
leaq 0x190(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x4ada
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x4b9e
movl (%rsp), %ecx
leaq 0x128(%rsp), %rdi
leaq 0x190(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x34b5
movl 0x4(%rsp), %edx
leaq 0x50(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x360a
movq 0x20(%rbx), %rsi
leaq 0x50(%rsp), %rdi
callq 0x4bec
leaq 0x50(%rsp), %rdi
callq 0x4a1a
leaq 0x128(%rsp), %rdi
callq 0x4a1a
leaq 0x38(%rsp), %rdi
callq 0x5680
leaq 0x190(%rsp), %rdi
callq 0x4a1a
leaq 0x8(%rsp), %rdi
callq 0x5680
leaq 0xc0(%rsp), %rdi
callq 0x4a1a
xorl %eax, %eax
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4d7a
leaq 0xc0(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x36c9
movq 0x10(%rbx), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x4bec
leaq 0xc0(%rsp), %rdi
callq 0x4a1a
leaq 0x20(%rsp), %rdi
callq 0x5a60
xorl %r15d, %r15d
pushq $0x68
popq %rbx
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r15, %rcx
jbe 0x407e
movq (%rax,%r15,8), %r14
testq %r14, %r14
je 0x406e
movq %r14, %rdi
callq 0x4a1a
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2200
incq %r15
jmp 0x4047
leaq 0x50(%rsp), %rdi
callq 0x5a60
jmp 0x3fe4
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x4a1a
jmp 0x40a2
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x5a60
jmp 0x4154
jmp 0x4151
jmp 0x4151
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4a1a
jmp 0x40cd
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x4a1a
jmp 0x40df
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x5680
jmp 0x40ee
movq %rax, %rbx
leaq 0x190(%rsp), %rdi
callq 0x4a1a
jmp 0x4100
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x5680
jmp 0x4132
jmp 0x412f
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x5680
jmp 0x4123
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4a1a
jmp 0x4132
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x4a1a
jmp 0x415e
movq %rax, %rbx
pushq $0x68
popq %rsi
movq %r13, %rdi
callq 0x2200
jmp 0x4154
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x5a60
movq %rbx, %rdi
callq 0x2380