#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 bitfield_get(char *, int);
double bitfield_percent(char *bitfield, int length) {
  int i;
  double percent = 0.0;
  for (i = 0; i < length; i++)
    if (bitfield_get(bitfield, i) == 1) {
      percent += (100.0) / (double)length;
    }
  return percent;
}


x86 assembly (clang, -O0):
<bitfield_percent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    117d <bitfield_percent+0x6d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1030 <bitfield_get@plt>
cmp    $0x1,%eax
jne    116a <bitfield_percent+0x5a>
cvtsi2sdl -0xc(%rbp),%xmm1
movsd  0xea4(%rip),%xmm0
divsd  %xmm1,%xmm0
addsd  -0x18(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
jmp    116f <bitfield_percent+0x5f>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112e <bitfield_percent+0x1e>
movsd  -0x18(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitfield_percent>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %esi,%esi
jle    1175 <bitfield_percent+0x65>
mov    %esi,%ebx
mov    %rdi,%r14
cvtsi2sd %esi,%xmm1
xor    %ebp,%ebp
xorpd  %xmm0,%xmm0
movsd  0xed5(%rip),%xmm2
divsd  %xmm1,%xmm2
movsd  %xmm2,(%rsp)
jmp    1151 <bitfield_percent+0x41>
xchg   %ax,%ax
movsd  0x8(%rsp),%xmm0
addsd  %xmm1,%xmm0
add    $0x1,%ebp
cmp    %ebp,%ebx
je     1179 <bitfield_percent+0x69>
movsd  %xmm0,0x8(%rsp)
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <bitfield_get@plt>
movsd  0xe97(%rip),%xmm1
cmp    $0x1,%eax
jne    1140 <bitfield_percent+0x30>
movsd  (%rsp),%xmm1
jmp    1140 <bitfield_percent+0x30>
xorpd  %xmm0,%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @bitfield_percent(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
store i8* %0, i8** %3
store i32 %1, i32* %4
store double 0.000000e+00, double* %6
store i32 0, i32* %5
br label %7
7: ; preds = %23, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %26
11: ; preds = %7
%12 = load i8*, i8** %3
%13 = load i32, i32* %5
%14 = call i32 @bitfield_get(i8* %12, i32 %13)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i32, i32* %4
%18 = sitofp i32 %17 to double
%19 = fdiv double 1.000000e+02, %18
%20 = load double, double* %6
%21 = fadd double %20, %19
store double %21, double* %6
br label %22
22: ; preds = %16, %11
br label %23
23: ; preds = %22
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %7
26: ; preds = %7
%27 = load double, double* %6
ret double %27
}

LLVM IR (-O2):
define double @bitfield_percent(i8* %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %16
4: ; preds = %2
%5 = sitofp i32 %1 to double
%6 = fdiv double 1.000000e+02, %5
br label %7
7: ; preds = %4, %7
%8 = phi double [ 0.000000e+00, %4 ], [ %13, %7 ]
%9 = phi i32 [ 0, %4 ], [ %14, %7 ]
%10 = tail call i32 @bitfield_get(i8* %0, i32 %9)
%11 = icmp eq i32 %10, 1
%12 = select i1 %11, double %6, double -0.000000e+00
%13 = fadd double %8, %12
%14 = add nuw nsw i32 %9, 1
%15 = icmp eq i32 %14, %1
br i1 %15, label %16, label %7
16: ; preds = %7, %2
%17 = phi double [ 0.000000e+00, %2 ], [ %13, %7 ]
ret double %17
}