int valley_threshold_segmentation(the_image, out_image, value, segment, rows,
                                  cols)
int rows, cols, segment;
short **the_image, **out_image, value;
{
  int peak1, peak2;
  short hi, low;
  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);
  valley_high_low(histogram, peak1, peak2, &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):
<valley_threshold_segmentation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%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   1090 <zero_histogram@plt>
mov    -0x8(%rbp),%rdi
lea    -0x830(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    $0x0,%al
call   1060 <calculate_histogram@plt>
lea    -0x830(%rbp),%rdi
mov    $0x100,%esi
mov    $0x0,%al
call   1030 <smooth_histogram@plt>
lea    -0x830(%rbp),%rdi
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%rdx
mov    $0x0,%al
call   1080 <find_peaks@plt>
lea    -0x830(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    -0x2a(%rbp),%rcx
lea    -0x2c(%rbp),%r8
mov    $0x0,%al
call   1070 <valley_high_low@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movswl -0x2a(%rbp),%edx
movswl -0x2c(%rbp),%ecx
movswl -0x12(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
mov    -0x20(%rbp),%eax
mov    %eax,(%rsp)
mov    $0x0,%al
call   1050 <threshold_image_array@plt>
cmpl   $0x1,-0x18(%rbp)
jne    124a <valley_threshold_segmentation+0xda>
mov    -0x10(%rbp),%rdi
movswl -0x12(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    $0x0,%al
call   1040 <grow@plt>
mov    $0x1,%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<valley_threshold_segmentation>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x828,%rsp
mov    %r9d,%r12d
mov    %r8d,%r13d
mov    %ecx,0x14(%rsp)
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0x100,%esi
xor    %eax,%eax
call   1090 <zero_histogram@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1060 <calculate_histogram@plt>
mov    %r15,%rdi
mov    $0x100,%esi
xor    %eax,%eax
call   1030 <smooth_histogram@plt>
lea    0x1c(%rsp),%rsi
lea    0x18(%rsp),%rdx
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <find_peaks@plt>
mov    0x1c(%rsp),%esi
mov    0x18(%rsp),%edx
lea    0x12(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <valley_high_low@plt>
movswl 0x12(%rsp),%edx
movswl 0x10(%rsp),%ecx
movswl %bp,%ebp
mov    %r12d,(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%r8d
mov    %r13d,%r9d
xor    %eax,%eax
call   1050 <threshold_image_array@plt>
cmpl   $0x1,0x14(%rsp)
jne    1236 <valley_threshold_segmentation+0xc6>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %r13d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1040 <grow@plt>
mov    $0x1,%eax
add    $0x828,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @valley_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 [256 x i64]
%18 = trunc i32 %2 to i16
store i16** %0, i16*** %7
store i16** %1, i16*** %8
store i16 %18, i16* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
%19 = getelementptr inbounds [256 x i64], [256 x i64]* %17, i64 0, i64 0
%20 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @zero_histogram to i32 (i64*, i32, ...)*)(i64* %19, i32 256)
%21 = load i16**, i16*** %7
%22 = getelementptr inbounds [256 x i64], [256 x i64]* %17, i64 0, i64 0
%23 = load i32, i32* %11
%24 = load i32, i32* %12
%25 = call i32 (i16**, i64*, i32, i32, ...) bitcast (i32 (...)* @calculate_histogram to i32 (i16**, i64*, i32, i32, ...)*)(i16** %21, i64* %22, i32 %23, i32 %24)
%26 = getelementptr inbounds [256 x i64], [256 x i64]* %17, i64 0, i64 0
%27 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @smooth_histogram to i32 (i64*, i32, ...)*)(i64* %26, i32 256)
%28 = getelementptr inbounds [256 x i64], [256 x i64]* %17, i64 0, i64 0
%29 = call i32 (i64*, i32*, i32*, ...) bitcast (i32 (...)* @find_peaks to i32 (i64*, i32*, i32*, ...)*)(i64* %28, i32* %13, i32* %14)
%30 = getelementptr inbounds [256 x i64], [256 x i64]* %17, i64 0, i64 0
%31 = load i32, i32* %13
%32 = load i32, i32* %14
%33 = call i32 (i64*, i32, i32, i16*, i16*, ...) bitcast (i32 (...)* @valley_high_low to i32 (i64*, i32, i32, i16*, i16*, ...)*)(i64* %30, i32 %31, i32 %32, i16* %15, i16* %16)
%34 = load i16**, i16*** %7
%35 = load i16**, i16*** %8
%36 = load i16, i16* %15
%37 = sext i16 %36 to i32
%38 = load i16, i16* %16
%39 = sext i16 %38 to i32
%40 = load i16, i16* %9
%41 = sext i16 %40 to i32
%42 = load i32, i32* %11
%43 = load i32, i32* %12
%44 = call i32 (i16**, i16**, i32, i32, i32, i32, i32, ...) bitcast (i32 (...)* @threshold_image_array to i32 (i16**, i16**, i32, i32, i32, i32, i32, ...)*)(i16** %34, i16** %35, i32 %37, i32 %39, i32 %41, i32 %42, i32 %43)
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, 1
br i1 %46, label %47, label %54
47: ; preds = %6
%48 = load i16**, i16*** %8
%49 = load i16, i16* %9
%50 = sext i16 %49 to i32
%51 = load i32, i32* %11
%52 = load i32, i32* %12
%53 = call i32 (i16**, i32, i32, i32, ...) bitcast (i32 (...)* @grow to i32 (i16**, i32, i32, i32, ...)*)(i16** %48, i32 %50, i32 %51, i32 %52)
br label %54
54: ; preds = %47, %6
ret i32 1
}

LLVM IR (-O2):
define i32 @valley_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 [256 x i64]
%12 = bitcast i32* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = bitcast i16* %9 to i8*
%15 = bitcast i16* %10 to i8*
%16 = bitcast [256 x i64]* %11 to i8*
%17 = getelementptr inbounds [256 x i64], [256 x i64]* %11, i64 0, i64 0
%18 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @zero_histogram to i32 (i64*, i32, ...)*)(i64* %17, i32 256)
%19 = call i32 (i16**, i64*, i32, i32, ...) bitcast (i32 (...)* @calculate_histogram to i32 (i16**, i64*, i32, i32, ...)*)(i16** %0, i64* %17, i32 %4, i32 %5)
%20 = call i32 (i64*, i32, ...) bitcast (i32 (...)* @smooth_histogram to i32 (i64*, i32, ...)*)(i64* %17, i32 256)
%21 = call i32 (i64*, i32*, i32*, ...) bitcast (i32 (...)* @find_peaks to i32 (i64*, i32*, i32*, ...)*)(i64* %17, i32* %7, i32* %8)
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = call i32 (i64*, i32, i32, i16*, i16*, ...) bitcast (i32 (...)* @valley_high_low to i32 (i64*, i32, i32, i16*, i16*, ...)*)(i64* %17, i32 %22, i32 %23, i16* %9, i16* %10)
%25 = load i16, i16* %9
%26 = sext i16 %25 to i32
%27 = load i16, i16* %10
%28 = sext i16 %27 to i32
%29 = shl i32 %2, 16
%30 = ashr exact i32 %29, 16
%31 = 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 %26, i32 %28, i32 %30, i32 %4, i32 %5)
%32 = icmp eq i32 %3, 1
br i1 %32, label %33, label %35
33: ; preds = %6
%34 = call i32 (i16**, i32, i32, i32, ...) bitcast (i32 (...)* @grow to i32 (i16**, i32, i32, i32, ...)*)(i16** %1, i32 %30, i32 %4, i32 %5)
br label %35
35: ; preds = %33, %6
ret i32 1
}