#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 get_f_link(int *, int);
int get_size(int);
int /*<<< orphan*/ partition_memory(int *, int, int, int);
int search_for_fpart(int *, int, int);
int allocate(int *mem, int k) {
  if (k < 3) {
    k = 3;
  }
  static int next_fpart = 0;
  int current_fpart = next_fpart;
  next_fpart = search_for_fpart(mem, k, next_fpart);
  if (-1 == next_fpart) {
    next_fpart = current_fpart;
    return -1;
  }
  current_fpart = next_fpart;
  next_fpart = get_f_link(mem, current_fpart);
  int psize = get_size(mem[current_fpart]);
  if (psize < (k + 5)) {
    partition_memory(mem, current_fpart, psize, 0);
  } else {
    partition_memory(mem, current_fpart, (k + 1), 1);
  }
  return current_fpart;
}


x86 assembly (clang, -O0):
<allocate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    1160 <allocate+0x20>
movl   $0x3,-0x14(%rbp)
mov    0x2ede(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    0x2ece(%rip),%edx
call   1050 <search_for_fpart@plt>
mov    %eax,0x2ec3(%rip)
mov    $0xffffffff,%eax
cmp    0x2eb8(%rip),%eax
jne    11a7 <allocate+0x67>
mov    -0x18(%rbp),%eax
mov    %eax,0x2ea9(%rip)
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <allocate+0xd9>
mov    0x2e97(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <get_f_link@plt>
mov    %eax,0x2e82(%rip)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1030 <get_size@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x14(%rbp),%ecx
add    $0x5,%ecx
cmp    %ecx,%eax
jge    11fc <allocate+0xbc>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1040 <partition_memory@plt>
jmp    1213 <allocate+0xd3>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
mov    $0x1,%ecx
call   1040 <partition_memory@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
cmp    $0x4,%esi
mov    $0x3,%ebx
cmovge %esi,%ebx
mov    0x2ee8(%rip),%r15d
mov    %ebx,%esi
mov    %r15d,%edx
call   1050 <search_for_fpart@plt>
mov    %eax,%ebp
mov    %eax,0x2ed6(%rip)
cmp    $0xffffffff,%eax
je     11a1 <allocate+0x61>
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <get_f_link@plt>
mov    %eax,0x2ec1(%rip)
movslq %ebp,%rax
mov    (%r14,%rax,4),%edi
call   1030 <get_size@plt>
lea    0x5(%rbx),%ecx
cmp    %ecx,%eax
jge    11aa <allocate+0x6a>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %ecx,%ecx
jmp    11b9 <allocate+0x79>
mov    %r15d,0x2e9c(%rip)
jmp    11be <allocate+0x7e>
add    $0x1,%ebx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    $0x1,%ecx
call   1040 <partition_memory@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@allocate.next_fpart = internal global i32 0
define i32 @allocate(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp slt i32 %8, 3
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 3, i32* %5
br label %11
11: ; preds = %10, %2
%12 = load i32, i32* @allocate.next_fpart
store i32 %12, i32* %6
%13 = load i32*, i32** %4
%14 = load i32, i32* %5
%15 = load i32, i32* @allocate.next_fpart
%16 = call i32 @search_for_fpart(i32* %13, i32 %14, i32 %15)
store i32 %16, i32* @allocate.next_fpart
%17 = load i32, i32* @allocate.next_fpart
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = load i32, i32* %6
store i32 %20, i32* @allocate.next_fpart
store i32 -1, i32* %3
br label %49
21: ; preds = %11
%22 = load i32, i32* @allocate.next_fpart
store i32 %22, i32* %6
%23 = load i32*, i32** %4
%24 = load i32, i32* %6
%25 = call i32 @get_f_link(i32* %23, i32 %24)
store i32 %25, i32* @allocate.next_fpart
%26 = load i32*, i32** %4
%27 = load i32, i32* %6
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32, i32* %29
%31 = call i32 @get_size(i32 %30)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 5
%35 = icmp slt i32 %32, %34
br i1 %35, label %36, label %41
36: ; preds = %21
%37 = load i32*, i32** %4
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = call i32 @partition_memory(i32* %37, i32 %38, i32 %39, i32 0)
br label %47
41: ; preds = %21
%42 = load i32*, i32** %4
%43 = load i32, i32* %6
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
%46 = call i32 @partition_memory(i32* %42, i32 %43, i32 %45, i32 1)
br label %47
47: ; preds = %41, %36
%48 = load i32, i32* %6
store i32 %48, i32* %3
br label %49
49: ; preds = %47, %19
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@allocate.next_fpart = internal unnamed_addr global i32 0
define i32 @allocate(i32* %0, i32 %1) {
%3 = icmp sgt i32 %1, 3
%4 = select i1 %3, i32 %1, i32 3
%5 = load i32, i32* @allocate.next_fpart
%6 = tail call i32 @search_for_fpart(i32* %0, i32 %4, i32 %5)
store i32 %6, i32* @allocate.next_fpart
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %9
8: ; preds = %2
store i32 %5, i32* @allocate.next_fpart
br label %22
9: ; preds = %2
%10 = tail call i32 @get_f_link(i32* %0, i32 %6)
store i32 %10, i32* @allocate.next_fpart
%11 = sext i32 %6 to i64
%12 = getelementptr inbounds i32, i32* %0, i64 %11
%13 = load i32, i32* %12
%14 = tail call i32 @get_size(i32 %13)
%15 = add nuw nsw i32 %4, 5
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %9
%18 = tail call i32 @partition_memory(i32* %0, i32 %6, i32 %14, i32 0)
br label %22
19: ; preds = %9
%20 = add nuw nsw i32 %4, 1
%21 = tail call i32 @partition_memory(i32* %0, i32 %6, i32 %20, i32 1)
br label %22
22: ; preds = %17, %19, %8
ret i32 %6
}