#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_block_map_bit_status(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ set_block_map_bit_status(int, int);
int get_free_blocks(int size) {
  if (size <= 0) {
    printf("size illegal!size:%d\n", size);
    return -1;
  }
  int expected_block_num = (size - 1) / 128 + 1;
  int offset = 0, tmp = 0;
  while (offset < 64 * 32) {
    if (get_block_map_bit_status(offset) == 0) {
      for (tmp = 0; tmp < expected_block_num; tmp++) {
        if (get_block_map_bit_status(offset + tmp) == 1)
          break;
      }
      if (tmp == expected_block_num)
        break;
      else
        offset += tmp;
    } else
      offset++;
  }
  if (offset >= 64 * 32) {
    printf("not enough memory!\n");
    return -1;
  }
  for (tmp = 0; tmp < expected_block_num; tmp++)
    set_block_map_bit_status(offset + tmp, 1);
  return offset;
}


x86 assembly (clang, -O0):
<get_free_blocks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jg     1162 <get_free_blocks+0x32>
mov    -0x8(%rbp),%esi
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <get_free_blocks+0x13c>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    $0x80,%ecx
cltd
idiv   %ecx
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x800,-0x10(%rbp)
jge    120e <get_free_blocks+0xde>
mov    -0x10(%rbp),%edi
call   1050 <get_block_map_bit_status@plt>
cmp    $0x0,%eax
jne    1200 <get_free_blocks+0xd0>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11e1 <get_free_blocks+0xb1>
mov    -0x10(%rbp),%edi
add    -0x14(%rbp),%edi
call   1050 <get_block_map_bit_status@plt>
cmp    $0x1,%eax
jne    11ce <get_free_blocks+0x9e>
jmp    11e1 <get_free_blocks+0xb1>
jmp    11d3 <get_free_blocks+0xa3>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11a9 <get_free_blocks+0x79>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jne    11f2 <get_free_blocks+0xc2>
jmp    120e <get_free_blocks+0xde>
mov    -0x14(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1209 <get_free_blocks+0xd9>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1184 <get_free_blocks+0x54>
cmpl   $0x800,-0x10(%rbp)
jl     1235 <get_free_blocks+0x105>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126c <get_free_blocks+0x13c>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1266 <get_free_blocks+0x136>
mov    -0x10(%rbp),%edi
add    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1040 <set_block_map_bit_status@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    123c <get_free_blocks+0x10c>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_free_blocks>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
test   %edi,%edi
jle    11bb <get_free_blocks+0x7b>
add    $0xffffffff,%r14d
shr    $0x7,%r14d
add    $0x1,%r14d
xor    %ebp,%ebp
jmp    1171 <get_free_blocks+0x31>
nopl   0x0(%rax,%rax,1)
cmp    %ebx,%r14d
je     11a5 <get_free_blocks+0x65>
add    %ebp,%ebx
mov    %ebx,%ebp
cmp    $0x800,%ebx
jae    11ad <get_free_blocks+0x6d>
mov    %ebp,%edi
call   1060 <get_block_map_bit_status@plt>
mov    $0x1,%ebx
test   %eax,%eax
jne    1165 <get_free_blocks+0x25>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%rbx,%rbp,1),%edi
call   1060 <get_block_map_bit_status@plt>
cmp    $0x1,%eax
je     1160 <get_free_blocks+0x20>
add    $0x1,%ebx
cmp    %ebx,%r14d
jne    1190 <get_free_blocks+0x50>
cmp    $0x7ff,%ebp
jbe    11d3 <get_free_blocks+0x93>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
jmp    11cc <get_free_blocks+0x8c>
lea    0xe3e(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
jmp    11f5 <get_free_blocks+0xb5>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%rbx,%rbp,1),%edi
mov    $0x1,%esi
call   1050 <set_block_map_bit_status@plt>
add    $0x1,%ebx
cmp    %ebx,%r14d
jne    11e0 <get_free_blocks+0xa0>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"size illegal!size:%d\0A\00"
@.str.1 = [20 x i8] c"not enough memory!\0A\00"
define i32 @get_free_blocks(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = icmp sle i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* %3
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 -1, i32* %2
br label %74
12: ; preds = %1
%13 = load i32, i32* %3
%14 = sub nsw i32 %13, 1
%15 = sdiv i32 %14, 128
%16 = add nsw i32 %15, 1
store i32 %16, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
br label %17
17: ; preds = %53, %12
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 2048
br i1 %19, label %20, label %54
20: ; preds = %17
%21 = load i32, i32* %5
%22 = call i32 @get_block_map_bit_status(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %50
24: ; preds = %20
store i32 0, i32* %6
br label %25
25: ; preds = %37, %24
%26 = load i32, i32* %6
%27 = load i32, i32* %4
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %40
29: ; preds = %25
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = add nsw i32 %30, %31
%33 = call i32 @get_block_map_bit_status(i32 %32)
%34 = icmp eq i32 %33, 1
br i1 %34, label %35, label %36
35: ; preds = %29
br label %40
36: ; preds = %29
br label %37
37: ; preds = %36
%38 = load i32, i32* %6
%39 = add nsw i32 %38, 1
store i32 %39, i32* %6
br label %25
40: ; preds = %35, %25
%41 = load i32, i32* %6
%42 = load i32, i32* %4
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40
br label %54
45: ; preds = %40
%46 = load i32, i32* %6
%47 = load i32, i32* %5
%48 = add nsw i32 %47, %46
store i32 %48, i32* %5
br label %49
49: ; preds = %45
br label %53
50: ; preds = %20
%51 = load i32, i32* %5
%52 = add nsw i32 %51, 1
store i32 %52, i32* %5
br label %53
53: ; preds = %50, %49
br label %17
54: ; preds = %44, %17
%55 = load i32, i32* %5
%56 = icmp sge i32 %55, 2048
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %74
59: ; preds = %54
store i32 0, i32* %6
br label %60
60: ; preds = %69, %59
%61 = load i32, i32* %6
%62 = load i32, i32* %4
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %72
64: ; preds = %60
%65 = load i32, i32* %5
%66 = load i32, i32* %6
%67 = add nsw i32 %65, %66
%68 = call i32 @set_block_map_bit_status(i32 %67, i32 1)
br label %69
69: ; preds = %64
%70 = load i32, i32* %6
%71 = add nsw i32 %70, 1
store i32 %71, i32* %6
br label %60
72: ; preds = %60
%73 = load i32, i32* %5
store i32 %73, i32* %2
br label %74
74: ; preds = %72, %57, %9
%75 = load i32, i32* %2
ret i32 %75
}

LLVM IR (-O2):
@.str = [22 x i8] c"size illegal!size:%d\0A\00"
@str = [19 x i8] c"not enough memory!\00"
define i32 @get_free_blocks(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %37
5: ; preds = %1
%6 = add nsw i32 %0, -1
%7 = lshr i32 %6, 7
%8 = add nuw nsw i32 %7, 1
br label %9
9: ; preds = %5, %23
%10 = phi i32 [ 0, %5 ], [ %25, %23 ]
%11 = tail call i32 @get_block_map_bit_status(i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %23
13: ; preds = %9, %18
%14 = phi i32 [ %19, %18 ], [ 0, %9 ]
%15 = add nuw nsw i32 %14, %10
%16 = tail call i32 @get_block_map_bit_status(i32 %15)
%17 = icmp eq i32 %16, 1
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = add nuw nsw i32 %14, 1
%20 = icmp eq i32 %14, %7
br i1 %20, label %27, label %13
21: ; preds = %13
%22 = icmp eq i32 %14, %8
br i1 %22, label %27, label %23
23: ; preds = %9, %21
%24 = phi i32 [ %14, %21 ], [ 1, %9 ]
%25 = add nuw nsw i32 %24, %10
%26 = icmp ult i32 %25, 2048
br i1 %26, label %9, label %29
27: ; preds = %21, %18
%28 = icmp ugt i32 %10, 2047
br i1 %28, label %29, label %31
29: ; preds = %23, %27
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %37
31: ; preds = %27, %31
%32 = phi i32 [ %35, %31 ], [ 0, %27 ]
%33 = add nuw nsw i32 %32, %10
%34 = tail call i32 @set_block_map_bit_status(i32 %33, i32 1)
%35 = add nuw nsw i32 %32, 1
%36 = icmp eq i32 %32, %7
br i1 %36, label %37, label %31
37: ; preds = %31, %29, %3
%38 = phi i32 [ -1, %3 ], [ -1, %29 ], [ %10, %31 ]
ret i32 %38
}