// File: 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:
subq $0x3b8, %rsp            # imm = 0x3B8
movl $0x0, 0x3b4(%rsp)
movl %edi, 0x3b0(%rsp)
movq %rsi, 0x3a8(%rsp)
movl $0xa, 0x3a4(%rsp)
cmpl $0x5, 0x3b0(%rsp)
jge 0x67d6
movq 0x3a8(%rsp), %rax
movq (%rax), %rdi
callq 0x3bd0
movl $0x1, 0x3b4(%rsp)
jmp 0x6deb
movq 0x3a8(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x7d1d(%rip), %rsi      # 0xe506
callq 0x3380
cmpl $0x0, %eax
jne 0x6940
cmpl $0x5, 0x3b0(%rsp)
je 0x6811
movq 0x3a8(%rsp), %rax
movq (%rax), %rdi
callq 0x3bd0
movq 0x3a8(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0x338(%rsp), %rdi
callq 0x85a0
movq 0x3a8(%rsp), %rax
movq 0x18(%rax), %rsi
leaq 0x2d0(%rsp), %rdi
callq 0x85a0
jmp 0x6845
movl $0x0, 0x2c0(%rsp)
movl 0x3a4(%rsp), %r8d
leaq 0x2a8(%rsp), %rdi
leaq 0x338(%rsp), %rsi
leaq 0x2d0(%rsp), %rdx
leaq 0x2c0(%rsp), %rcx
callq 0x4cf0
jmp 0x687f
movq 0x3a8(%rsp), %rax
movq 0x20(%rax), %rdi
movl 0x3a4(%rsp), %edx
movl 0x2c0(%rsp), %ecx
leaq 0x2a8(%rsp), %rsi
callq 0x6370
jmp 0x68a8
leaq 0x2a8(%rsp), %rdi
callq 0x7ee0
leaq 0x2d0(%rsp), %rdi
callq 0x8310
leaq 0x338(%rsp), %rdi
callq 0x8310
jmp 0x6de0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x692e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x6921
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x2a8(%rsp), %rdi
callq 0x7ee0
leaq 0x2d0(%rsp), %rdi
callq 0x8310
leaq 0x338(%rsp), %rdi
callq 0x8310
jmp 0x6dfa
movq 0x3a8(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x7bb9(%rip), %rsi      # 0xe50c
callq 0x3380
cmpl $0x0, %eax
jne 0x6b85
cmpl $0x5, 0x3b0(%rsp)
je 0x697b
movq 0x3a8(%rsp), %rax
movq (%rax), %rdi
callq 0x3bd0
movq 0x3a8(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0x240(%rsp), %rdi
callq 0x85a0
movl $0x0, 0x23c(%rsp)
movq 0x3a8(%rsp), %rax
movq 0x18(%rax), %rsi
leaq 0x220(%rsp), %rdi
leaq 0x3a4(%rsp), %rdx
leaq 0x23c(%rsp), %rcx
callq 0x65a0
jmp 0x69ca
leaq 0xe8(%rsp), %rdi
leaq 0x240(%rsp), %rsi
callq 0x8670
jmp 0x69e1
leaq 0xd0(%rsp), %rdi
leaq 0x220(%rsp), %rsi
callq 0x87b0
jmp 0x69f8
movl 0x3a4(%rsp), %ecx
leaq 0x150(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
callq 0x5210
jmp 0x6a1e
movl 0x23c(%rsp), %edx
leaq 0x1b8(%rsp), %rdi
leaq 0x150(%rsp), %rsi
callq 0x5590
jmp 0x6a3c
movq 0x3a8(%rsp), %rax
movq 0x20(%rax), %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x88b0
jmp 0x6a57
leaq 0x1b8(%rsp), %rdi
callq 0x8310
leaq 0x150(%rsp), %rdi
callq 0x8310
leaq 0xd0(%rsp), %rdi
callq 0x7ee0
leaq 0xe8(%rsp), %rdi
callq 0x8310
leaq 0x220(%rsp), %rdi
callq 0x7ee0
leaq 0x240(%rsp), %rdi
callq 0x8310
jmp 0x6dde
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x6b73
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x6b66
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x6b59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x6b4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x6b3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x1b8(%rsp), %rdi
callq 0x8310
leaq 0x150(%rsp), %rdi
callq 0x8310
leaq 0xd0(%rsp), %rdi
callq 0x7ee0
leaq 0xe8(%rsp), %rdi
callq 0x8310
leaq 0x220(%rsp), %rdi
callq 0x7ee0
leaq 0x240(%rsp), %rdi
callq 0x8310
jmp 0x6dfa
movq 0x3a8(%rsp), %rax
movq 0x8(%rax), %rdi
leaq 0x797c(%rip), %rsi      # 0xe514
callq 0x3380
cmpl $0x0, %eax
jne 0x6dbf
leaq 0xb8(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x8a70
movq 0x18(%rsp), %rdi
movl 0x3b0(%rsp), %eax
addl $-0x3, %eax
movslq %eax, %rsi
callq 0x8a80
jmp 0x6bd1
movl $0x3, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
cmpl 0x3b0(%rsp), %eax
jge 0x6c9b
movl $0x68, %edi
callq 0x3260
movq %rax, 0x10(%rsp)
jmp 0x6c01
movq 0x10(%rsp), %rdi
movq 0x3a8(%rsp), %rax
movslq 0xb4(%rsp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x85a0
jmp 0x6c21
movq 0x10(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x8b90
jmp 0x6c45
jmp 0x6c47
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x6bdc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x6db0
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
movl $0x68, %esi
callq 0x3270
jmp 0x6db0
leaq 0x28(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x8bc0
jmp 0x6caf
leaq 0x40(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x5710
jmp 0x6cc0
movq 0x3a8(%rsp), %rax
movq 0x10(%rax), %rsi
leaq 0x40(%rsp), %rdi
callq 0x88b0
jmp 0x6cd8
leaq 0x40(%rsp), %rdi
callq 0x8310
leaq 0x28(%rsp), %rdi
callq 0x8cc0
movl $0x0, 0x24(%rsp)
movslq 0x24(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x8440
movq %rax, %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
jae 0x6da1
movslq 0x24(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x8420
movq (%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x6d52
movq (%rsp), %rdi
callq 0x8310
movq (%rsp), %rdi
movl $0x68, %esi
callq 0x3270
jmp 0x6d54
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x6cf4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
jmp 0x6d95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c8(%rsp)
movl %eax, 0x2c4(%rsp)
leaq 0x40(%rsp), %rdi
callq 0x8310
leaq 0x28(%rsp), %rdi
callq 0x8cc0
jmp 0x6db0
leaq 0xb8(%rsp), %rdi
callq 0x8cc0
jmp 0x6ddc
leaq 0xb8(%rsp), %rdi
callq 0x8cc0
jmp 0x6dfa
movq 0x3a8(%rsp), %rax
movq (%rax), %rdi
callq 0x3bd0
movl $0x1, 0x3b4(%rsp)
jmp 0x6deb
jmp 0x6dde
jmp 0x6de0
movl $0x0, 0x3b4(%rsp)
movl 0x3b4(%rsp), %eax
addq $0x3b8, %rsp            # imm = 0x3B8
retq
movq 0x2c8(%rsp), %rdi
callq 0x3460
nopw (%rax,%rax)