// File: /workspace/github_cpp_makefile_star2_O2/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)
andq $0x0, 0x10(%rdi)
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
movq 0x4f34(%rip), %rax      # 0xbd30
leaq -0x100(%rbp), %rcx
movq %rax, 0x20(%rcx)
movups 0x4f12(%rip), %xmm0     # 0xbd20
movaps %xmm0, 0x10(%rcx)
movups 0x4ef7(%rip), %xmm0     # 0xbd10
movaps %xmm0, (%rcx)
leaq -0x50(%rbp), %rsi
movq %rcx, (%rsi)
callq 0x70cc
movq 0x4f19(%rip), %rax      # 0xbd48
leaq -0xd0(%rbp), %rcx
movq %rax, 0x10(%rcx)
movups 0x4ef7(%rip), %xmm0     # 0xbd38
movaps %xmm0, (%rcx)
leaq -0x50(%rbp), %rsi
movq %rcx, (%rsi)
leaq -0x70(%rbp), %rdi
callq 0x7248
movq 0x4f05(%rip), %rax      # 0xbd60
leaq -0xb0(%rbp), %rcx
movq %rax, 0x10(%rcx)
movups 0x4ee3(%rip), %xmm0     # 0xbd50
movaps %xmm0, (%rcx)
leaq -0x50(%rbp), %rsi
movq %rcx, (%rsi)
leaq -0x70(%rbp), %rdi
callq 0x7248
leaq 0x1d31(%rip), %rdi      # 0x8bb8
leaq 0x1d30(%rip), %rsi      # 0x8bbe
leaq -0x90(%rbp), %r8
leaq -0x70(%rbp), %r9
movl %r14d, %edx
movq %rbx, %rcx
callq 0x4487
testl %eax, %eax
je 0x6fe6
cmpl $0x0, -0xf8(%rbp)
je 0x6ede
movq -0xe8(%rbp), %rdi
callq 0x3440
testl %eax, %eax
js 0x6ecd
cmpl 0x53ed(%rip), %eax      # 0xc2b8
jle 0x6ee0
leaq 0x1d2a(%rip), %rdi      # 0x8bfe
callq 0x3430
jmp 0x700c
xorl %eax, %eax
movl %eax, -0x2c(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x47de
movq -0xc8(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x499a
leaq -0x50(%rbp), %rdi
callq 0x52b4
movl %eax, %r14d
leaq -0x50(%rbp), %rdi
callq 0x48e8
movl %eax, %r12d
leaq -0x50(%rbp), %rdi
callq 0x5a06
movl %eax, %r13d
leaq -0x50(%rbp), %rdi
callq 0x5a02
cmpl $0x2, %r12d
jge 0x6ff0
movl %eax, %ebx
pushq $0x20
popq %rdi
callq 0x3280
movq %rax, %r15
movq %rax, %rdi
movl %r13d, %esi
movl %ebx, %edx
movl %r14d, %ecx
movl %r12d, %r8d
callq 0x4810
movq %rsp, %r12
movl 0x535c(%rip), %eax      # 0xc2b8
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 0x6bdc
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl -0x2c(%rbp), %ecx
callq 0x6cdf
movq -0xa8(%rbp), %rsi
movq %r15, %rdi
movl %r14d, %edx
callq 0x52b8
movq %r15, %rdi
callq 0x487a
pushq $0x20
popq %rsi
movq %r15, %rdi
callq 0x3290
movq %r12, %rsp
leaq -0x50(%rbp), %rdi
callq 0x487a
leaq -0x70(%rbp), %rdi
callq 0x7098
leaq -0x90(%rbp), %rdi
callq 0x7064
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0xa
popq %rdi
callq 0x33b0
jmp 0x700c
movq 0x4fc9(%rip), %rax      # 0xbfc0
movq (%rax), %rcx
leaq 0x1bd1(%rip), %rdi      # 0x8bd2
pushq $0x2b
popq %rsi
pushq $0x1
popq %rdx
callq 0x3520
pushq $-0x1
popq %rdi
callq 0x3340
movq %rax, %rbx
pushq $0x20
popq %rsi
movq %r15, %rdi
callq 0x3290
jmp 0x703e
jmp 0x703b
jmp 0x703b
jmp 0x703b
jmp 0x703b
jmp 0x703b
jmp 0x7036
jmp 0x7036
jmp 0x7036
jmp 0x7036
movq %rax, %rbx
jmp 0x7047
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x487a
leaq -0x70(%rbp), %rdi
callq 0x7098
leaq -0x90(%rbp), %rdi
callq 0x7064
movq %rbx, %rdi
callq 0x34c0