// File: /workspace/github_cpp_makefile_star2_O3/jpbalarini[P]otsus/./algo.cpp
// Function: algorithm(int, char**)
void algorithm(int argc, char **argv) {
  // Optional parameters
  vector <OptStruct *> options;
  // Mandatory parameters
  vector <ParStruct *> parameters;

  OptStruct oThreshold = {"t:", 0,  NULL, NULL, "override threshold"};
  options.push_back(&oThreshold);

  ParStruct pinput = {"input", NULL, "input image"}; parameters.push_back(&pinput);
  ParStruct pout = {"output", NULL, "output image"}; parameters.push_back(&pout);

  if (!parsecmdline("otsus", "otsu's segmentation",
                    argc, argv, options, parameters)) {
    printf("\n");
    exit(-1);
  }

  // Optional parameters
  bool override_thres = oThreshold.flag;
  int overrided_threshold = 0;
  if (override_thres){
    overrided_threshold = atoi(oThreshold.value);
    // Check threshold bounds
    if (overrided_threshold > MAX_INTENSITY || overrided_threshold < 0){
      printf("Invalid threshold value\n");
      exit(-1);
    }
  }

  // Load input image
  CImage input;
  input.load((char*)pinput.value);

  // Input image properties
  int bits = input.get_bits_per_channel();
  int num_channels = input.get_num_channels();
  int Nx = input.get_width();
  int Ny = input.get_height();
  long int N = Nx * Ny;

  if (num_channels > 1){
    fprintf(stderr, "Algorithm works only with grayscale images\n");
    exit(-1);
  }
  // Create output
  CImage *output = new CImage(Nx, Ny,
                              bits,
                              num_channels);

  unsigned hist[MAX_INTENSITY + 1];
  computeHistogram(input, hist);

  computeOtsusSegmentation(input, hist, output, overrided_threshold);

  // Save output
  output->save((char*)pout.value, bits);
  delete output;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, %rbx
movl %edi, %r14d
xorps %xmm0, %xmm0
leaq -0x90(%rbp), %rdi
movaps %xmm0, (%rdi)
xorl %eax, %eax
movq %rax, 0x10(%rdi)
movq %rax, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
movq 0x488f(%rip), %rax      # 0xbd28
leaq -0x100(%rbp), %rcx
movq %rax, 0x20(%rcx)
movups 0x486d(%rip), %xmm0     # 0xbd18
movaps %xmm0, 0x10(%rcx)
movups 0x4852(%rip), %xmm0     # 0xbd08
movaps %xmm0, (%rcx)
leaq -0x50(%rbp), %rsi
movq %rcx, (%rsi)
callq 0x7726
movq 0x4874(%rip), %rax      # 0xbd40
leaq -0xd0(%rbp), %rcx
movq %rax, 0x10(%rcx)
movups 0x4852(%rip), %xmm0     # 0xbd30
movaps %xmm0, (%rcx)
leaq -0x50(%rbp), %rsi
movq %rcx, (%rsi)
leaq -0x70(%rbp), %rdi
callq 0x789a
movq 0x4860(%rip), %rax      # 0xbd58
leaq -0xb0(%rbp), %rcx
movq %rax, 0x10(%rcx)
movups 0x483e(%rip), %xmm0     # 0xbd48
movaps %xmm0, (%rcx)
leaq -0x50(%rbp), %rsi
movq %rcx, (%rsi)
leaq -0x70(%rbp), %rdi
callq 0x789a
leaq 0x16c0(%rip), %rdi      # 0x8be4
leaq 0x16bf(%rip), %rsi      # 0x8bea
leaq -0x90(%rbp), %r8
leaq -0x70(%rbp), %r9
movl %r14d, %edx
movq %rbx, %rcx
callq 0x4af8
testl %eax, %eax
je 0x769f
cmpl $0x0, -0xf8(%rbp)
je 0x757b
movq -0xe8(%rbp), %rdi
callq 0x3410
testl %eax, %eax
js 0x756a
cmpl 0x4d48(%rip), %eax      # 0xc2b0
jle 0x757d
leaq 0x16b9(%rip), %rdi      # 0x8c2a
callq 0x3400
jmp 0x76a9
xorl %eax, %eax
movl %eax, -0x2c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4e36
movq -0xc8(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x4fb8
leaq -0x50(%rbp), %rdi
callq 0x56e0
movl %eax, %r14d
leaq -0x50(%rbp), %rdi
callq 0x4f4a
movl %eax, %r12d
leaq -0x50(%rbp), %rdi
callq 0x5c1a
movl %eax, %r13d
leaq -0x50(%rbp), %rdi
callq 0x5c16
cmpl $0x2, %r12d
jge 0x76b3
movl %eax, %ebx
movl $0x20, %edi
callq 0x3250
movq %rax, %r15
movq %rax, %rdi
movl %r13d, %esi
movl %ebx, %edx
movl %r14d, %ecx
movl %r12d, %r8d
callq 0x4e6a
movq %rsp, %r12
movl 0x4cb5(%rip), %eax      # 0xc2b0
incl %eax
movq %rsp, %rbx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rbx
movq %rbx, %rsp
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x7264
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl -0x2c(%rbp), %ecx
callq 0x7361
movq -0xa8(%rbp), %rsi
movq %r15, %rdi
movl %r14d, %edx
callq 0x56e4
movq %r15, %rdi
callq 0x4ed4
movl $0x20, %esi
movq %r15, %rdi
callq 0x3260
movq %r12, %rsp
leaq -0x50(%rbp), %rdi
callq 0x4ed4
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x7678
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x3260
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x7690
movq -0x80(%rbp), %rsi
subq %rdi, %rsi
callq 0x3260
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xa, %edi
callq 0x3360
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3310
callq 0x3c2c
movq %rax, %rbx
movl $0x20, %esi
movq %r15, %rdi
callq 0x3260
jmp 0x76e7
jmp 0x76e4
jmp 0x76e4
jmp 0x76e4
jmp 0x76e4
jmp 0x76e4
jmp 0x76da
jmp 0x76da
jmp 0x76da
movq %rax, %rbx
jmp 0x76f0
movq %rax, %rbx
jmp 0x7705
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x4ed4
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x7705
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x3260
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x771d
movq -0x80(%rbp), %rsi
subq %rdi, %rsi
callq 0x3260
movq %rbx, %rdi
callq 0x34b0
nop