#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 average_block_size(int const *, int);
int get_size(int const);
int /*<<< orphan*/ get_use_bit(int const);
int fragmentation_level(const int *mem) {
  int k, current_size;
  int frag_val = 0;
  float avg_alloc_size = average_block_size(mem, 1);
  k = 4;
  current_size = get_size(mem[k]);
  while (current_size != 0) {
    if (!get_use_bit(mem[k]) && (current_size >= (avg_alloc_size + 1))) {
      frag_val++;
    }
    k += current_size;
    current_size = get_size(mem[k]);
  }
  return frag_val;
}


x86 assembly (clang, -O0):
<fragmentation_level>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1040 <average_block_size@plt>
movss  %xmm0,-0x18(%rbp)
movl   $0x4,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1030 <get_size@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     11d8 <fragmentation_level+0xa8>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1050 <get_use_bit@plt>
cmp    $0x0,%eax
jne    11b7 <fragmentation_level+0x87>
cvtsi2ssl -0x10(%rbp),%xmm0
movss  0xe60(%rip),%xmm1
addss  -0x18(%rbp),%xmm1
ucomiss %xmm1,%xmm0
jb     11b7 <fragmentation_level+0x87>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1030 <get_size@plt>
mov    %eax,-0x10(%rbp)
jmp    1170 <fragmentation_level+0x40>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fragmentation_level>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x1,%esi
call   1040 <average_block_size@plt>
movss  %xmm0,0x4(%rsp)
mov    0x10(%r14),%edi
call   1030 <get_size@plt>
test   %eax,%eax
je     11be <fragmentation_level+0x8e>
mov    %eax,%ebx
movss  0x4(%rsp),%xmm0
addss  0xe99(%rip),%xmm0
movss  %xmm0,0x4(%rsp)
xor    %r15d,%r15d
mov    $0x4,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebp,%rbp
mov    (%r14,%rbp,4),%edi
call   1050 <get_use_bit@plt>
xorps  %xmm0,%xmm0
cvtsi2ss %ebx,%xmm0
test   %eax,%eax
sete   %al
ucomiss 0x4(%rsp),%xmm0
setae  %cl
and    %al,%cl
movzbl %cl,%eax
add    %eax,%r15d
add    %ebx,%ebp
movslq %ebp,%rax
mov    (%r14,%rax,4),%edi
call   1030 <get_size@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1180 <fragmentation_level+0x50>
jmp    11c1 <fragmentation_level+0x91>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fragmentation_level(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca float
store i32* %0, i32** %2
store i32 0, i32* %5
%7 = load i32*, i32** %2
%8 = call float @average_block_size(i32* %7, i32 1)
store float %8, float* %6
store i32 4, i32* %3
%9 = load i32*, i32** %2
%10 = load i32, i32* %3
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i32, i32* %9, i64 %11
%13 = load i32, i32* %12
%14 = call i32 @get_size(i32 %13)
store i32 %14, i32* %4
br label %15
15: ; preds = %35, %1
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %45
18: ; preds = %15
%19 = load i32*, i32** %2
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22
%24 = call i32 @get_use_bit(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %35, label %26
26: ; preds = %18
%27 = load i32, i32* %4
%28 = sitofp i32 %27 to float
%29 = load float, float* %6
%30 = fadd float %29, 1.000000e+00
%31 = fcmp oge float %28, %30
br i1 %31, label %32, label %35
32: ; preds = %26
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
store i32 %34, i32* %5
br label %35
35: ; preds = %32, %26, %18
%36 = load i32, i32* %4
%37 = load i32, i32* %3
%38 = add nsw i32 %37, %36
store i32 %38, i32* %3
%39 = load i32*, i32** %2
%40 = load i32, i32* %3
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32, i32* %39, i64 %41
%43 = load i32, i32* %42
%44 = call i32 @get_size(i32 %43)
store i32 %44, i32* %4
br label %15
45: ; preds = %15
%46 = load i32, i32* %5
ret i32 %46
}

LLVM IR (-O2):
define i32 @fragmentation_level(i32* %0) {
%2 = tail call float @average_block_size(i32* %0, i32 1)
%3 = getelementptr inbounds i32, i32* %0, i64 4
%4 = load i32, i32* %3
%5 = tail call i32 @get_size(i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %29, label %7
7: ; preds = %1
%8 = fadd float %2, 1.000000e+00
br label %9
9: ; preds = %7, %9
%10 = phi i32 [ 4, %7 ], [ %23, %9 ]
%11 = phi i32 [ 0, %7 ], [ %22, %9 ]
%12 = phi i32 [ %5, %7 ], [ %27, %9 ]
%13 = sext i32 %10 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = tail call i32 @get_use_bit(i32 %15)
%17 = icmp eq i32 %16, 0
%18 = sitofp i32 %12 to float
%19 = fcmp ole float %8, %18
%20 = select i1 %17, i1 %19, i1 false
%21 = zext i1 %20 to i32
%22 = add nuw nsw i32 %11, %21
%23 = add nsw i32 %10, %12
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %0, i64 %24
%26 = load i32, i32* %25
%27 = tail call i32 @get_size(i32 %26)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %9
29: ; preds = %9, %1
%30 = phi i32 [ 0, %1 ], [ %22, %9 ]
ret i32 %30
}