#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 */
float INFINITY;
float atof(char *);
int atoi(char *);
float *calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(float *);
float *iio_read_image_float_split(char *, int *, int *, int *);
int /*<<< orphan*/ iio_save_image_float_split(char *, float *, int, int, int);
int remove_small_cc(int, int, float *, float *, int, float);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc < 3) {
    fprintf(stderr, " %s img out area [intensity_threshold (default INF)]\n",
            argv[0]);
    fprintf(stderr,
            "   remove connected compotents of img smaller than area\n"
            "   if intensity difference between two neighboring pixels of img\n"
            "   is larger than the threshold then the pixels are considered "
            "disjoint\n");
    return 1;
  }
  char *img_file = argv[1];
  char *out_file = argv[2];
  int area = atoi(argv[3]);
  float intensity_threshold = argc > 4 ? atof(argv[4]) : INFINITY;
  int w, h, nch;
  float *img = iio_read_image_float_split(img_file, &w, &h, &nch);
  float *out = calloc(w * h * nch, sizeof(*out));
  int n_cc = remove_small_cc(w, h, img, out, area, intensity_threshold);
  fprintf(stderr, "remaining components %d\n", n_cc);
  iio_save_image_float_split(out_file, out, w, h, nch);
  free(img);
  free(out);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11e1 <main+0x61>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <main+0x158>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x4,-0x8(%rbp)
jle    122a <main+0xaa>
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1040 <atof@plt>
movss  %xmm0,-0x50(%rbp)
jmp    123a <main+0xba>
mov    0x2db7(%rip),%rax
movss  (%rax),%xmm0
movss  %xmm0,-0x50(%rbp)
movss  -0x50(%rbp),%xmm0
movss  %xmm0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x34(%rbp),%rcx
call   10a0 <iio_read_image_float_split@plt>
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%edi
imul   -0x30(%rbp),%edi
imul   -0x34(%rbp),%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x30(%rbp),%esi
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x24(%rbp),%r8d
movss  -0x28(%rbp),%xmm0
call   1050 <remove_small_cc@plt>
mov    %eax,-0x4c(%rbp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
mov    -0x4c(%rbp),%edx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x34(%rbp),%r8d
call   1080 <iio_save_image_float_split@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
cmp    $0x2,%edi
jg     11cb <main+0x4b>
mov    0x2e3b(%rip),%rbx
mov    (%rbx),%edi
mov    0x0(%rbp),%rdx
lea    0xe56(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe7c(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%ebx
jmp    1296 <main+0x116>
mov    %edi,%ebx
mov    0x8(%rbp),%r12
mov    0x10(%rbp),%r14
mov    0x18(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%r15d
cmp    $0x5,%ebx
jb     11f1 <main+0x71>
mov    0x20(%rbp),%rdi
call   1040 <atof@plt>
jmp    11fc <main+0x7c>
mov    0x2df0(%rip),%rax
movss  (%rax),%xmm0
movss  %xmm0,0x8(%rsp)
lea    0x14(%rsp),%rsi
lea    0x10(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %r12,%rdi
call   10a0 <iio_read_image_float_split@plt>
mov    %rax,%r12
mov    0x14(%rsp),%ebx
mov    0x10(%rsp),%ebp
mov    %ebp,%edi
imul   %ebx,%edi
imul   0xc(%rsp),%edi
mov    $0x4,%esi
call   1060 <calloc@plt>
mov    %rax,%r13
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r12,%rdx
mov    %rax,%rcx
mov    %r15d,%r8d
movss  0x8(%rsp),%xmm0
call   1050 <remove_small_cc@plt>
mov    0x2d7e(%rip),%rcx
mov    (%rcx),%edi
lea    0xe95(%rip),%rsi
xor    %ebx,%ebx
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x14(%rsp),%edx
mov    0x10(%rsp),%ecx
mov    0xc(%rsp),%r8d
mov    %r14,%rdi
mov    %r13,%rsi
call   1080 <iio_save_image_float_split@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [54 x i8] c" %s img out area [intensity_threshold (default INF)]\0A\00"
@.str.1 = [194 x i8] c" remove connected compotents of img smaller than area\0A if intensity difference between two neighboring pixels of img\0A is larger than the threshold then the pixels are considered disjoint\0A\00"
@INFINITY = global float 0.000000e+00
@.str.2 = [25 x i8] c"remaining components %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca float
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca float*
%14 = alloca float*
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %26
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([194 x i8], [194 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %76
26: ; preds = %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
store i8* %29, i8** %6
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 2
%32 = load i8*, i8** %31
store i8* %32, i8** %7
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 3
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
store i32 %36, i32* %8
%37 = load i32, i32* %4
%38 = icmp sgt i32 %37, 4
br i1 %38, label %39, label %44
39: ; preds = %26
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 4
%42 = load i8*, i8** %41
%43 = call float @atof(i8* %42)
br label %46
44: ; preds = %26
%45 = load float, float* @INFINITY
br label %46
46: ; preds = %44, %39
%47 = phi float [ %43, %39 ], [ %45, %44 ]
store float %47, float* %9
%48 = load i8*, i8** %6
%49 = call float* @iio_read_image_float_split(i8* %48, i32* %10, i32* %11, i32* %12)
store float* %49, float** %13
%50 = load i32, i32* %10
%51 = load i32, i32* %11
%52 = mul nsw i32 %50, %51
%53 = load i32, i32* %12
%54 = mul nsw i32 %52, %53
%55 = call float* @calloc(i32 %54, i32 4)
store float* %55, float** %14
%56 = load i32, i32* %10
%57 = load i32, i32* %11
%58 = load float*, float** %13
%59 = load float*, float** %14
%60 = load i32, i32* %8
%61 = load float, float* %9
%62 = call i32 @remove_small_cc(i32 %56, i32 %57, float* %58, float* %59, i32 %60, float %61)
store i32 %62, i32* %15
%63 = load i32, i32* @stderr
%64 = load i32, i32* %15
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %64)
%66 = load i8*, i8** %7
%67 = load float*, float** %14
%68 = load i32, i32* %10
%69 = load i32, i32* %11
%70 = load i32, i32* %12
%71 = call i32 @iio_save_image_float_split(i8* %66, float* %67, i32 %68, i32 %69, i32 %70)
%72 = load float*, float** %13
%73 = call i32 @free(float* %72)
%74 = load float*, float** %14
%75 = call i32 @free(float* %74)
br label %76
76: ; preds = %46, %18
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [54 x i8] c" %s img out area [intensity_threshold (default INF)]\0A\00"
@.str.1 = [194 x i8] c" remove connected compotents of img smaller than area\0A if intensity difference between two neighboring pixels of img\0A is larger than the threshold then the pixels are considered disjoint\0A\00"
@INFINITY = global float 0.000000e+00
@.str.2 = [25 x i8] c"remaining components %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = icmp slt i32 %0, 3
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @stderr
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([194 x i8], [194 x i8]* @.str.1, i64 0, i64 0))
br label %49
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = getelementptr inbounds i8*, i8** %1, i64 3
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = icmp ugt i32 %0, 4
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = getelementptr inbounds i8*, i8** %1, i64 4
%24 = load i8*, i8** %23
%25 = tail call float @atof(i8* %24)
br label %28
26: ; preds = %13
%27 = load float, float* @INFINITY
br label %28
28: ; preds = %26, %22
%29 = phi float [ %25, %22 ], [ %27, %26 ]
%30 = bitcast i32* %3 to i8*
%31 = bitcast i32* %4 to i8*
%32 = bitcast i32* %5 to i8*
%33 = call float* @iio_read_image_float_split(i8* %15, i32* %3, i32* %4, i32* %5)
%34 = load i32, i32* %3
%35 = load i32, i32* %4
%36 = mul nsw i32 %35, %34
%37 = load i32, i32* %5
%38 = mul nsw i32 %36, %37
%39 = call float* @calloc(i32 %38, i32 4)
%40 = call i32 @remove_small_cc(i32 %34, i32 %35, float* %33, float* %39, i32 %20, float %29)
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%43 = load i32, i32* %3
%44 = load i32, i32* %4
%45 = load i32, i32* %5
%46 = call i32 @iio_save_image_float_split(i8* %17, float* %39, i32 %43, i32 %44, i32 %45)
%47 = call i32 @free(float* %33)
%48 = call i32 @free(float* %39)
br label %49
49: ; preds = %28, %7
%50 = phi i32 [ 1, %7 ], [ 0, %28 ]
ret i32 %50
}