#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 */
int UCHAR_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_8(unsigned char *in_im, unsigned char *out_im,
                             const int dimx, const int dimy, const int dimz,
                             unsigned char *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((UCHAR_MAX + 1), sizeof(double))) == NULL)) {
    goto MEM_ERROR;
  };
  if (((F = (double *)calloc((UCHAR_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 <= UCHAR_MAX; i++) {
    F[i] = _p3dKittlerTresholding_J(i, prob, UCHAR_MAX);
    if (F[i] < F[tbest])
      tbest = i;
  }
  *thresh = (unsigned char)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_8>:
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_8+0x4b>
mov    $0x0,%al
call   1080 <p3dResetStartTime@plt>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   *0x10(%rbp)
mov    0x2e3e(%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_8+0x74>
jmp    13d0 <p3dKittlerThresholding_8+0x270>
mov    0x2e15(%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_8+0x9d>
jmp    13d0 <p3dKittlerThresholding_8+0x270>
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_8+0xfa>
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x48(%rbp),%rdx
movzbl (%rcx,%rdx,1),%ecx
movsd  0xdce(%rip),%xmm0
addsd  (%rax,%rcx,8),%xmm0
mov    -0x38(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x48(%rbp),%rdx
movzbl (%rcx,%rdx,1),%ecx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1204 <p3dKittlerThresholding_8+0xa4>
movl   $0x1,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    0x2d85(%rip),%rcx
cmp    (%rcx),%eax
jg     12d2 <p3dKittlerThresholding_8+0x172>
mov    -0x44(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    0x2d6f(%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_8+0x15f>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    12c4 <p3dKittlerThresholding_8+0x164>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1261 <p3dKittlerThresholding_8+0x101>
mov    -0x4c(%rbp),%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %cl,(%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    134e <p3dKittlerThresholding_8+0x1ee>
mov    -0x10(%rbp),%rax
movslq -0x48(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    -0x30(%rbp),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
jle    1326 <p3dKittlerThresholding_8+0x1c6>
mov    0x2cd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
jmp    1330 <p3dKittlerThresholding_8+0x1d0>
xor    %eax,%eax
mov    %eax,-0x50(%rbp)
jmp    1330 <p3dKittlerThresholding_8+0x1d0>
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    12e4 <p3dKittlerThresholding_8+0x184>
cmpq   $0x0,0x10(%rbp)
je     139c <p3dKittlerThresholding_8+0x23c>
mov    0x10(%rbp),%rcx
mov    -0x30(%rbp),%rax
movzbl (%rax),%esi
lea    0xce8(%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    0xcd6(%rip),%rdi
mov    $0x0,%al
call   *%rcx
cmpq   $0x0,-0x38(%rbp)
je     13b0 <p3dKittlerThresholding_8+0x250>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x40(%rbp)
je     13c4 <p3dKittlerThresholding_8+0x264>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
movl   $0x2,-0x4(%rbp)
jmp    1416 <p3dKittlerThresholding_8+0x2b6>
cmpq   $0x0,0x10(%rbp)
je     13e7 <p3dKittlerThresholding_8+0x287>
lea    0xcc2(%rip),%rdi
mov    $0x0,%al
call   *0x10(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     13fb <p3dKittlerThresholding_8+0x29b>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x40(%rbp)
je     140f <p3dKittlerThresholding_8+0x2af>
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_8>:
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,%r14d
mov    %edx,%r15d
mov    %rsi,0x10(%rsp)
mov    %rdi,%r12
mov    0x60(%rsp),%rbx
test   %rbx,%rbx
je     11a2 <p3dKittlerThresholding_8+0x42>
xor    %eax,%eax
call   1080 <p3dResetStartTime@plt>
lea    0xe6a(%rip),%rdi
xor    %eax,%eax
call   *%rbx
mov    0x2e47(%rip),%rbp
mov    0x0(%rbp),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1214 <p3dKittlerThresholding_8+0xb4>
mov    0x0(%rbp),%edi
add    $0x1,%edi
mov    $0x8,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     1214 <p3dKittlerThresholding_8+0xb4>
mov    %rax,%r13
mov    %r14d,%edi
imul   %r15d,%edi
imul   0x8(%rsp),%edi
mov    %edi,%r9d
test   %edi,%edi
mov    0x18(%rsp),%r10
mov    %rbp,%r8
mov    0x10(%rsp),%rbp
jle    12db <p3dKittlerThresholding_8+0x17b>
lea    -0x1(%r9),%rcx
mov    %r9d,%eax
and    $0x3,%eax
cmp    $0x3,%rcx
jae    1239 <p3dKittlerThresholding_8+0xd9>
xor    %ecx,%ecx
jmp    12a8 <p3dKittlerThresholding_8+0x148>
mov    0x60(%rsp),%rcx
test   %rcx,%rcx
je     1229 <p3dKittlerThresholding_8+0xc9>
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   *%rcx
xor    %ebp,%ebp
test   %rbx,%rbx
jne    14fe <p3dKittlerThresholding_8+0x39e>
jmp    1506 <p3dKittlerThresholding_8+0x3a6>
mov    %r9d,%edx
and    $0xfffffffc,%edx
xor    %ecx,%ecx
movsd  0xdb7(%rip),%xmm0
nopl   0x0(%rax)
movzbl (%r12,%rcx,1),%esi
movsd  (%rbx,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rsi,8)
movzbl 0x1(%r12,%rcx,1),%esi
movsd  (%rbx,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rsi,8)
movzbl 0x2(%r12,%rcx,1),%esi
movsd  (%rbx,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rsi,8)
movzbl 0x3(%r12,%rcx,1),%esi
movsd  (%rbx,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rsi,8)
add    $0x4,%rcx
cmp    %rcx,%rdx
jne    1250 <p3dKittlerThresholding_8+0xf0>
test   %rax,%rax
je     12db <p3dKittlerThresholding_8+0x17b>
add    %r12,%rcx
xor    %edx,%edx
movsd  0xd46(%rip),%xmm0
nopw   0x0(%rax,%rax,1)
movzbl (%rcx,%rdx,1),%esi
movsd  (%rbx,%rsi,8),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,(%rbx,%rsi,8)
add    $0x1,%rdx
cmp    %rdx,%rax
jne    12c0 <p3dKittlerThresholding_8+0x160>
mov    (%r8),%edx
xor    %r14d,%r14d
test   %edx,%edx
mov    %r8,%r15
jle    1353 <p3dKittlerThresholding_8+0x1f3>
mov    %edi,0x24(%rsp)
mov    %r9,0x8(%rsp)
mov    $0x1,%ebp
jmp    1312 <p3dKittlerThresholding_8+0x1b2>
nopl   0x0(%rax,%rax,1)
lea    0x1(%rbp),%rcx
movslq (%r15),%rdx
mov    %eax,%r14d
cmp    %rdx,%rbp
mov    %rcx,%rbp
jge    133a <p3dKittlerThresholding_8+0x1da>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <_p3dKittlerTresholding_J@plt>
movsd  %xmm0,0x0(%r13,%rbp,8)
movslq %r14d,%rax
movsd  0x0(%r13,%rax,8),%xmm1
ucomisd %xmm0,%xmm1
mov    %ebp,%eax
ja     1300 <p3dKittlerThresholding_8+0x1a0>
mov    %r14d,%eax
jmp    1300 <p3dKittlerThresholding_8+0x1a0>
mov    %r15,%r8
mov    %eax,%r14d
mov    0x18(%rsp),%r10
mov    0x10(%rsp),%rbp
mov    0x8(%rsp),%r9
mov    0x24(%rsp),%edi
mov    %r14b,(%r10)
test   %edi,%edi
jle    14b4 <p3dKittlerThresholding_8+0x354>
xor    %eax,%eax
cmp    %r14b,(%r12)
jbe    1369 <p3dKittlerThresholding_8+0x209>
mov    (%r8),%eax
mov    %al,0x0(%rbp)
cmp    $0x1,%edi
je     14b4 <p3dKittlerThresholding_8+0x354>
lea    -0x1(%r9),%r11
mov    $0x1,%eax
cmp    $0x8,%r11
jb     148c <p3dKittlerThresholding_8+0x32c>
lea    0x1(%rbp),%rcx
lea    (%r9,%rbp,1),%rsi
lea    0x1(%r12),%r14
cmp    %r10,%rbp
mov    %r9,%rdx
setb   %r9b
cmp    %r10,%rsi
seta   %r10b
mov    %rdx,0x8(%rsp)
add    %r12,%rdx
cmp    %rdx,%rcx
setb   %dl
cmp    %rsi,%r14
setb   %bpl
lea    0x4(%r8),%rdi
cmp    %rdi,%rcx
setb   %cl
cmp    %r8,%rsi
seta   %r8b
test   %r10b,%r9b
jne    147a <p3dKittlerThresholding_8+0x31a>
and    %bpl,%dl
jne    147a <p3dKittlerThresholding_8+0x31a>
and    %r8b,%cl
mov    0x18(%rsp),%r10
mov    %r15,%r8
mov    0x10(%rsp),%rbp
mov    0x8(%rsp),%r9
jne    148c <p3dKittlerThresholding_8+0x32c>
mov    %r11,%rdx
and    $0xfffffffffffffff8,%rdx
lea    0x1(%rdx),%rax
movzbl (%r10),%ecx
movd   %ecx,%xmm0
punpcklbw %xmm0,%xmm0
pshuflw $0x0,%xmm0,%xmm0
movd   (%r8),%xmm1
punpcklbw %xmm1,%xmm1
pshuflw $0x0,%xmm1,%xmm1
xor    %esi,%esi
cs nopw 0x0(%rax,%rax,1)
nop
movd   0x1(%r12,%rsi,1),%xmm2
movd   0x5(%r12,%rsi,1),%xmm3
movdqa %xmm2,%xmm4
pminub %xmm0,%xmm4
pcmpeqb %xmm2,%xmm4
movdqa %xmm3,%xmm2
pminub %xmm0,%xmm2
pcmpeqb %xmm3,%xmm2
pandn  %xmm1,%xmm4
pandn  %xmm1,%xmm2
movd   %xmm4,0x1(%rbp,%rsi,1)
movd   %xmm2,0x5(%rbp,%rsi,1)
add    $0x8,%rsi
cmp    %rsi,%rdx
jne    1430 <p3dKittlerThresholding_8+0x2d0>
cmp    %rdx,%r11
jne    148c <p3dKittlerThresholding_8+0x32c>
jmp    14b4 <p3dKittlerThresholding_8+0x354>
mov    0x18(%rsp),%r10
mov    %r15,%r8
mov    0x10(%rsp),%rbp
mov    0x8(%rsp),%r9
mov    %rax,%rcx
not    %rcx
test   $0x1,%r9b
jne    14af <p3dKittlerThresholding_8+0x34f>
mov    (%r12,%rax,1),%dl
xor    %esi,%esi
cmp    (%r10),%dl
jbe    14a6 <p3dKittlerThresholding_8+0x346>
mov    (%r8),%esi
mov    %sil,0x0(%rbp,%rax,1)
add    $0x1,%rax
add    %r9,%rcx
jne    152e <p3dKittlerThresholding_8+0x3ce>
mov    0x60(%rsp),%r14
test   %r14,%r14
je     14ee <p3dKittlerThresholding_8+0x38e>
movzbl (%r10),%esi
lea    0xb8a(%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    0xb89(%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
nopw   0x0(%rax,%rax,1)
mov    %sil,0x1(%rbp,%rax,1)
add    $0x2,%rax
cmp    %rax,%r9
je     14b4 <p3dKittlerThresholding_8+0x354>
movzbl (%r12,%rax,1),%ecx
xor    %esi,%esi
mov    $0x0,%edx
cmp    (%r10),%cl
jbe    1542 <p3dKittlerThresholding_8+0x3e2>
mov    (%r8),%edx
mov    %dl,0x0(%rbp,%rax,1)
movzbl 0x1(%r12,%rax,1),%ecx
cmp    (%r10),%cl
jbe    1520 <p3dKittlerThresholding_8+0x3c0>
mov    (%r8),%esi
jmp    1520 <p3dKittlerThresholding_8+0x3c0>

LLVM IR (-O0):
@.str = [75 x i8] c"Pore3D - Thresholding image according to Kittler and Illingworth method...\00"
@UCHAR_MAX = global i32 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_8(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4, i8* %5, i32 (i8*, ...)* %6, i32 (i32, ...)* %7) {
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32 (i8*, ...)*
%17 = alloca i32 (i32, ...)*
%18 = alloca double*
%19 = alloca double*
%20 = alloca i32
%21 = alloca i32
%22 = alloca i32
store i8* %0, i8** %10
store i8* %1, i8** %11
store i32 %2, i32* %12
store i32 %3, i32* %13
store i32 %4, i32* %14
store i8* %5, i8** %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* @UCHAR_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 %167
36: ; preds = %29
%37 = load i32, i32* @UCHAR_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 %167
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 i8*, i8** %10
%55 = load i32, i32* %21
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8, i8* %57
%59 = zext i8 %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 i8*, i8** %10
%65 = load i32, i32* %21
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = load i8, i8* %67
%69 = zext i8 %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* @UCHAR_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* @UCHAR_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 i8
%108 = load i8*, i8** %15
store i8 %107, i8* %108
store i32 0, i32* %21
br label %109
109: ; preds = %138, %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 %141
117: ; preds = %109
%118 = load i8*, i8** %10
%119 = load i32, i32* %21
%120 = sext i32 %119 to i64
%121 = getelementptr inbounds i8, i8* %118, i64 %120
%122 = load i8, i8* %121
%123 = zext i8 %122 to i32
%124 = load i8*, i8** %15
%125 = load i8, i8* %124
%126 = zext i8 %125 to i32
%127 = icmp sgt i32 %123, %126
br i1 %127, label %128, label %130
128: ; preds = %117
%129 = load i32, i32* @UCHAR_MAX
br label %131
130: ; preds = %117
br label %131
131: ; preds = %130, %128
%132 = phi i32 [ %129, %128 ], [ 0, %130 ]
%133 = trunc i32 %132 to i8
%134 = load i8*, i8** %11
%135 = load i32, i32* %21
%136 = sext i32 %135 to i64
%137 = getelementptr inbounds i8, i8* %134, i64 %136
store i8 %133, i8* %137
br label %138
138: ; preds = %131
%139 = load i32, i32* %21
%140 = add nsw i32 %139, 1
store i32 %140, i32* %21
br label %109
141: ; preds = %109
%142 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%143 = icmp ne i32 (i8*, ...)* %142, null
br i1 %143, label %144, label %154
144: ; preds = %141
%145 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%146 = load i8*, i8** %15
%147 = load i8, i8* %146
%148 = zext i8 %147 to i32
%149 = call i32 (i8*, ...) %145(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %148)
%150 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%151 = call i32 (...) @p3dGetElapsedTime_min()
%152 = call i32 (...) @p3dGetElapsedTime_sec()
%153 = call i32 (i8*, ...) %150(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %151, i32 %152)
br label %154
154: ; preds = %144, %141
%155 = load double*, double** %18
%156 = icmp ne double* %155, null
br i1 %156, label %157, label %160
157: ; preds = %154
%158 = load double*, double** %18
%159 = call i32 @free(double* %158)
br label %160
160: ; preds = %157, %154
%161 = load double*, double** %19
%162 = icmp ne double* %161, null
br i1 %162, label %163, label %166
163: ; preds = %160
%164 = load double*, double** %19
%165 = call i32 @free(double* %164)
br label %166
166: ; preds = %163, %160
store i32 2, i32* %9
br label %186
167: ; preds = %42, %35
%168 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%169 = icmp ne i32 (i8*, ...)* %168, null
br i1 %169, label %170, label %173
170: ; preds = %167
%171 = load i32 (i8*, ...)*, i32 (i8*, ...)** %16
%172 = call i32 (i8*, ...) %171(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0))
br label %173
173: ; preds = %170, %167
%174 = load double*, double** %18
%175 = icmp ne double* %174, null
br i1 %175, label %176, label %179
176: ; preds = %173
%177 = load double*, double** %18
%178 = call i32 @free(double* %177)
br label %179
179: ; preds = %176, %173
%180 = load double*, double** %19
%181 = icmp ne double* %180, null
br i1 %181, label %182, label %185
182: ; preds = %179
%183 = load double*, double** %19
%184 = call i32 @free(double* %183)
br label %185
185: ; preds = %182, %179
store i32 0, i32* %9
br label %186
186: ; preds = %185, %166
%187 = load i32, i32* %9
ret i32 %187
}

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