#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(double *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(double *, int /*<<< orphan*/, int);
int /*<<< orphan*/ morphological_feature_object_moment(double, double, int **,
                                                       int, int, int,
                                                       unsigned char *,
                                                       double *);
int /*<<< orphan*/ printf(char *, int);
int morphological_feature_calculate_centroids(int **obj_id, int width,
                                              int height, int n_object,
                                              unsigned char *image,
                                              double **centroids) {
  unsigned int i = 0;
  double *moment00 = NULL;
  moment00 = (double *)malloc(n_object * sizeof(double));
  if (moment00 == NULL) {
    printf("Cannot allocate %d bytes for memory.\n",
           (n_object * sizeof(double)));
    exit(-1);
  }
  memset(moment00, 0, (n_object * sizeof(double)));
  double *moment10 = NULL;
  moment10 = (double *)malloc(n_object * sizeof(double));
  if (moment10 == NULL) {
    printf("Cannot allocate %d bytes for memory.\n",
           (n_object * sizeof(double)));
    exit(-1);
  }
  memset(moment10, 0, (n_object * sizeof(double)));
  double *moment01 = NULL;
  moment01 = (double *)malloc(n_object * sizeof(double));
  if (moment01 == NULL) {
    printf("Cannot allocate %d bytes for memory.\n",
           (n_object * sizeof(double)));
    exit(-1);
  }
  memset(moment01, 0, (n_object * sizeof(double)));
  morphological_feature_object_moment(0.0, 0.0, obj_id, width, height, n_object,
                                      image, moment00);
  morphological_feature_object_moment(1.0, 0.0, obj_id, width, height, n_object,
                                      image, moment10);
  morphological_feature_object_moment(0.0, 1.0, obj_id, width, height, n_object,
                                      image, moment01);
  for (i = 0; i < n_object; i++) {
    centroids[i][0] = moment10[i] / moment00[i];
    centroids[i][1] = moment01[i] / moment00[i];
  }
  free(moment00);
  free(moment10);
  free(moment01);
  return 1;
}


x86 assembly (clang, -O0):
<morphological_feature_calculate_centroids>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ca <morphological_feature_calculate_centroids+0x6a>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
lea    0xe4d(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
movq   $0x0,-0x40(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1225 <morphological_feature_calculate_centroids+0xc5>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
lea    0xdf2(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x40(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
movq   $0x0,-0x48(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1280 <morphological_feature_calculate_centroids+0x120>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
lea    0xd97(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x48(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%r8
mov    -0x38(%rbp),%r9
xorps  %xmm1,%xmm1
movaps %xmm1,%xmm0
call   1070 <morphological_feature_object_moment@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%r8
mov    -0x40(%rbp),%r9
movsd  0xd2e(%rip),%xmm0
xorps  %xmm1,%xmm1
call   1070 <morphological_feature_object_moment@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%r8
mov    -0x48(%rbp),%r9
xorps  %xmm0,%xmm0
movsd  0xd06(%rip),%xmm1
call   1070 <morphological_feature_object_moment@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    136f <morphological_feature_calculate_centroids+0x20f>
mov    -0x40(%rbp),%rax
mov    -0x2c(%rbp),%ecx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x38(%rbp),%rax
mov    -0x2c(%rbp),%ecx
divsd  (%rax,%rcx,8),%xmm0
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    (%rax,%rcx,8),%rax
movsd  %xmm0,(%rax)
mov    -0x48(%rbp),%rax
mov    -0x2c(%rbp),%ecx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x38(%rbp),%rax
mov    -0x2c(%rbp),%ecx
divsd  (%rax,%rcx,8),%xmm0
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    (%rax,%rcx,8),%rax
movsd  %xmm0,0x8(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1306 <morphological_feature_calculate_centroids+0x1a6>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<morphological_feature_calculate_centroids>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %edx,%r13d
mov    %esi,0x8(%rsp)
mov    %rdi,0x10(%rsp)
mov    %rcx,0x20(%rsp)
lea    0x0(,%rcx,8),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     12d9 <morphological_feature_calculate_centroids+0x179>
mov    %rax,%rbx
mov    %r13d,%r12d
mov    %r15,0x18(%rsp)
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     12d9 <morphological_feature_calculate_centroids+0x179>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     12d9 <morphological_feature_calculate_centroids+0x179>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
xorps  %xmm0,%xmm0
xorps  %xmm1,%xmm1
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%esi
mov    %r12d,%edx
mov    %r12d,0xc(%rsp)
mov    0x20(%rsp),%r12
mov    %r12d,%ecx
mov    0x18(%rsp),%rbp
mov    %rbp,%r8
mov    %rbx,%r9
call   1070 <morphological_feature_object_moment@plt>
movsd  0xdd8(%rip),%xmm0
xorps  %xmm1,%xmm1
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    %r12d,%ecx
mov    %rbp,%r8
mov    %r15,%r9
call   1070 <morphological_feature_object_moment@plt>
xorpd  %xmm0,%xmm0
movsd  0xdae(%rip),%xmm1
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%esi
mov    0xc(%rsp),%edx
mov    %r12d,%ecx
mov    %rbp,%r8
mov    %r13,%r9
call   1070 <morphological_feature_object_moment@plt>
test   %r12d,%r12d
je     12ad <morphological_feature_calculate_centroids+0x14d>
mov    %r12d,%eax
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
movsd  (%r15,%rcx,8),%xmm0
divsd  (%rbx,%rcx,8),%xmm0
mov    (%r14,%rcx,8),%rdx
movsd  %xmm0,(%rdx)
movsd  0x0(%r13,%rcx,8),%xmm0
divsd  (%rbx,%rcx,8),%xmm0
movsd  %xmm0,0x8(%rdx)
add    $0x1,%rcx
cmp    %rcx,%rax
jne    1280 <morphological_feature_calculate_centroids+0x120>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd28(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [38 x i8] c"Cannot allocate %d bytes for memory.\0A\00"
define i32 @morphological_feature_calculate_centroids(i32** %0, i32 %1, i32 %2, i32 %3, i8* %4, double** %5) {
%7 = alloca i32**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca double**
%13 = alloca i32
%14 = alloca double*
%15 = alloca double*
%16 = alloca double*
store i32** %0, i32*** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store double** %5, double*** %12
store i32 0, i32* %13
store double* null, double** %14
%17 = load i32, i32* %10
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 8
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to double*
store double* %22, double** %14
%23 = load double*, double** %14
%24 = icmp eq double* %23, null
br i1 %24, label %25, label %32
25: ; preds = %6
%26 = load i32, i32* %10
%27 = sext i32 %26 to i64
%28 = mul i64 %27, 8
%29 = trunc i64 %28 to i32
%30 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %6
%33 = load double*, double** %14
%34 = load i32, i32* %10
%35 = sext i32 %34 to i64
%36 = mul i64 %35, 8
%37 = trunc i64 %36 to i32
%38 = call i32 @memset(double* %33, i32 0, i32 %37)
store double* null, double** %15
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = mul i64 %40, 8
%42 = trunc i64 %41 to i32
%43 = call i64 @malloc(i32 %42)
%44 = inttoptr i64 %43 to double*
store double* %44, double** %15
%45 = load double*, double** %15
%46 = icmp eq double* %45, null
br i1 %46, label %47, label %54
47: ; preds = %32
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = mul i64 %49, 8
%51 = trunc i64 %50 to i32
%52 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %51)
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %32
%55 = load double*, double** %15
%56 = load i32, i32* %10
%57 = sext i32 %56 to i64
%58 = mul i64 %57, 8
%59 = trunc i64 %58 to i32
%60 = call i32 @memset(double* %55, i32 0, i32 %59)
store double* null, double** %16
%61 = load i32, i32* %10
%62 = sext i32 %61 to i64
%63 = mul i64 %62, 8
%64 = trunc i64 %63 to i32
%65 = call i64 @malloc(i32 %64)
%66 = inttoptr i64 %65 to double*
store double* %66, double** %16
%67 = load double*, double** %16
%68 = icmp eq double* %67, null
br i1 %68, label %69, label %76
69: ; preds = %54
%70 = load i32, i32* %10
%71 = sext i32 %70 to i64
%72 = mul i64 %71, 8
%73 = trunc i64 %72 to i32
%74 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %73)
%75 = call i32 @exit(i32 -1)
unreachable
76: ; preds = %54
%77 = load double*, double** %16
%78 = load i32, i32* %10
%79 = sext i32 %78 to i64
%80 = mul i64 %79, 8
%81 = trunc i64 %80 to i32
%82 = call i32 @memset(double* %77, i32 0, i32 %81)
%83 = load i32**, i32*** %7
%84 = load i32, i32* %8
%85 = load i32, i32* %9
%86 = load i32, i32* %10
%87 = load i8*, i8** %11
%88 = load double*, double** %14
%89 = call i32 @morphological_feature_object_moment(double 0.000000e+00, double 0.000000e+00, i32** %83, i32 %84, i32 %85, i32 %86, i8* %87, double* %88)
%90 = load i32**, i32*** %7
%91 = load i32, i32* %8
%92 = load i32, i32* %9
%93 = load i32, i32* %10
%94 = load i8*, i8** %11
%95 = load double*, double** %15
%96 = call i32 @morphological_feature_object_moment(double 1.000000e+00, double 0.000000e+00, i32** %90, i32 %91, i32 %92, i32 %93, i8* %94, double* %95)
%97 = load i32**, i32*** %7
%98 = load i32, i32* %8
%99 = load i32, i32* %9
%100 = load i32, i32* %10
%101 = load i8*, i8** %11
%102 = load double*, double** %16
%103 = call i32 @morphological_feature_object_moment(double 0.000000e+00, double 1.000000e+00, i32** %97, i32 %98, i32 %99, i32 %100, i8* %101, double* %102)
store i32 0, i32* %13
br label %104
104: ; preds = %143, %76
%105 = load i32, i32* %13
%106 = load i32, i32* %10
%107 = icmp ult i32 %105, %106
br i1 %107, label %108, label %146
108: ; preds = %104
%109 = load double*, double** %15
%110 = load i32, i32* %13
%111 = zext i32 %110 to i64
%112 = getelementptr inbounds double, double* %109, i64 %111
%113 = load double, double* %112
%114 = load double*, double** %14
%115 = load i32, i32* %13
%116 = zext i32 %115 to i64
%117 = getelementptr inbounds double, double* %114, i64 %116
%118 = load double, double* %117
%119 = fdiv double %113, %118
%120 = load double**, double*** %12
%121 = load i32, i32* %13
%122 = zext i32 %121 to i64
%123 = getelementptr inbounds double*, double** %120, i64 %122
%124 = load double*, double** %123
%125 = getelementptr inbounds double, double* %124, i64 0
store double %119, double* %125
%126 = load double*, double** %16
%127 = load i32, i32* %13
%128 = zext i32 %127 to i64
%129 = getelementptr inbounds double, double* %126, i64 %128
%130 = load double, double* %129
%131 = load double*, double** %14
%132 = load i32, i32* %13
%133 = zext i32 %132 to i64
%134 = getelementptr inbounds double, double* %131, i64 %133
%135 = load double, double* %134
%136 = fdiv double %130, %135
%137 = load double**, double*** %12
%138 = load i32, i32* %13
%139 = zext i32 %138 to i64
%140 = getelementptr inbounds double*, double** %137, i64 %139
%141 = load double*, double** %140
%142 = getelementptr inbounds double, double* %141, i64 1
store double %136, double* %142
br label %143
143: ; preds = %108
%144 = load i32, i32* %13
%145 = add i32 %144, 1
store i32 %145, i32* %13
br label %104
146: ; preds = %104
%147 = load double*, double** %14
%148 = call i32 @free(double* %147)
%149 = load double*, double** %15
%150 = call i32 @free(double* %149)
%151 = load double*, double** %16
%152 = call i32 @free(double* %151)
ret i32 1
}

LLVM IR (-O2):
@.str = [38 x i8] c"Cannot allocate %d bytes for memory.\0A\00"
define i32 @morphological_feature_calculate_centroids(i32** %0, i32 %1, i32 %2, i32 %3, i8* %4, double** nocapture %5) {
%7 = shl i32 %3, 3
%8 = tail call i64 @malloc(i32 %7)
%9 = inttoptr i64 %8 to double*
%10 = icmp eq i64 %8, 0
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %7)
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %6
%15 = tail call i32 @memset(double* %9, i32 0, i32 %7)
%16 = tail call i64 @malloc(i32 %7)
%17 = inttoptr i64 %16 to double*
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %7)
%21 = tail call i32 @exit(i32 -1)
unreachable
22: ; preds = %14
%23 = tail call i32 @memset(double* %17, i32 0, i32 %7)
%24 = tail call i64 @malloc(i32 %7)
%25 = inttoptr i64 %24 to double*
%26 = icmp eq i64 %24, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %7)
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %22
%31 = tail call i32 @memset(double* %25, i32 0, i32 %7)
%32 = tail call i32 @morphological_feature_object_moment(double 0.000000e+00, double 0.000000e+00, i32** %0, i32 %1, i32 %2, i32 %3, i8* %4, double* %9)
%33 = tail call i32 @morphological_feature_object_moment(double 1.000000e+00, double 0.000000e+00, i32** %0, i32 %1, i32 %2, i32 %3, i8* %4, double* %17)
%34 = tail call i32 @morphological_feature_object_moment(double 0.000000e+00, double 1.000000e+00, i32** %0, i32 %1, i32 %2, i32 %3, i8* %4, double* %25)
%35 = icmp eq i32 %3, 0
br i1 %35, label %54, label %36
36: ; preds = %30
%37 = zext i32 %3 to i64
br label %38
38: ; preds = %36, %38
%39 = phi i64 [ 0, %36 ], [ %52, %38 ]
%40 = getelementptr inbounds double, double* %17, i64 %39
%41 = load double, double* %40
%42 = getelementptr inbounds double, double* %9, i64 %39
%43 = load double, double* %42
%44 = fdiv double %41, %43
%45 = getelementptr inbounds double*, double** %5, i64 %39
%46 = load double*, double** %45
store double %44, double* %46
%47 = getelementptr inbounds double, double* %25, i64 %39
%48 = load double, double* %47
%49 = load double, double* %42
%50 = fdiv double %48, %49
%51 = getelementptr inbounds double, double* %46, i64 1
store double %50, double* %51
%52 = add nuw nsw i64 %39, 1
%53 = icmp eq i64 %52, %37
br i1 %53, label %54, label %38
54: ; preds = %38, %30
%55 = tail call i32 @free(double* %9)
%56 = tail call i32 @free(double* %17)
%57 = tail call i32 @free(double* %25)
ret i32 1
}