#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*/ ext2_test_bit(unsigned char *, int);
int find_first_zero(void *addr, unsigned int offset, unsigned int max) {
  unsigned char *v = addr;
  int i = offset;
  while (i < max - 1) {
    if (!ext2_test_bit(v, i))
      return i;
    i++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<find_first_zero>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jae    116f <find_first_zero+0x5f>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <ext2_test_bit@plt>
cmp    $0x0,%eax
jne    1161 <find_first_zero+0x51>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1176 <find_first_zero+0x66>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1130 <find_first_zero+0x20>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_first_zero>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
add    $0xffffffff,%ebx
xor    %r14d,%r14d
cmp    %esi,%ebx
jbe    114a <find_first_zero+0x3a>
mov    %esi,%ebp
mov    %rdi,%r15
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebp,%esi
call   1030 <ext2_test_bit@plt>
test   %eax,%eax
je     1147 <find_first_zero+0x37>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    1130 <find_first_zero+0x20>
jmp    114a <find_first_zero+0x3a>
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @find_first_zero(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
store i8* %10, i8** %8
%11 = load i32, i32* %6
store i32 %11, i32* %9
br label %12
12: ; preds = %24, %3
%13 = load i32, i32* %9
%14 = load i32, i32* %7
%15 = sub i32 %14, 1
%16 = icmp ult i32 %13, %15
br i1 %16, label %17, label %27
17: ; preds = %12
%18 = load i8*, i8** %8
%19 = load i32, i32* %9
%20 = call i32 @ext2_test_bit(i8* %18, i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = load i32, i32* %9
store i32 %23, i32* %4
br label %28
24: ; preds = %17
%25 = load i32, i32* %9
%26 = add nsw i32 %25, 1
store i32 %26, i32* %9
br label %12
27: ; preds = %12
store i32 0, i32* %4
br label %28
28: ; preds = %27, %22
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
define i32 @find_first_zero(i8* %0, i32 %1, i32 %2) {
%4 = add i32 %2, -1
%5 = icmp ugt i32 %4, %1
br i1 %5, label %6, label %13
6: ; preds = %3, %10
%7 = phi i32 [ %11, %10 ], [ %1, %3 ]
%8 = tail call i32 @ext2_test_bit(i8* %0, i32 %7)
%9 = icmp eq i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = add i32 %7, 1
%12 = icmp eq i32 %11, %4
br i1 %12, label %13, label %6
13: ; preds = %6, %10, %3
%14 = phi i32 [ 0, %3 ], [ 0, %10 ], [ %7, %6 ]
ret i32 %14
}