#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*/ printf(char *, void *, ...);
int linearsearch_c(void *a, int size, int n, void *x,
                   int (*comp)(char *, char *)) {
  int i = 0;
  while (i < n) {
    if (comp((char *)a + i * size, x) == 0) {
      printf("a[%d] :      %p \n", i, (char *)a + i * size);
      printf("&x:      %p\n", x);
      return i;
    }
    i++;
  }
  return -1;
}


x86 assembly (clang, -O0):
<linearsearch_c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11ae <linearsearch_c+0x9e>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%ecx
imul   -0x14(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rdi
mov    -0x20(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jne    11a0 <linearsearch_c+0x90>
movslq -0x2c(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x2c(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
add    %rax,%rdx
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <linearsearch_c+0xa5>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1131 <linearsearch_c+0x21>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<linearsearch_c>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%eax
test   %edx,%edx
jle    1187 <linearsearch_c+0x77>
mov    %r8,%r12
mov    %rcx,%r14
mov    %rdi,%rbp
movslq %esi,%r13
mov    %edx,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
mov    %r14,%rsi
call   *%r12
test   %eax,%eax
je     1160 <linearsearch_c+0x50>
add    $0x1,%rbx
add    %r13,%rbp
cmp    %rbx,%r15
jne    1140 <linearsearch_c+0x30>
mov    $0xffffffff,%eax
jmp    1187 <linearsearch_c+0x77>
lea    0xe99(%rip),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe97(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"a[%d] : %p \0A\00"
@.str.1 = [13 x i8] c"&x: %p\0A\00"
define i32 @linearsearch_c(i8* %0, i32 %1, i32 %2, i8* %3, i32 (i8*, i8*)* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32 (i8*, i8*)*
%12 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %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 %13
13: ; preds = %42, %5
%14 = load i32, i32* %12
%15 = load i32, i32* %9
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %45
17: ; preds = %13
%18 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %11
%19 = load i8*, i8** %7
%20 = load i32, i32* %12
%21 = load i32, i32* %8
%22 = mul nsw i32 %20, %21
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %19, i64 %23
%25 = load i8*, i8** %10
%26 = call i32 %18(i8* %24, i8* %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %42
28: ; preds = %17
%29 = load i32, i32* %12
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i8*
%32 = load i8*, i8** %7
%33 = load i32, i32* %12
%34 = load i32, i32* %8
%35 = mul nsw i32 %33, %34
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %32, i64 %36
%38 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %31, i8* %37)
%39 = load i8*, i8** %10
%40 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = load i32, i32* %12
store i32 %41, i32* %6
br label %46
42: ; preds = %17
%43 = load i32, i32* %12
%44 = add nsw i32 %43, 1
store i32 %44, i32* %12
br label %13
45: ; preds = %13
store i32 -1, i32* %6
br label %46
46: ; preds = %45, %28
%47 = load i32, i32* %6
ret i32 %47
}

LLVM IR (-O2):
@.str = [18 x i8] c"a[%d] : %p \0A\00"
@.str.1 = [13 x i8] c"&x: %p\0A\00"
define i32 @linearsearch_c(i8* %0, i32 %1, i32 %2, i8* %3, i32 (i8*, i8*)* nocapture %4) {
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %25
7: ; preds = %5
%8 = sext i32 %1 to i64
%9 = zext i32 %2 to i64
br label %10
10: ; preds = %7, %22
%11 = phi i64 [ 0, %7 ], [ %23, %22 ]
%12 = mul nsw i64 %11, %8
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = tail call i32 %4(i8* %13, i8* %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %10
%17 = trunc i64 %11 to i32
%18 = and i64 %11, 4294967295
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %13)
%21 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %25
22: ; preds = %10
%23 = add nuw nsw i64 %11, 1
%24 = icmp eq i64 %23, %9
br i1 %24, label %25, label %10
25: ; preds = %22, %5, %16
%26 = phi i32 [ %17, %16 ], [ -1, %5 ], [ -1, %22 ]
ret i32 %26
}