#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);
double pow(unsigned int, double);
int /*<<< orphan*/ printf(char *, int);
int morphological_feature_object_moment(double p, double q, int **obj_id,
                                        int width, int height, int n_object,
                                        unsigned char *image, double *moment) {
  unsigned int i = 0, j = 0;
  double *sum = NULL;
  sum = (double *)malloc(n_object * sizeof(double));
  if (sum == NULL) {
    printf("morphological_feature_object_moment:Cannot allocate %d bytes for "
           "memory.\n",
           (n_object * sizeof(double)));
    exit(-1);
  }
  memset(sum, 0, sizeof(n_object * sizeof(double)));
  for (i = 0; i < height; i++) {
    double a = pow(i, p);
    for (j = 0; j < width; j++) {
      double b = pow(j, q);
      if (!obj_id[i][j])
        continue;
      register unsigned int idx = (j + i * width) * 3;
      unsigned char val = ((unsigned char)(0.2126 * (double)(image[idx])) +
                           (unsigned char)(0.7152 * (double)(image[idx + 1])) +
                           (unsigned char)(0.0722 * (double)(image[idx + 2])));
      moment[(obj_id[i][j] - 1)] += a * b * (double)val;
      sum[(obj_id[i][j] - 1)] += (double)val;
    }
  }
  free(sum);
  return 1;
}


x86 assembly (clang, -O0):
<morphological_feature_object_moment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
mov    %edx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11db <morphological_feature_object_moment+0x7b>
movslq -0x24(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jae    1359 <morphological_feature_object_moment+0x1f9>
mov    -0x3c(%rbp),%edi
movsd  -0x8(%rbp),%xmm0
call   1070 <pow@plt>
movsd  %xmm0,-0x50(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    1346 <morphological_feature_object_moment+0x1e6>
mov    -0x40(%rbp),%edi
movsd  -0x10(%rbp),%xmm0
call   1070 <pow@plt>
movsd  %xmm0,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    (%rax,%rcx,8),%rax
mov    -0x40(%rbp),%ecx
cmpl   $0x0,(%rax,%rcx,4)
jne    1252 <morphological_feature_object_moment+0xf2>
jmp    1338 <morphological_feature_object_moment+0x1d8>
mov    -0x40(%rbp),%ecx
mov    -0x3c(%rbp),%eax
mov    -0x1c(%rbp),%edx
imul   %edx,%eax
add    %eax,%ecx
mov    %ecx,%eax
lea    (%rax,%rax,2),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x5c(%rbp),%eax
mov    %eax,%edx
movzbl (%rcx,%rax,1),%eax
cvtsi2sd %eax,%xmm0
movsd  0xd7f(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %edx,%esi
add    $0x1,%esi
movzbl (%rcx,%rsi,1),%esi
cvtsi2sd %esi,%xmm0
movsd  0xd6a(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%esi
add    %esi,%eax
add    $0x2,%edx
movzbl (%rcx,%rdx,1),%ecx
cvtsi2sd %ecx,%xmm0
movsd  0xd55(%rip),%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%ecx
add    %ecx,%eax
mov    %al,-0x5d(%rbp)
movsd  -0x50(%rbp),%xmm0
movsd  -0x58(%rbp),%xmm1
mulsd  %xmm1,%xmm0
movzbl -0x5d(%rbp),%eax
cvtsi2sd %eax,%xmm2
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x3c(%rbp),%edx
mov    (%rcx,%rdx,8),%rcx
mov    -0x40(%rbp),%edx
mov    (%rcx,%rdx,4),%ecx
add    $0xffffffff,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
movzbl -0x5d(%rbp),%eax
cvtsi2sd %eax,%xmm0
mov    -0x48(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    -0x3c(%rbp),%edx
mov    (%rcx,%rdx,8),%rcx
mov    -0x40(%rbp),%edx
mov    (%rcx,%rdx,4),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
addsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1217 <morphological_feature_object_moment+0xb7>
jmp    134b <morphological_feature_object_moment+0x1eb>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11f2 <morphological_feature_object_moment+0x92>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<morphological_feature_object_moment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r9,%r14
mov    %r8,%rbx
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,0x10(%rsp)
mov    %rdi,0x40(%rsp)
movsd  %xmm1,0x38(%rsp)
movsd  %xmm0,0x20(%rsp)
shl    $0x3,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1306 <morphological_feature_object_moment+0x1a6>
mov    %rax,%r13
xor    %eax,%eax
mov    %rax,(%rsp)
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
test   %r15d,%r15d
je     12ea <morphological_feature_object_moment+0x18a>
mov    %r15d,%eax
mov    %rax,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    %eax,%ecx
mov    %rcx,0x30(%rsp)
lea    (%rax,%rax,2),%eax
mov    %eax,0xc(%rsp)
xor    %ebp,%ebp
jmp    120b <morphological_feature_object_moment+0xab>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rbp
mov    (%rsp),%rax
add    0xc(%rsp),%eax
mov    %rax,(%rsp)
cmp    0x18(%rsp),%rbp
je     12ea <morphological_feature_object_moment+0x18a>
mov    %ebp,%edi
movsd  0x20(%rsp),%xmm0
call   1070 <pow@plt>
movsd  %xmm0,0x28(%rsp)
cmpl   $0x0,0x10(%rsp)
je     11f0 <morphological_feature_object_moment+0x90>
mov    (%rsp),%r12
xor    %r15d,%r15d
jmp    123f <morphological_feature_object_moment+0xdf>
xchg   %ax,%ax
add    $0x1,%r15
add    $0x3,%r12d
cmp    %r15,0x30(%rsp)
je     11f0 <morphological_feature_object_moment+0x90>
mov    %r15d,%edi
movsd  0x38(%rsp),%xmm0
call   1070 <pow@plt>
mov    0x40(%rsp),%rax
mov    (%rax,%rbp,8),%rax
movslq (%rax,%r15,4),%rax
test   %rax,%rax
je     1230 <morphological_feature_object_moment+0xd0>
mov    %r12d,%ecx
movzbl (%rbx,%rcx,1),%ecx
xorps  %xmm1,%xmm1
cvtsi2sd %ecx,%xmm1
mulsd  0xd8b(%rip),%xmm1
cvttsd2si %xmm1,%ecx
lea    0x1(%r12),%edx
movzbl (%rbx,%rdx,1),%edx
xorps  %xmm1,%xmm1
cvtsi2sd %edx,%xmm1
mulsd  0xd77(%rip),%xmm1
cvttsd2si %xmm1,%edx
lea    0x2(%r12),%esi
movzbl (%rbx,%rsi,1),%esi
xorps  %xmm1,%xmm1
cvtsi2sd %esi,%xmm1
add    %cl,%dl
mulsd  0xd61(%rip),%xmm1
cvttsd2si %xmm1,%ecx
add    %dl,%cl
movzbl %cl,%ecx
xorps  %xmm1,%xmm1
cvtsi2sd %ecx,%xmm1
mulsd  0x28(%rsp),%xmm0
mulsd  %xmm1,%xmm0
addsd  -0x8(%r14,%rax,8),%xmm0
movsd  %xmm0,-0x8(%r14,%rax,8)
addsd  -0x8(%r13,%rax,8),%xmm1
movsd  %xmm1,-0x8(%r13,%rax,8)
jmp    1230 <morphological_feature_object_moment+0xd0>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd0b(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [74 x i8] c"morphological_feature_object_moment:Cannot allocate %d bytes for memory.\0A\00"
define i32 @morphological_feature_object_moment(double %0, double %1, i32** nocapture %2, i32 %3, i32 %4, i32 %5, i8* nocapture %6, double* nocapture %7) {
%9 = shl i32 %5, 3
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to double*
%12 = icmp eq i64 %10, 0
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @printf(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str, i64 0, i64 0), i32 %9)
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %8
%17 = tail call i32 @memset(double* %11, i32 0, i32 8)
%18 = icmp eq i32 %4, 0
br i1 %18, label %79, label %19
19: ; preds = %16
%20 = icmp eq i32 %3, 0
%21 = zext i32 %4 to i64
%22 = zext i32 %3 to i64
br label %23
23: ; preds = %19, %76
%24 = phi i64 [ 0, %19 ], [ %77, %76 ]
%25 = trunc i64 %24 to i32
%26 = tail call double @pow(i32 %25, double %0)
br i1 %20, label %76, label %27
27: ; preds = %23
%28 = getelementptr inbounds i32*, i32** %2, i64 %24
%29 = mul i32 %25, %3
br label %30
30: ; preds = %27, %73
%31 = phi i64 [ 0, %27 ], [ %74, %73 ]
%32 = trunc i64 %31 to i32
%33 = tail call double @pow(i32 %32, double %1)
%34 = load i32*, i32** %28
%35 = getelementptr inbounds i32, i32* %34, i64 %31
%36 = load i32, i32* %35
%37 = icmp eq i32 %36, 0
br i1 %37, label %73, label %38
38: ; preds = %30
%39 = add i32 %29, %32
%40 = mul i32 %39, 3
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %6, i64 %41
%43 = load i8, i8* %42
%44 = uitofp i8 %43 to double
%45 = fmul double %44, 2.126000e-01
%46 = fptoui double %45 to i8
%47 = add i32 %40, 1
%48 = zext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %6, i64 %48
%50 = load i8, i8* %49
%51 = uitofp i8 %50 to double
%52 = fmul double %51, 7.152000e-01
%53 = fptoui double %52 to i8
%54 = add i8 %53, %46
%55 = add i32 %40, 2
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %6, i64 %56
%58 = load i8, i8* %57
%59 = uitofp i8 %58 to double
%60 = fmul double %59, 7.220000e-02
%61 = fptoui double %60 to i8
%62 = add i8 %54, %61
%63 = fmul double %26, %33
%64 = uitofp i8 %62 to double
%65 = add nsw i32 %36, -1
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds double, double* %7, i64 %66
%68 = load double, double* %67
%69 = tail call double @llvm.fmuladd.f64(double %63, double %64, double %68)
store double %69, double* %67
%70 = getelementptr inbounds double, double* %11, i64 %66
%71 = load double, double* %70
%72 = fadd double %71, %64
store double %72, double* %70
br label %73
73: ; preds = %30, %38
%74 = add nuw nsw i64 %31, 1
%75 = icmp eq i64 %74, %22
br i1 %75, label %76, label %30
76: ; preds = %73, %23
%77 = add nuw nsw i64 %24, 1
%78 = icmp eq i64 %77, %21
br i1 %78, label %79, label %23
79: ; preds = %76, %16
%80 = tail call i32 @free(double* %11)
ret i32 1
}