#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 /*<<< orphan*/ block_read(int, char *);
int /*<<< orphan*/ free(char *);
scalar_t__ get_block_icon(char *);
scalar_t__ malloc(int);
int get_first_empty_block_from(int i) {
  char *buffer = (char *)malloc(4096);
  for (; i < 8192; i++) {
    block_read(i, buffer);
    if (get_block_icon(buffer) == 0) {
      free(buffer);
      return i;
    }
  }
  free(buffer);
  return -1;
}


x86 assembly (clang, -O0):
<get_first_empty_block_from>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x2000,-0x8(%rbp)
jge    11ac <get_first_empty_block_from+0x6c>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1060 <block_read@plt>
mov    -0x10(%rbp),%rdi
call   1040 <get_block_icon@plt>
cmp    $0x0,%rax
jne    1199 <get_first_empty_block_from+0x59>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <get_first_empty_block_from+0x7c>
jmp    119e <get_first_empty_block_from+0x5e>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1159 <get_first_empty_block_from+0x19>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_first_empty_block_from>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    $0xffffffff,%r14d
cmp    $0x1fff,%ebp
jg     1197 <get_first_empty_block_from+0x57>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <block_read@plt>
mov    %rbx,%rdi
call   1040 <get_block_icon@plt>
test   %rax,%rax
je     1194 <get_first_empty_block_from+0x54>
add    $0x1,%ebp
cmp    $0x2000,%ebp
jne    1170 <get_first_empty_block_from+0x30>
jmp    1197 <get_first_empty_block_from+0x57>
mov    %ebp,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @get_first_empty_block_from(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
%5 = call i64 @malloc(i32 4096)
%6 = inttoptr i64 %5 to i8*
store i8* %6, i8** %4
br label %7
7: ; preds = %22, %1
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 8192
br i1 %9, label %10, label %25
10: ; preds = %7
%11 = load i32, i32* %3
%12 = load i8*, i8** %4
%13 = call i32 @block_read(i32 %11, i8* %12)
%14 = load i8*, i8** %4
%15 = call i64 @get_block_icon(i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = load i8*, i8** %4
%19 = call i32 @free(i8* %18)
%20 = load i32, i32* %3
store i32 %20, i32* %2
br label %28
21: ; preds = %10
br label %22
22: ; preds = %21
%23 = load i32, i32* %3
%24 = add nsw i32 %23, 1
store i32 %24, i32* %3
br label %7
25: ; preds = %7
%26 = load i8*, i8** %4
%27 = call i32 @free(i8* %26)
store i32 -1, i32* %2
br label %28
28: ; preds = %25, %17
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
define i32 @get_first_empty_block_from(i32 %0) {
%2 = tail call i64 @malloc(i32 4096)
%3 = inttoptr i64 %2 to i8*
%4 = icmp slt i32 %0, 8192
br i1 %4, label %5, label %13
5: ; preds = %1, %10
%6 = phi i32 [ %11, %10 ], [ %0, %1 ]
%7 = tail call i32 @block_read(i32 %6, i8* %3)
%8 = tail call i64 @get_block_icon(i8* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %5
%11 = add nsw i32 %6, 1
%12 = icmp eq i32 %11, 8192
br i1 %12, label %13, label %5
13: ; preds = %10, %5, %1
%14 = phi i32 [ -1, %1 ], [ %6, %5 ], [ -1, %10 ]
%15 = tail call i32 @free(i8* %3)
ret i32 %14
}