int adaptive_threshold_segmentation(the_image, out_image, value, segment, rows,
                                    cols)
int rows, cols, segment;
short **the_image, **out_image, value;
{
  int peak1, peak2;
  short background, hi, low, object;
  unsigned long histogram[255 + 1];
  zero_histogram(histogram, 255 + 1);
  calculate_histogram(the_image, histogram, rows, cols);
  smooth_histogram(histogram, 255 + 1);
  find_peaks(histogram, &peak1, &peak2);
  peaks_high_low(histogram, peak1, peak2, &hi, &low);
  threshold_and_find_means(the_image, out_image, hi, low, value, &object,
                           &background, rows, cols);
  peaks_high_low(histogram, object, background, &hi, &low);
  printf("\nATS> hi=%d low=%d\n", hi, low);
  threshold_image_array(the_image, out_image, hi, low, value, rows, cols);
  if (segment == 1)
    grow(out_image, value, rows, cols);
  return (1);
}


x86 assembly (clang, -O0):
<adaptive_threshold_segmentation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %dx,%ax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9d,-0x20(%rbp)
lea    -0x830(%rbp),%rdi
mov    $0x100,%esi
mov    $0x0,%al
call   10b0 <zero_histogram@plt>
mov    -0x8(%rbp),%rdi
lea    -0x830(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    $0x0,%al
call   1080 <calculate_histogram@plt>
lea    -0x830(%rbp),%rdi
mov    $0x100,%esi
mov    $0x0,%al
call   1040 <smooth_histogram@plt>
lea    -0x830(%rbp),%rdi
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%rdx
mov    $0x0,%al
call   10a0 <find_peaks@plt>
lea    -0x830(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    -0x2c(%rbp),%rcx
lea    -0x2e(%rbp),%r8
mov    $0x0,%al
call   1090 <peaks_high_low@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movswl -0x2c(%rbp),%edx
movswl -0x2e(%rbp),%ecx
movswl -0x12(%rbp),%r8d
mov    -0x1c(%rbp),%r10d
mov    -0x20(%rbp),%eax
lea    -0x30(%rbp),%r9
lea    -0x2a(%rbp),%r11
mov    %r11,(%rsp)
mov    %r10d,0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    $0x0,%al
call   1030 <threshold_and_find_means@plt>
lea    -0x830(%rbp),%rdi
movswl -0x30(%rbp),%esi
movswl -0x2a(%rbp),%edx
lea    -0x2c(%rbp),%rcx
lea    -0x2e(%rbp),%r8
mov    $0x0,%al
call   1090 <peaks_high_low@plt>
movswl -0x2c(%rbp),%esi
movswl -0x2e(%rbp),%edx
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movswl -0x2c(%rbp),%edx
movswl -0x2e(%rbp),%ecx
movswl -0x12(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
mov    -0x20(%rbp),%eax
mov    %eax,(%rsp)
mov    $0x0,%al
call   1060 <threshold_image_array@plt>
cmpl   $0x1,-0x18(%rbp)
jne    12d6 <adaptive_threshold_segmentation+0x146>
mov    -0x10(%rbp),%rdi
movswl -0x12(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    $0x0,%al
call   1050 <grow@plt>
mov    $0x1,%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adaptive_threshold_segmentation>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %r9d,%r14d
mov    %r8d,%r13d
mov    %ecx,0x14(%rsp)
mov    %edx,%r12d
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
xor    %eax,%eax
call   10b0 <zero_histogram@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1080 <calculate_histogram@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
xor    %eax,%eax
call   1040 <smooth_histogram@plt>
lea    0x1c(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   10a0 <find_peaks@plt>
mov    0x1c(%rsp),%esi
mov    0x18(%rsp),%edx
lea    0xe(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <peaks_high_low@plt>
movswl 0xe(%rsp),%edx
movswl 0xc(%rsp),%ecx
movswl %r12w,%r12d
sub    $0x8,%rsp
lea    0x1a(%rsp),%r10
lea    0x18(%rsp),%r9
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12d,%r8d
xor    %eax,%eax
push   %r14
push   %r13
push   %r10
call   1030 <threshold_and_find_means@plt>
add    $0x20,%rsp
movswl 0x10(%rsp),%esi
movswl 0x12(%rsp),%edx
mov    %rbx,%rdi
lea    0xe(%rsp),%rcx
lea    0xc(%rsp),%r8
xor    %eax,%eax
call   1090 <peaks_high_low@plt>
movswl 0xe(%rsp),%esi
movswl 0xc(%rsp),%edx
lea    0xd81(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
movswl 0xe(%rsp),%edx
movswl 0xc(%rsp),%ecx
sub    $0x8,%rsp
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12d,%r8d
mov    %r13d,%r9d
xor    %eax,%eax
push   %r14
call   1060 <threshold_image_array@plt>
add    $0x10,%rsp
cmpl   $0x1,0x14(%rsp)
jne    12c7 <adaptive_threshold_segmentation+0x137>
mov    %r15,%rdi
mov    %r12d,%esi
mov    %r13d,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <grow@plt>
mov    $0x1,%eax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"\0AATS> hi=%d low=%d\0A\00"
define i32 @adaptive_threshold_segmentation(i16** %0, i16** %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i16**
%8 = alloca i16**
%9 = alloca i16
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i16
%16 = alloca i16
%17 = alloca i16
%18 = alloca i16
%19 = alloca [256 x i64]
%20 = trunc i32 %2 to i16
store i16** %0, i16*** %7
store i16** %1, i16*** %8
store i16 %20, i16* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%21 = getelementptr inbounds [256 x i64], [256 x i64]* %19, i64 0, i64 0
%22 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @zero_histogram to i32 (i64*, i32, ...)*)(i64* %21, i32 256)
%23 = load i16**, i16*** %7
%24 = getelementptr inbounds [256 x i64], [256 x i64]* %19, i64 0, i64 0
%25 = load i32, i32* %11
%26 = load i32, i32* %12
%27 = call i32 (i16**, i64*, i32, i32, ...) bitcast (i32 (...)* @calculate_histogram to i32 (i16**, i64*, i32, i32, ...)*)(i16** %23, i64* %24, i32 %25, i32 %26)
%28 = getelementptr inbounds [256 x i64], [256 x i64]* %19, i64 0, i64 0
%29 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @smooth_histogram to i32 (i64*, i32, ...)*)(i64* %28, i32 256)
%30 = getelementptr inbounds [256 x i64], [256 x i64]* %19, i64 0, i64 0
%31 = call i32 (i64*, i32*, i32*, ...) bitcast (i32 (...)* @find_peaks to i32 (i64*, i32*, i32*, ...)*)(i64* %30, i32* %13, i32* %14)
%32 = getelementptr inbounds [256 x i64], [256 x i64]* %19, i64 0, i64 0
%33 = load i32, i32* %13
%34 = load i32, i32* %14
%35 = call i32 (i64*, i32, i32, i16*, i16*, ...) bitcast (i32 (...)* @peaks_high_low to i32 (i64*, i32, i32, i16*, i16*, ...)*)(i64* %32, i32 %33, i32 %34, i16* %16, i16* %17)
%36 = load i16**, i16*** %7
%37 = load i16**, i16*** %8
%38 = load i16, i16* %16
%39 = sext i16 %38 to i32
%40 = load i16, i16* %17
%41 = sext i16 %40 to i32
%42 = load i16, i16* %9
%43 = sext i16 %42 to i32
%44 = load i32, i32* %11
%45 = load i32, i32* %12
%46 = call i32 (i16**, i16**, i32, i32, i32, i16*, i16*, i32, i32, ...) bitcast (i32 (...)* @threshold_and_find_means to i32 (i16**, i16**, i32, i32, i32, i16*, i16*, i32, i32, ...)*)(i16** %36, i16** %37, i32 %39, i32 %41, i32 %43, i16* %18, i16* %15, i32 %44, i32 %45)
%47 = getelementptr inbounds [256 x i64], [256 x i64]* %19, i64 0, i64 0
%48 = load i16, i16* %18
%49 = sext i16 %48 to i32
%50 = load i16, i16* %15
%51 = sext i16 %50 to i32
%52 = call i32 (i64*, i32, i32, i16*, i16*, ...) bitcast (i32 (...)* @peaks_high_low to i32 (i64*, i32, i32, i16*, i16*, ...)*)(i64* %47, i32 %49, i32 %51, i16* %16, i16* %17)
%53 = load i16, i16* %16
%54 = sext i16 %53 to i32
%55 = load i16, i16* %17
%56 = sext i16 %55 to i32
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %54, i32 %56)
%58 = load i16**, i16*** %7
%59 = load i16**, i16*** %8
%60 = load i16, i16* %16
%61 = sext i16 %60 to i32
%62 = load i16, i16* %17
%63 = sext i16 %62 to i32
%64 = load i16, i16* %9
%65 = sext i16 %64 to i32
%66 = load i32, i32* %11
%67 = load i32, i32* %12
%68 = call i32 (i16**, i16**, i32, i32, i32, i32, i32, ...) bitcast (i32 (...)* @threshold_image_array to i32 (i16**, i16**, i32, i32, i32, i32, i32, ...)*)(i16** %58, i16** %59, i32 %61, i32 %63, i32 %65, i32 %66, i32 %67)
%69 = load i32, i32* %10
%70 = icmp eq i32 %69, 1
br i1 %70, label %71, label %78
71: ; preds = %6
%72 = load i16**, i16*** %8
%73 = load i16, i16* %9
%74 = sext i16 %73 to i32
%75 = load i32, i32* %11
%76 = load i32, i32* %12
%77 = call i32 (i16**, i32, i32, i32, ...) bitcast (i32 (...)* @grow to i32 (i16**, i32, i32, i32, ...)*)(i16** %72, i32 %74, i32 %75, i32 %76)
br label %78
78: ; preds = %71, %6
ret i32 1
}

LLVM IR (-O2):
@.str = [20 x i8] c"\0AATS> hi=%d low=%d\0A\00"
define i32 @adaptive_threshold_segmentation(i16** %0, i16** %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i16
%10 = alloca i16
%11 = alloca i16
%12 = alloca i16
%13 = alloca [256 x i64]
%14 = bitcast i32* %7 to i8*
%15 = bitcast i32* %8 to i8*
%16 = bitcast i16* %9 to i8*
%17 = bitcast i16* %10 to i8*
%18 = bitcast i16* %11 to i8*
%19 = bitcast i16* %12 to i8*
%20 = bitcast [256 x i64]* %13 to i8*
%21 = getelementptr inbounds [256 x i64], [256 x i64]* %13, i64 0, i64 0
%22 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @zero_histogram to i32 (i64*, i32, ...)*)(i64* %21, i32 256)
%23 = call i32 (i16**, i64*, i32, i32, ...) bitcast (i32 (...)* @calculate_histogram to i32 (i16**, i64*, i32, i32, ...)*)(i16** %0, i64* %21, i32 %4, i32 %5)
%24 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @smooth_histogram to i32 (i64*, i32, ...)*)(i64* %21, i32 256)
%25 = call i32 (i64*, i32*, i32*, ...) bitcast (i32 (...)* @find_peaks to i32 (i64*, i32*, i32*, ...)*)(i64* %21, i32* %7, i32* %8)
%26 = load i32, i32* %7
%27 = load i32, i32* %8
%28 = call i32 (i64*, i32, i32, i16*, i16*, ...) bitcast (i32 (...)* @peaks_high_low to i32 (i64*, i32, i32, i16*, i16*, ...)*)(i64* %21, i32 %26, i32 %27, i16* %10, i16* %11)
%29 = load i16, i16* %10
%30 = sext i16 %29 to i32
%31 = load i16, i16* %11
%32 = sext i16 %31 to i32
%33 = shl i32 %2, 16
%34 = ashr exact i32 %33, 16
%35 = call i32 (i16**, i16**, i32, i32, i32, i16*, i16*, i32, i32, ...) bitcast (i32 (...)* @threshold_and_find_means to i32 (i16**, i16**, i32, i32, i32, i16*, i16*, i32, i32, ...)*)(i16** %0, i16** %1, i32 %30, i32 %32, i32 %34, i16* %12, i16* %9, i32 %4, i32 %5)
%36 = load i16, i16* %12
%37 = sext i16 %36 to i32
%38 = load i16, i16* %9
%39 = sext i16 %38 to i32
%40 = call i32 (i64*, i32, i32, i16*, i16*, ...) bitcast (i32 (...)* @peaks_high_low to i32 (i64*, i32, i32, i16*, i16*, ...)*)(i64* %21, i32 %37, i32 %39, i16* %10, i16* %11)
%41 = load i16, i16* %10
%42 = sext i16 %41 to i32
%43 = load i16, i16* %11
%44 = sext i16 %43 to i32
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %42, i32 %44)
%46 = load i16, i16* %10
%47 = sext i16 %46 to i32
%48 = load i16, i16* %11
%49 = sext i16 %48 to i32
%50 = call i32 (i16**, i16**, i32, i32, i32, i32, i32, ...) bitcast (i32 (...)* @threshold_image_array to i32 (i16**, i16**, i32, i32, i32, i32, i32, ...)*)(i16** %0, i16** %1, i32 %47, i32 %49, i32 %34, i32 %4, i32 %5)
%51 = icmp eq i32 %3, 1
br i1 %51, label %52, label %54
52: ; preds = %6
%53 = call i32 (i16**, i32, i32, i32, ...) bitcast (i32 (...)* @grow to i32 (i16**, i32, i32, i32, ...)*)(i16** %1, i32 %34, i32 %4, i32 %5)
br label %54
54: ; preds = %52, %6
ret i32 1
}