// File: /workspace/github_cpp_makefile_star2_O1/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 0x46f3(%rip), %rax      # 0xbd28
leaq -0x100(%rbp), %rcx
movq %rax, 0x20(%rcx)
movups 0x46d1(%rip), %xmm0     # 0xbd18
movaps %xmm0, 0x10(%rcx)
movups 0x46b6(%rip), %xmm0     # 0xbd08
movaps %xmm0, (%rcx)
leaq -0x50(%rbp), %rsi
movq %rcx, (%rsi)
callq 0x78be
movq 0x46d8(%rip), %rax      # 0xbd40
leaq -0xd0(%rbp), %rcx
movq %rax, 0x10(%rcx)
movups 0x46b6(%rip), %xmm0     # 0xbd30
movaps %xmm0, (%rcx)
leaq -0x50(%rbp), %rsi
movq %rcx, (%rsi)
leaq -0x70(%rbp), %rdi
callq 0x7a2a
movq 0x46c4(%rip), %rax      # 0xbd58
leaq -0xb0(%rbp), %rcx
movq %rax, 0x10(%rcx)
movups 0x46a2(%rip), %xmm0     # 0xbd48
movaps %xmm0, (%rcx)
leaq -0x50(%rbp), %rsi
movq %rcx, (%rsi)
leaq -0x70(%rbp), %rdi
callq 0x7a2a
leaq 0x1544(%rip), %rdi      # 0x8c04
leaq 0x1543(%rip), %rsi      # 0x8c0a
leaq -0x90(%rbp), %r8
leaq -0x70(%rbp), %r9
movl %r14d, %edx
movq %rbx, %rcx
callq 0x4b23
testl %eax, %eax
je 0x783b
cmpl $0x0, -0xf8(%rbp)
je 0x7717
movq -0xe8(%rbp), %rdi
callq 0x3410
testl %eax, %eax
js 0x7706
cmpl 0x4bac(%rip), %eax      # 0xc2b0
jle 0x7719
leaq 0x153d(%rip), %rdi      # 0x8c4a
callq 0x3400
jmp 0x7845
xorl %eax, %eax
movl %eax, -0x2c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4eb2
movq -0xc8(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x5066
leaq -0x50(%rbp), %rdi
callq 0x5786
movl %eax, %r14d
leaq -0x50(%rbp), %rdi
callq 0x4fc6
movl %eax, %r12d
leaq -0x50(%rbp), %rdi
callq 0x5d06
movl %eax, %r13d
leaq -0x50(%rbp), %rdi
callq 0x5d02
cmpl $0x2, %r12d
jge 0x784f
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 0x4ee6
movq %rsp, %r12
movl 0x4b19(%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 0x7408
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl -0x2c(%rbp), %ecx
callq 0x7505
movq -0xa8(%rbp), %rsi
movq %r15, %rdi
movl %r14d, %edx
callq 0x578a
movq %r15, %rdi
callq 0x4f50
movl $0x20, %esi
movq %r15, %rdi
callq 0x3260
movq %r12, %rsp
leaq -0x50(%rbp), %rdi
callq 0x4f50
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x7814
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x3260
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x782c
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 0x3c37
movq %rax, %rbx
movl $0x20, %esi
movq %r15, %rdi
callq 0x3260
jmp 0x7880
jmp 0x787d
jmp 0x787d
jmp 0x787d
jmp 0x787d
jmp 0x787d
jmp 0x7878
jmp 0x7878
jmp 0x7878
jmp 0x7878
movq %rax, %rbx
jmp 0x7889
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x4f50
movq -0x70(%rbp), %rdi
testq %rdi, %rdi
je 0x789e
movq -0x60(%rbp), %rsi
subq %rdi, %rsi
callq 0x3260
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x78b6
movq -0x80(%rbp), %rsi
subq %rdi, %rsi
callq 0x3260
movq %rbx, %rdi
callq 0x34b0