#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 genericLinearSearch(void *array, size_t elemSize, int lenArray, void *key,
                        int (*cmp)(void *a, void *b)) {
  for (int i = 0; i < lenArray; i++) {
    void *currentElem = array + (elemSize * i);
    if (cmp(currentElem, key) == 0)
      return i;
  }
  return -1;
}


x86 assembly (clang, -O0):
<genericLinearSearch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    117a <genericLinearSearch+0x7a>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x34(%rbp),%rdx
imul   %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jne    1167 <genericLinearSearch+0x67>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1181 <genericLinearSearch+0x81>
jmp    116c <genericLinearSearch+0x6c>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1122 <genericLinearSearch+0x22>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genericLinearSearch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%eax
test   %edx,%edx
jle    1152 <genericLinearSearch+0x52>
mov    %r8,%r15
mov    %rcx,%r12
mov    %rsi,%r13
mov    %rdi,%rbp
mov    %edx,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
mov    %r12,%rsi
call   *%r15
test   %eax,%eax
je     1150 <genericLinearSearch+0x50>
add    $0x1,%rbx
add    %r13,%rbp
cmp    %rbx,%r14
jne    1130 <genericLinearSearch+0x30>
mov    $0xffffffff,%eax
jmp    1152 <genericLinearSearch+0x52>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @genericLinearSearch(i8* %0, i64 %1, i32 %2, i8* %3, i32 (i8*, i8*)* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32 (i8*, i8*)*
%12 = alloca i32
%13 = alloca i8*
store i8* %0, i8** %7
store i64 %1, i64* %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32 (i8*, i8*)* %4, i32 (i8*, i8*)** %11
store i32 0, i32* %12
br label %14
14: ; preds = %33, %5
%15 = load i32, i32* %12
%16 = load i32, i32* %9
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %36
18: ; preds = %14
%19 = load i8*, i8** %7
%20 = load i64, i64* %8
%21 = load i32, i32* %12
%22 = sext i32 %21 to i64
%23 = mul i64 %20, %22
%24 = getelementptr i8, i8* %19, i64 %23
store i8* %24, i8** %13
%25 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %11
%26 = load i8*, i8** %13
%27 = load i8*, i8** %10
%28 = call i32 %25(i8* %26, i8* %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = load i32, i32* %12
store i32 %31, i32* %6
br label %37
32: ; preds = %18
br label %33
33: ; preds = %32
%34 = load i32, i32* %12
%35 = add nsw i32 %34, 1
store i32 %35, i32* %12
br label %14
36: ; preds = %14
store i32 -1, i32* %6
br label %37
37: ; preds = %36, %30
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
define i32 @genericLinearSearch(i8* %0, i64 %1, i32 %2, i8* %3, i32 (i8*, i8*)* nocapture %4) {
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %20
7: ; preds = %5
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %15
%10 = phi i64 [ 0, %7 ], [ %16, %15 ]
%11 = mul i64 %10, %1
%12 = getelementptr i8, i8* %0, i64 %11
%13 = tail call i32 %4(i8* %12, i8* %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = add nuw nsw i64 %10, 1
%17 = icmp eq i64 %16, %8
br i1 %17, label %20, label %9
18: ; preds = %9
%19 = trunc i64 %10 to i32
br label %20
20: ; preds = %15, %18, %5
%21 = phi i32 [ -1, %5 ], [ %19, %18 ], [ -1, %15 ]
ret i32 %21
}