#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
unsigned char UCHAR_MAX;
int USHRT_MAX;
double _p3dKittlerTresholding_J(int, double *, int);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ p3dGetElapsedTime_min();
int /*<<< orphan*/ p3dGetElapsedTime_sec();
int /*<<< orphan*/ p3dResetStartTime();
int p3dKittlerThresholding_16(unsigned short *in_im, unsigned char *out_im,
                              const int dimx, const int dimy, const int dimz,
                              unsigned short *thresh,
                              int (*wr_log)(const char *, ...),
                              int (*wr_progress)(const int, ...)) {
  double *prob;
  double *F;
  int i, ct;
  int tbest = 0;
  if (wr_log != NULL) {
    p3dResetStartTime();
    wr_log("Pore3D - Thresholding image according to Kittler and Illingworth "
           "method...");
  }
  if (((prob = (double *)calloc((USHRT_MAX + 1), sizeof(double))) == NULL)) {
    goto MEM_ERROR;
  };
  if (((F = (double *)calloc((USHRT_MAX + 1), sizeof(double))) == NULL)) {
    goto MEM_ERROR;
  };
  for (ct = 0; ct < (dimx * dimy * dimz); ct++)
    prob[in_im[ct]] = prob[in_im[ct]] + 1.0;
  for (i = 1; i <= USHRT_MAX; i++) {
    F[i] = _p3dKittlerTresholding_J(i, prob, USHRT_MAX);
    if (F[i] < F[tbest])
      tbest = i;
  }
  *thresh = (unsigned short)tbest;
  for (ct = 0; ct < (dimx * dimy * dimz); ct++)
    out_im[ct] = (in_im[ct] > (*thresh)) ? UCHAR_MAX : 0;
  if (wr_log != NULL) {
    wr_log("\tDetermined threshold: %d.", *thresh);
    wr_log("Pore3D - Image thresholded successfully in %dm%0.3fs.",
           p3dGetElapsedTime_min(), p3dGetElapsedTime_sec());
  }
  if (prob != NULL)
    free(prob);
  if (F != NULL)
    free(F);
  return 2;
MEM_ERROR:
  if (wr_log != NULL) {
    wr_log("Pore3D - Not enough (contiguous) memory. Program will exit.");
  }
  if (prob != NULL)
    free(prob);
  if (F != NULL)
    free(F);
  return 0;
}


x86 assembly (clang, -O0):
<p3dKittlerThresholding_16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x4c(%rbp)
cmpq   $0x0,0x10(%rbp)
je     11ab <p3dKittlerThresholding_16+0x4b>
mov    $0x0,%al
call   1080 <p3dResetStartTime@plt>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   *0x10(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11d4 <p3dKittlerThresholding_16+0x74>
jmp    13d3 <p3dKittlerThresholding_16+0x273>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    11fd <p3dKittlerThresholding_16+0x9d>
jmp    13d3 <p3dKittlerThresholding_16+0x273>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    -0x1c(%rbp),%ecx
imul   -0x20(%rbp),%ecx
imul   -0x24(%rbp),%ecx
cmp    %ecx,%eax
jge    125a <p3dKittlerThresholding_16+0xfa>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x48(%rbp),%rdx
movzwl (%rcx,%rdx,2),%ecx
movsd  0xdce(%rip),%xmm0
addsd  (%rax,%rcx,8),%xmm0
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x48(%rbp),%rdx
movzwl (%rcx,%rdx,2),%ecx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1204 <p3dKittlerThresholding_16+0xa4>
movl   $0x1,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%eax
jg     12d2 <p3dKittlerThresholding_16+0x172>
mov    -0x44(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
call   1030 <_p3dKittlerTresholding_J@plt>
mov    -0x40(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x40(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
mov    -0x40(%rbp),%rax
movslq -0x4c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
ucomisd %xmm1,%xmm0
jbe    12bf <p3dKittlerThresholding_16+0x15f>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    12c4 <p3dKittlerThresholding_16+0x164>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1261 <p3dKittlerThresholding_16+0x101>
mov    -0x4c(%rbp),%eax
mov    %ax,%cx
mov    -0x30(%rbp),%rax
mov    %cx,(%rax)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    -0x1c(%rbp),%ecx
imul   -0x20(%rbp),%ecx
imul   -0x24(%rbp),%ecx
cmp    %ecx,%eax
jge    1351 <p3dKittlerThresholding_16+0x1f1>
mov    -0x10(%rbp),%rax
movslq -0x48(%rbp),%rcx
movzwl (%rax,%rcx,2),%eax
mov    -0x30(%rbp),%rcx
movzwl (%rcx),%ecx
cmp    %ecx,%eax
jle    1329 <p3dKittlerThresholding_16+0x1c9>
mov    0x2cd2(%rip),%rax
movzbl (%rax),%eax
mov    %eax,-0x50(%rbp)
jmp    1333 <p3dKittlerThresholding_16+0x1d3>
xor    %eax,%eax
mov    %eax,-0x50(%rbp)
jmp    1333 <p3dKittlerThresholding_16+0x1d3>
mov    -0x50(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x48(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    12e6 <p3dKittlerThresholding_16+0x186>
cmpq   $0x0,0x10(%rbp)
je     139f <p3dKittlerThresholding_16+0x23f>
mov    0x10(%rbp),%rcx
mov    -0x30(%rbp),%rax
movzwl (%rax),%esi
lea    0xce5(%rip),%rdi
mov    $0x0,%al
call   *%rcx
mov    0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   1070 <p3dGetElapsedTime_min@plt>
mov    %eax,-0x5c(%rbp)
mov    $0x0,%al
call   1050 <p3dGetElapsedTime_sec@plt>
mov    -0x5c(%rbp),%esi
mov    -0x58(%rbp),%rcx
mov    %eax,%edx
lea    0xcd3(%rip),%rdi
mov    $0x0,%al
call   *%rcx
cmpq   $0x0,-0x38(%rbp)
je     13b3 <p3dKittlerThresholding_16+0x253>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x40(%rbp)
je     13c7 <p3dKittlerThresholding_16+0x267>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
movl   $0x2,-0x4(%rbp)
jmp    1419 <p3dKittlerThresholding_16+0x2b9>
cmpq   $0x0,0x10(%rbp)
je     13ea <p3dKittlerThresholding_16+0x28a>
lea    0xcbf(%rip),%rdi
mov    $0x0,%al
call   *0x10(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     13fe <p3dKittlerThresholding_16+0x29e>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x40(%rbp)
je     1412 <p3dKittlerThresholding_16+0x2b2>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<p3dKittlerThresholding_16>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,0x18(%rsp)
mov    %r8d,0x8(%rsp)
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,0x10(%rsp)
mov    %rdi,%r12
mov    0x60(%rsp),%rbx
test   %rbx,%rbx
je     11a1 <p3dKittlerThresholding_16+0x41>
xor    %eax,%eax
call   1080 <p3dResetStartTime@plt>
lea    0xe83(%rip),%rdi
xor    %eax,%eax
call   *%rbx
mov    0x2e38(%rip),%r14
mov    (%r14),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     120f <p3dKittlerThresholding_16+0xaf>
mov    (%r14),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     120f <p3dKittlerThresholding_16+0xaf>
mov    %rax,%r13
mov    %ebp,%edi
imul   %r15d,%edi
imul   0x8(%rsp),%edi
mov    %edi,%r8d
test   %edi,%edi
mov    0x18(%rsp),%r11
mov    0x10(%rsp),%rbp
jle    12db <p3dKittlerThresholding_16+0x17b>
lea    -0x1(%r8),%rcx
mov    %r8d,%eax
and    $0x3,%eax
cmp    $0x3,%rcx
jae    1234 <p3dKittlerThresholding_16+0xd4>
xor    %ecx,%ecx
jmp    12a8 <p3dKittlerThresholding_16+0x148>
mov    0x60(%rsp),%rcx
test   %rcx,%rcx
je     1224 <p3dKittlerThresholding_16+0xc4>
lea    0xe9c(%rip),%rdi
xor    %eax,%eax
call   *%rcx
xor    %ebp,%ebp
test   %rbx,%rbx
jne    1507 <p3dKittlerThresholding_16+0x3a7>
jmp    150f <p3dKittlerThresholding_16+0x3af>
mov    %r8d,%edx
and    $0xfffffffc,%edx
xor    %ecx,%ecx
movsd  0xdbc(%rip),%xmm0
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzwl (%r12,%rcx,2),%esi
movsd  (%rbx,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rsi,8)
movzwl 0x2(%r12,%rcx,2),%esi
movsd  (%rbx,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rsi,8)
movzwl 0x4(%r12,%rcx,2),%esi
movsd  (%rbx,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rsi,8)
movzwl 0x6(%r12,%rcx,2),%esi
movsd  (%rbx,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rsi,8)
add    $0x4,%rcx
cmp    %rcx,%rdx
jne    1250 <p3dKittlerThresholding_16+0xf0>
test   %rax,%rax
je     12db <p3dKittlerThresholding_16+0x17b>
lea    (%r12,%rcx,2),%rcx
xor    %edx,%edx
movsd  0xd45(%rip),%xmm0
nopl   0x0(%rax,%rax,1)
movzwl (%rcx,%rdx,2),%esi
movsd  (%rbx,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rsi,8)
add    $0x1,%rdx
cmp    %rdx,%rax
jne    12c0 <p3dKittlerThresholding_16+0x160>
mov    (%r14),%edx
xor    %r15d,%r15d
test   %edx,%edx
jle    1350 <p3dKittlerThresholding_16+0x1f0>
mov    %edi,0x24(%rsp)
mov    %r8,0x8(%rsp)
mov    $0x1,%ebp
jmp    1312 <p3dKittlerThresholding_16+0x1b2>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%rbp),%rcx
movslq (%r14),%rdx
mov    %eax,%r15d
cmp    %rdx,%rbp
mov    %rcx,%rbp
jge    133a <p3dKittlerThresholding_16+0x1da>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <_p3dKittlerTresholding_J@plt>
movsd  %xmm0,0x0(%r13,%rbp,8)
movslq %r15d,%rax
movsd  0x0(%r13,%rax,8),%xmm1
ucomisd %xmm0,%xmm1
mov    %ebp,%eax
ja     1300 <p3dKittlerThresholding_16+0x1a0>
mov    %r15d,%eax
jmp    1300 <p3dKittlerThresholding_16+0x1a0>
mov    %eax,%r15d
mov    0x18(%rsp),%r11
mov    0x10(%rsp),%rbp
mov    0x8(%rsp),%r8
mov    0x24(%rsp),%edi
mov    %r15w,(%r11)
test   %edi,%edi
mov    0x60(%rsp),%r14
jle    14c2 <p3dKittlerThresholding_16+0x362>
xor    %ecx,%ecx
cmp    %r15w,(%r12)
mov    0x2c81(%rip),%rax
movzbl (%rax),%edx
cmovbe %ecx,%edx
mov    %dl,0x0(%rbp)
cmp    $0x1,%edi
je     14c2 <p3dKittlerThresholding_16+0x362>
lea    -0x1(%r8),%r9
mov    $0x1,%ecx
cmp    $0x10,%r9
jb     1497 <p3dKittlerThresholding_16+0x337>
lea    0x1(%rbp),%rdx
lea    (%r8,%rbp,1),%rdi
lea    0x2(%r11),%r10
mov    %r8,%rsi
lea    0x2(%r12),%r8
cmp    %r10,%rdx
setb   %r10b
cmp    %r11,%rdi
seta   %r11b
mov    %rsi,0x8(%rsp)
lea    (%r12,%rsi,2),%rsi
cmp    %rsi,%rdx
setb   %dl
cmp    %r8,%rdi
seta   %r15b
cmp    %rax,%rbp
setb   %sil
cmp    %rax,%rdi
seta   %r8b
test   %r11b,%r10b
jne    1488 <p3dKittlerThresholding_16+0x328>
and    %r15b,%dl
jne    1488 <p3dKittlerThresholding_16+0x328>
and    %r8b,%sil
mov    0x18(%rsp),%r11
mov    0x10(%rsp),%rbp
mov    0x8(%rsp),%r8
jne    1497 <p3dKittlerThresholding_16+0x337>
mov    %r9,%rsi
and    $0xfffffffffffffff0,%rsi
lea    0x1(%rsi),%rcx
movzwl (%r11),%edx
movd   %edx,%xmm0
pshuflw $0x0,%xmm0,%xmm0
pshufd $0x0,%xmm0,%xmm0
movzbl (%rax),%edx
movd   %edx,%xmm1
punpcklbw %xmm1,%xmm1
pshuflw $0x0,%xmm1,%xmm1
xor    %edi,%edi
movdqa 0xbd5(%rip),%xmm2
pxor   %xmm2,%xmm0
nop
movdqu 0x2(%r12,%rdi,2),%xmm3
movdqu 0x12(%r12,%rdi,2),%xmm4
pxor   %xmm2,%xmm3
pcmpgtw %xmm0,%xmm3
packsswb %xmm3,%xmm3
pxor   %xmm2,%xmm4
pcmpgtw %xmm0,%xmm4
packsswb %xmm4,%xmm4
pand   %xmm1,%xmm3
pand   %xmm1,%xmm4
punpcklqdq %xmm4,%xmm3
movdqu %xmm3,0x1(%rbp,%rdi,1)
add    $0x10,%rdi
cmp    %rdi,%rsi
jne    1440 <p3dKittlerThresholding_16+0x2e0>
cmp    %rsi,%r9
jne    1497 <p3dKittlerThresholding_16+0x337>
jmp    14c2 <p3dKittlerThresholding_16+0x362>
mov    0x18(%rsp),%r11
mov    0x10(%rsp),%rbp
mov    0x8(%rsp),%r8
mov    %rcx,%rdx
not    %rdx
test   $0x1,%r8b
jne    14bd <p3dKittlerThresholding_16+0x35d>
movzwl (%r12,%rcx,2),%esi
xor    %edi,%edi
cmp    (%r11),%si
movzbl (%rax),%esi
cmovbe %edi,%esi
mov    %sil,0x0(%rbp,%rcx,1)
add    $0x1,%rcx
add    %r8,%rdx
jne    152d <p3dKittlerThresholding_16+0x3cd>
test   %r14,%r14
je     14f7 <p3dKittlerThresholding_16+0x397>
movzwl (%r11),%esi
lea    0xb99(%rip),%rdi
xor    %eax,%eax
call   *%r14
xor    %eax,%eax
call   1070 <p3dGetElapsedTime_min@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1050 <p3dGetElapsedTime_sec@plt>
lea    0xb98(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   *%r14
mov    %rbx,%rdi
call   1040 <free@plt>
mov    $0x2,%ebp
mov    %r13,%rbx
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %dl,0x1(%rbp,%rcx,1)
add    $0x2,%rcx
cmp    %rcx,%r8
je     14c2 <p3dKittlerThresholding_16+0x362>
movzwl (%r12,%rcx,2),%esi
xor    %edx,%edx
cmp    (%r11),%si
ja     1540 <p3dKittlerThresholding_16+0x3e0>
xor    %esi,%esi
jmp    1543 <p3dKittlerThresholding_16+0x3e3>
xchg   %ax,%ax
movzbl (%rax),%esi
mov    %sil,0x0(%rbp,%rcx,1)
movzwl 0x2(%r12,%rcx,2),%esi
cmp    (%r11),%si
jbe    1520 <p3dKittlerThresholding_16+0x3c0>
movzbl (%rax),%edx
jmp    1520 <p3dKittlerThresholding_16+0x3c0>

LLVM IR (-O0):
@.str = [75 x i8] c"Pore3D - Thresholding image according to Kittler and Illingworth method...\00"
@USHRT_MAX = global i32 0
@UCHAR_MAX = global i8 0
@.str.1 = [27 x i8] c"\09Determined threshold: %d.\00"
@.str.2 = [54 x i8] c"Pore3D - Image thresholded successfully in %dm%0.3fs.\00"
@.str.3 = [60 x i8] c"Pore3D - Not enough (contiguous) memory. Program will exit.\00"
define i32 @p3dKittlerThresholding_16(i16* %0, i8* %1, i32 %2, i32 %3, i32 %4, i16* %5, i32 (i8*, ...)* %6, i32 (i32, ...)* %7) {
%9 = alloca i32
%10 = alloca i16*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i16*
%16 = alloca i32 (i8*, ...)*
%17 = alloca i32 (i32, ...)*
%18 = alloca double*
%19 = alloca double*
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
store i16* %0, i16** %10
store i8* %1, i8** %11
store i32 %2, i32* %12
store i32 %3, i32* %13
store i32 %4, i32* %14
store i16* %5, i16** %15
store i32 (i8*, ...)* %6, i32 (i8*, ...)** %16
store i32 (i32, ...)* %7, i32 (i32, ...)** %17
store i32 0, i32* %22
%23 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%24 = icmp ne i32 (i8*, ...)* %23, null
br i1 %24, label %25, label %29
25: ; preds = %8
%26 = call i32 (...) @p3dResetStartTime()
%27 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%28 = call i32 (i8*, ...) %27(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %25, %8
%30 = load i32, i32* @USHRT_MAX
%31 = add nsw i32 %30, 1
%32 = call i64 @calloc(i32 %31, i32 8)
%33 = inttoptr i64 %32 to double*
store double* %33, double** %18
%34 = icmp eq double* %33, null
br i1 %34, label %35, label %36
35: ; preds = %29
br label %168
36: ; preds = %29
%37 = load i32, i32* @USHRT_MAX
%38 = add nsw i32 %37, 1
%39 = call i64 @calloc(i32 %38, i32 8)
%40 = inttoptr i64 %39 to double*
store double* %40, double** %19
%41 = icmp eq double* %40, null
br i1 %41, label %42, label %43
42: ; preds = %36
br label %168
43: ; preds = %36
store i32 0, i32* %21
br label %44
44: ; preds = %71, %43
%45 = load i32, i32* %21
%46 = load i32, i32* %12
%47 = load i32, i32* %13
%48 = mul nsw i32 %46, %47
%49 = load i32, i32* %14
%50 = mul nsw i32 %48, %49
%51 = icmp slt i32 %45, %50
br i1 %51, label %52, label %74
52: ; preds = %44
%53 = load double*, double** %18
%54 = load i16*, i16** %10
%55 = load i32, i32* %21
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i16, i16* %54, i64 %56
%58 = load i16, i16* %57
%59 = zext i16 %58 to i64
%60 = getelementptr inbounds double, double* %53, i64 %59
%61 = load double, double* %60
%62 = fadd double %61, 1.000000e+00
%63 = load double*, double** %18
%64 = load i16*, i16** %10
%65 = load i32, i32* %21
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i16, i16* %64, i64 %66
%68 = load i16, i16* %67
%69 = zext i16 %68 to i64
%70 = getelementptr inbounds double, double* %63, i64 %69
store double %62, double* %70
br label %71
71: ; preds = %52
%72 = load i32, i32* %21
%73 = add nsw i32 %72, 1
store i32 %73, i32* %21
br label %44
74: ; preds = %44
store i32 1, i32* %20
br label %75
75: ; preds = %102, %74
%76 = load i32, i32* %20
%77 = load i32, i32* @USHRT_MAX
%78 = icmp sle i32 %76, %77
br i1 %78, label %79, label %105
79: ; preds = %75
%80 = load i32, i32* %20
%81 = load double*, double** %18
%82 = load i32, i32* @USHRT_MAX
%83 = call double @_p3dKittlerTresholding_J(i32 %80, double* %81, i32 %82)
%84 = load double*, double** %19
%85 = load i32, i32* %20
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds double, double* %84, i64 %86
store double %83, double* %87
%88 = load double*, double** %19
%89 = load i32, i32* %20
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds double, double* %88, i64 %90
%92 = load double, double* %91
%93 = load double*, double** %19
%94 = load i32, i32* %22
%95 = sext i32 %94 to i64
%96 = getelementptr inbounds double, double* %93, i64 %95
%97 = load double, double* %96
%98 = fcmp olt double %92, %97
br i1 %98, label %99, label %101
99: ; preds = %79
%100 = load i32, i32* %20
store i32 %100, i32* %22
br label %101
101: ; preds = %99, %79
br label %102
102: ; preds = %101
%103 = load i32, i32* %20
%104 = add nsw i32 %103, 1
store i32 %104, i32* %20
br label %75
105: ; preds = %75
%106 = load i32, i32* %22
%107 = trunc i32 %106 to i16
%108 = load i16*, i16** %15
store i16 %107, i16* %108
store i32 0, i32* %21
br label %109
109: ; preds = %139, %105
%110 = load i32, i32* %21
%111 = load i32, i32* %12
%112 = load i32, i32* %13
%113 = mul nsw i32 %111, %112
%114 = load i32, i32* %14
%115 = mul nsw i32 %113, %114
%116 = icmp slt i32 %110, %115
br i1 %116, label %117, label %142
117: ; preds = %109
%118 = load i16*, i16** %10
%119 = load i32, i32* %21
%120 = sext i32 %119 to i64
%121 = getelementptr inbounds i16, i16* %118, i64 %120
%122 = load i16, i16* %121
%123 = zext i16 %122 to i32
%124 = load i16*, i16** %15
%125 = load i16, i16* %124
%126 = zext i16 %125 to i32
%127 = icmp sgt i32 %123, %126
br i1 %127, label %128, label %131
128: ; preds = %117
%129 = load i8, i8* @UCHAR_MAX
%130 = zext i8 %129 to i32
br label %132
131: ; preds = %117
br label %132
132: ; preds = %131, %128
%133 = phi i32 [ %130, %128 ], [ 0, %131 ]
%134 = trunc i32 %133 to i8
%135 = load i8*, i8** %11
%136 = load i32, i32* %21
%137 = sext i32 %136 to i64
%138 = getelementptr inbounds i8, i8* %135, i64 %137
store i8 %134, i8* %138
br label %139
139: ; preds = %132
%140 = load i32, i32* %21
%141 = add nsw i32 %140, 1
store i32 %141, i32* %21
br label %109
142: ; preds = %109
%143 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%144 = icmp ne i32 (i8*, ...)* %143, null
br i1 %144, label %145, label %155
145: ; preds = %142
%146 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%147 = load i16*, i16** %15
%148 = load i16, i16* %147
%149 = zext i16 %148 to i32
%150 = call i32 (i8*, ...) %146(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %149)
%151 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%152 = call i32 (...) @p3dGetElapsedTime_min()
%153 = call i32 (...) @p3dGetElapsedTime_sec()
%154 = call i32 (i8*, ...) %151(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %152, i32 %153)
br label %155
155: ; preds = %145, %142
%156 = load double*, double** %18
%157 = icmp ne double* %156, null
br i1 %157, label %158, label %161
158: ; preds = %155
%159 = load double*, double** %18
%160 = call i32 @free(double* %159)
br label %161
161: ; preds = %158, %155
%162 = load double*, double** %19
%163 = icmp ne double* %162, null
br i1 %163, label %164, label %167
164: ; preds = %161
%165 = load double*, double** %19
%166 = call i32 @free(double* %165)
br label %167
167: ; preds = %164, %161
store i32 2, i32* %9
br label %187
168: ; preds = %42, %35
%169 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%170 = icmp ne i32 (i8*, ...)* %169, null
br i1 %170, label %171, label %174
171: ; preds = %168
%172 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%173 = call i32 (i8*, ...) %172(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0))
br label %174
174: ; preds = %171, %168
%175 = load double*, double** %18
%176 = icmp ne double* %175, null
br i1 %176, label %177, label %180
177: ; preds = %174
%178 = load double*, double** %18
%179 = call i32 @free(double* %178)
br label %180
180: ; preds = %177, %174
%181 = load double*, double** %19
%182 = icmp ne double* %181, null
br i1 %182, label %183, label %186
183: ; preds = %180
%184 = load double*, double** %19
%185 = call i32 @free(double* %184)
br label %186
186: ; preds = %183, %180
store i32 0, i32* %9
br label %187
187: ; preds = %186, %167
%188 = load i32, i32* %9
ret i32 %188
}

LLVM IR (-O2):
@.str = [75 x i8] c"Pore3D - Thresholding image according to Kittler and Illingworth method...\00"
@USHRT_MAX = global i32 0
@UCHAR_MAX = global i8 0
@.str.1 = [27 x i8] c"\09Determined threshold: %d.\00"
@.str.2 = [54 x i8] c"Pore3D - Image thresholded successfully in %dm%0.3fs.\00"
@.str.3 = [60 x i8] c"Pore3D - Not enough (contiguous) memory. Program will exit.\00"
define i32 @p3dKittlerThresholding_16(i16* nocapture %0, i8* nocapture writeonly %1, i32 %2, i32 %3, i32 %4, i16* nocapture %5, i32 (i8*, ...)* %6, i32 (i32, ...)* nocapture readnone %7) {
%9 = bitcast i16* %5 to i8*
%10 = icmp eq i32 (i8*, ...)* %6, null
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = tail call i32 (...) @p3dResetStartTime()
%13 = tail call i32 (i8*, ...) %6(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %11, %8
%15 = load i32, i32* @USHRT_MAX
%16 = add nsw i32 %15, 1
%17 = tail call i64 @calloc(i32 %16, i32 8)
%18 = inttoptr i64 %17 to double*
%19 = icmp eq i64 %17, 0
br i1 %19, label %219, label %20
20: ; preds = %14
%21 = load i32, i32* @USHRT_MAX
%22 = add nsw i32 %21, 1
%23 = tail call i64 @calloc(i32 %22, i32 8)
%24 = inttoptr i64 %23 to double*
%25 = icmp eq i64 %23, 0
br i1 %25, label %219, label %26
26: ; preds = %20
%27 = mul nsw i32 %3, %2
%28 = mul i32 %27, %4
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %52
30: ; preds = %26
%31 = zext i32 %28 to i64
%32 = add nsw i64 %31, -1
%33 = and i64 %31, 3
%34 = icmp ult i64 %32, 3
br i1 %34, label %37, label %35
35: ; preds = %30
%36 = and i64 %31, 4294967292
br label %55
37: ; preds = %55, %30
%38 = phi i64 [ 0, %30 ], [ %85, %55 ]
%39 = icmp eq i64 %33, 0
br i1 %39, label %52, label %40
40: ; preds = %37, %40
%41 = phi i64 [ %49, %40 ], [ %38, %37 ]
%42 = phi i64 [ %50, %40 ], [ 0, %37 ]
%43 = getelementptr inbounds i16, i16* %0, i64 %41
%44 = load i16, i16* %43
%45 = zext i16 %44 to i64
%46 = getelementptr inbounds double, double* %18, i64 %45
%47 = load double, double* %46
%48 = fadd double %47, 1.000000e+00
store double %48, double* %46
%49 = add nuw nsw i64 %41, 1
%50 = add i64 %42, 1
%51 = icmp eq i64 %50, %33
br i1 %51, label %52, label %40
52: ; preds = %37, %40, %26
%53 = load i32, i32* @USHRT_MAX
%54 = icmp slt i32 %53, 1
br i1 %54, label %106, label %88
55: ; preds = %55, %35
%56 = phi i64 [ 0, %35 ], [ %85, %55 ]
%57 = phi i64 [ 0, %35 ], [ %86, %55 ]
%58 = getelementptr inbounds i16, i16* %0, i64 %56
%59 = load i16, i16* %58
%60 = zext i16 %59 to i64
%61 = getelementptr inbounds double, double* %18, i64 %60
%62 = load double, double* %61
%63 = fadd double %62, 1.000000e+00
store double %63, double* %61
%64 = or i64 %56, 1
%65 = getelementptr inbounds i16, i16* %0, i64 %64
%66 = load i16, i16* %65
%67 = zext i16 %66 to i64
%68 = getelementptr inbounds double, double* %18, i64 %67
%69 = load double, double* %68
%70 = fadd double %69, 1.000000e+00
store double %70, double* %68
%71 = or i64 %56, 2
%72 = getelementptr inbounds i16, i16* %0, i64 %71
%73 = load i16, i16* %72
%74 = zext i16 %73 to i64
%75 = getelementptr inbounds double, double* %18, i64 %74
%76 = load double, double* %75
%77 = fadd double %76, 1.000000e+00
store double %77, double* %75
%78 = or i64 %56, 3
%79 = getelementptr inbounds i16, i16* %0, i64 %78
%80 = load i16, i16* %79
%81 = zext i16 %80 to i64
%82 = getelementptr inbounds double, double* %18, i64 %81
%83 = load double, double* %82
%84 = fadd double %83, 1.000000e+00
store double %84, double* %82
%85 = add nuw nsw i64 %56, 4
%86 = add i64 %57, 4
%87 = icmp eq i64 %86, %36
br i1 %87, label %37, label %55
88: ; preds = %52, %88
%89 = phi i64 [ %100, %88 ], [ 1, %52 ]
%90 = phi i32 [ %101, %88 ], [ %53, %52 ]
%91 = phi i32 [ %99, %88 ], [ 0, %52 ]
%92 = trunc i64 %89 to i32
%93 = tail call double @_p3dKittlerTresholding_J(i32 %92, double* %18, i32 %90)
%94 = getelementptr inbounds double, double* %24, i64 %89
store double %93, double* %94
%95 = sext i32 %91 to i64
%96 = getelementptr inbounds double, double* %24, i64 %95
%97 = load double, double* %96
%98 = fcmp olt double %93, %97
%99 = select i1 %98, i32 %92, i32 %91
%100 = add nuw nsw i64 %89, 1
%101 = load i32, i32* @USHRT_MAX
%102 = sext i32 %101 to i64
%103 = icmp slt i64 %89, %102
br i1 %103, label %88, label %104
104: ; preds = %88
%105 = trunc i32 %99 to i16
br label %106
106: ; preds = %104, %52
%107 = phi i16 [ 0, %52 ], [ %105, %104 ]
store i16 %107, i16* %5
br i1 %29, label %108, label %209
108: ; preds = %106
%109 = zext i32 %28 to i64
%110 = load i16, i16* %0
%111 = icmp ugt i16 %110, %107
%112 = load i8, i8* @UCHAR_MAX
%113 = select i1 %111, i8 %112, i8 0
store i8 %113, i8* %1
%114 = icmp eq i32 %28, 1
br i1 %114, label %209, label %115
115: ; preds = %108
%116 = add nsw i64 %109, -1
%117 = icmp ult i64 %116, 16
br i1 %117, label %172, label %118
118: ; preds = %115
%119 = getelementptr i8, i8* %1, i64 1
%120 = getelementptr i8, i8* %1, i64 %109
%121 = getelementptr i16, i16* %5, i64 1
%122 = bitcast i16* %121 to i8*
%123 = getelementptr i16, i16* %0, i64 1
%124 = bitcast i16* %123 to i8*
%125 = getelementptr i16, i16* %0, i64 %109
%126 = bitcast i16* %125 to i8*
%127 = icmp ult i8* %119, %122
%128 = icmp ugt i8* %120, %9
%129 = and i1 %127, %128
%130 = icmp ult i8* %119, %126
%131 = icmp ugt i8* %120, %124
%132 = and i1 %130, %131
%133 = or i1 %129, %132
%134 = icmp ult i8* %1, @UCHAR_MAX
%135 = icmp ugt i8* %120, @UCHAR_MAX
%136 = and i1 %134, %135
%137 = or i1 %133, %136
br i1 %137, label %172, label %138
138: ; preds = %118
%139 = and i64 %116, -16
%140 = or i64 %139, 1
%141 = load i16, i16* %5
%142 = insertelement <8 x i16> poison, i16 %141, i64 0
%143 = shufflevector <8 x i16> %142, <8 x i16> poison, <8 x i32> zeroinitializer
%144 = insertelement <8 x i16> poison, i16 %141, i64 0
%145 = shufflevector <8 x i16> %144, <8 x i16> poison, <8 x i32> zeroinitializer
%146 = load i8, i8* @UCHAR_MAX
%147 = insertelement <8 x i8> poison, i8 %146, i64 0
%148 = shufflevector <8 x i8> %147, <8 x i8> poison, <8 x i32> zeroinitializer
%149 = insertelement <8 x i8> poison, i8 %146, i64 0
%150 = shufflevector <8 x i8> %149, <8 x i8> poison, <8 x i32> zeroinitializer
br label %151
151: ; preds = %151, %138
%152 = phi i64 [ 0, %138 ], [ %168, %151 ]
%153 = or i64 %152, 1
%154 = getelementptr inbounds i16, i16* %0, i64 %153
%155 = bitcast i16* %154 to <8 x i16>*
%156 = load <8 x i16>, <8 x i16>* %155
%157 = getelementptr inbounds i16, i16* %154, i64 8
%158 = bitcast i16* %157 to <8 x i16>*
%159 = load <8 x i16>, <8 x i16>* %158
%160 = icmp ugt <8 x i16> %156, %143
%161 = icmp ugt <8 x i16> %159, %145
%162 = select <8 x i1> %160, <8 x i8> %148, <8 x i8> zeroinitializer
%163 = select <8 x i1> %161, <8 x i8> %150, <8 x i8> zeroinitializer
%164 = getelementptr inbounds i8, i8* %1, i64 %153
%165 = bitcast i8* %164 to <8 x i8>*
store <8 x i8> %162, <8 x i8>* %165
%166 = getelementptr inbounds i8, i8* %164, i64 8
%167 = bitcast i8* %166 to <8 x i8>*
store <8 x i8> %163, <8 x i8>* %167
%168 = add nuw i64 %152, 16
%169 = icmp eq i64 %168, %139
br i1 %169, label %170, label %151
170: ; preds = %151
%171 = icmp eq i64 %116, %139
br i1 %171, label %209, label %172
172: ; preds = %118, %115, %170
%173 = phi i64 [ 1, %118 ], [ 1, %115 ], [ %140, %170 ]
%174 = xor i64 %173, -1
%175 = and i64 %109, 1
%176 = icmp eq i64 %175, 0
br i1 %176, label %177, label %186
177: ; preds = %172
%178 = load i16, i16* %5
%179 = getelementptr inbounds i16, i16* %0, i64 %173
%180 = load i16, i16* %179
%181 = icmp ugt i16 %180, %178
%182 = load i8, i8* @UCHAR_MAX
%183 = select i1 %181, i8 %182, i8 0
%184 = getelementptr inbounds i8, i8* %1, i64 %173
store i8 %183, i8* %184
%185 = add nuw nsw i64 %173, 1
br label %186
186: ; preds = %177, %172
%187 = phi i64 [ %173, %172 ], [ %185, %177 ]
%188 = sub nsw i64 0, %109
%189 = icmp eq i64 %174, %188
br i1 %189, label %209, label %190
190: ; preds = %186, %190
%191 = phi i64 [ %207, %190 ], [ %187, %186 ]
%192 = load i16, i16* %5
%193 = getelementptr inbounds i16, i16* %0, i64 %191
%194 = load i16, i16* %193
%195 = icmp ugt i16 %194, %192
%196 = load i8, i8* @UCHAR_MAX
%197 = select i1 %195, i8 %196, i8 0
%198 = getelementptr inbounds i8, i8* %1, i64 %191
store i8 %197, i8* %198
%199 = add nuw nsw i64 %191, 1
%200 = load i16, i16* %5
%201 = getelementptr inbounds i16, i16* %0, i64 %199
%202 = load i16, i16* %201
%203 = icmp ugt i16 %202, %200
%204 = load i8, i8* @UCHAR_MAX
%205 = select i1 %203, i8 %204, i8 0
%206 = getelementptr inbounds i8, i8* %1, i64 %199
store i8 %205, i8* %206
%207 = add nuw nsw i64 %191, 2
%208 = icmp eq i64 %207, %109
br i1 %208, label %209, label %190
209: ; preds = %186, %190, %108, %170, %106
br i1 %10, label %217, label %210
210: ; preds = %209
%211 = load i16, i16* %5
%212 = zext i16 %211 to i32
%213 = tail call i32 (i8*, ...) %6(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %212)
%214 = tail call i32 (...) @p3dGetElapsedTime_min()
%215 = tail call i32 (...) @p3dGetElapsedTime_sec()
%216 = tail call i32 (i8*, ...) %6(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %214, i32 %215)
br label %217
217: ; preds = %209, %210
%218 = tail call i32 @free(double* %18)
br label %223
219: ; preds = %20, %14
br i1 %10, label %222, label %220
220: ; preds = %219
%221 = tail call i32 (i8*, ...) %6(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0))
br label %222
222: ; preds = %220, %219
br i1 %19, label %227, label %223
223: ; preds = %222, %217
%224 = phi double* [ %24, %217 ], [ %18, %222 ]
%225 = phi i32 [ 2, %217 ], [ 0, %222 ]
%226 = tail call i32 @free(double* %224)
br label %227
227: ; preds = %223, %222
%228 = phi i32 [ 0, %222 ], [ %225, %223 ]
ret i32 %228
}