#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 calculate_violation_storage(int *, int);
int /*<<< orphan*/ printf(char *);
void add_a_optimal_config(int *goal, int *goal_num, int *option,
                          int option_num) {
  int i;
  float optimal_value = 999999999;
  int optimal_index = 0;
  int gn = *goal_num;
  for (i = 0; i < option_num; i++) {
    goal[gn] = option[i];
    int v = calculate_violation_storage(goal, gn);
    if (v < optimal_value) {
      optimal_index = i;
      optimal_value = v;
    }
  }
  if (optimal_index == -1)
    printf("ERROR");
  else
    goal[gn++] = option[optimal_index];
  *goal_num = gn;
}


x86 assembly (clang, -O0):
<add_a_optimal_config>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movss  0xec1(%rip),%xmm0
movss  %xmm0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11c2 <add_a_optimal_config+0xa2>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <calculate_violation_storage@plt>
mov    %eax,-0x30(%rbp)
cvtsi2ssl -0x30(%rbp),%xmm1
movss  -0x24(%rbp),%xmm0
ucomiss %xmm1,%xmm0
jbe    11af <add_a_optimal_config+0x8f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
cvtsi2ssl -0x30(%rbp),%xmm0
movss  %xmm0,-0x24(%rbp)
jmp    11b4 <add_a_optimal_config+0x94>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    115b <add_a_optimal_config+0x3b>
cmpl   $0xffffffff,-0x28(%rbp)
jne    11dd <add_a_optimal_config+0xbd>
lea    0xe31(%rip),%rdi
call   1030 <printf@plt>
jmp    11fd <add_a_optimal_config+0xdd>
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_a_optimal_config>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rsi),%eax
movslq %eax,%rbx
test   %ecx,%ecx
jle    11b5 <add_a_optimal_config+0x95>
mov    %eax,0x8(%rsp)
mov    %r14,0x10(%rsp)
mov    %ecx,%r14d
movss  0xeac(%rip),%xmm0
xor    %r13d,%r13d
xor    %ebp,%ebp
nopl   0x0(%rax)
movss  %xmm0,0xc(%rsp)
mov    (%r12,%r13,4),%eax
mov    %eax,(%r15,%rbx,4)
mov    %r15,%rdi
mov    %ebx,%esi
call   1040 <calculate_violation_storage@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
movss  0xc(%rsp),%xmm1
ucomiss %xmm0,%xmm1
cmova  %r13d,%ebp
minss  %xmm1,%xmm0
add    $0x1,%r13
cmp    %r13,%r14
jne    1160 <add_a_optimal_config+0x40>
cmp    $0xffffffff,%ebp
mov    0x10(%rsp),%r14
jne    11b7 <add_a_optimal_config+0x97>
lea    0xe5a(%rip),%rdi
call   1030 <printf@plt>
mov    0x8(%rsp),%ecx
jmp    11c7 <add_a_optimal_config+0xa7>
xor    %ebp,%ebp
movslq %ebp,%rax
mov    (%r12,%rax,4),%eax
mov    %ebx,%ecx
add    $0x1,%ecx
mov    %eax,(%r15,%rbx,4)
mov    %ecx,(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"ERROR\00"
define void @add_a_optimal_config(i32* %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca float
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32 %3, i32* %8
store float 1.000000e+09, float* %10
store i32 0, i32* %11
%14 = load i32*, i32** %6
%15 = load i32, i32* %14
store i32 %15, i32* %12
store i32 0, i32* %9
br label %16
16: ; preds = %42, %4
%17 = load i32, i32* %9
%18 = load i32, i32* %8
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %45
20: ; preds = %16
%21 = load i32*, i32** %7
%22 = load i32, i32* %9
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %24
%26 = load i32*, i32** %5
%27 = load i32, i32* %12
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
store i32 %25, i32* %29
%30 = load i32*, i32** %5
%31 = load i32, i32* %12
%32 = call i32 @calculate_violation_storage(i32* %30, i32 %31)
store i32 %32, i32* %13
%33 = load i32, i32* %13
%34 = sitofp i32 %33 to float
%35 = load float, float* %10
%36 = fcmp olt float %34, %35
br i1 %36, label %37, label %41
37: ; preds = %20
%38 = load i32, i32* %9
store i32 %38, i32* %11
%39 = load i32, i32* %13
%40 = sitofp i32 %39 to float
store float %40, float* %10
br label %41
41: ; preds = %37, %20
br label %42
42: ; preds = %41
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
store i32 %44, i32* %9
br label %16
45: ; preds = %16
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %61
50: ; preds = %45
%51 = load i32*, i32** %7
%52 = load i32, i32* %11
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %51, i64 %53
%55 = load i32, i32* %54
%56 = load i32*, i32** %5
%57 = load i32, i32* %12
%58 = add nsw i32 %57, 1
store i32 %58, i32* %12
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i32, i32* %56, i64 %59
store i32 %55, i32* %60
br label %61
61: ; preds = %50, %48
%62 = load i32, i32* %12
%63 = load i32*, i32** %6
store i32 %62, i32* %63
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"ERROR\00"
define void @add_a_optimal_config(i32* %0, i32* nocapture %1, i32* nocapture %2, i32 %3) {
%5 = load i32, i32* %1
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %29
7: ; preds = %4
%8 = sext i32 %5 to i64
%9 = getelementptr inbounds i32, i32* %0, i64 %8
%10 = zext i32 %3 to i64
br label %11
11: ; preds = %7, %11
%12 = phi i64 [ 0, %7 ], [ %23, %11 ]
%13 = phi i32 [ 0, %7 ], [ %22, %11 ]
%14 = phi float [ 1.000000e+09, %7 ], [ %20, %11 ]
%15 = getelementptr inbounds i32, i32* %2, i64 %12
%16 = load i32, i32* %15
store i32 %16, i32* %9
%17 = tail call i32 @calculate_violation_storage(i32* %0, i32 %5)
%18 = sitofp i32 %17 to float
%19 = fcmp ogt float %14, %18
%20 = select i1 %19, float %18, float %14
%21 = trunc i64 %12 to i32
%22 = select i1 %19, i32 %21, i32 %13
%23 = add nuw nsw i64 %12, 1
%24 = icmp eq i64 %23, %10
br i1 %24, label %25, label %11
25: ; preds = %11
%26 = icmp eq i32 %22, -1
br i1 %26, label %27, label %29
27: ; preds = %25
%28 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %37
29: ; preds = %4, %25
%30 = phi i32 [ %22, %25 ], [ 0, %4 ]
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %2, i64 %31
%33 = load i32, i32* %32
%34 = add nsw i32 %5, 1
%35 = sext i32 %5 to i64
%36 = getelementptr inbounds i32, i32* %0, i64 %35
store i32 %33, i32* %36
br label %37
37: ; preds = %29, %27
%38 = phi i32 [ %5, %27 ], [ %34, %29 ]
store i32 %38, i32* %1
ret void
}