#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 */
struct carray {
  int dummy;
};

/* Variables and functions */
void *carray_at(struct carray *, int);
int carray_count(struct carray *);
void *carray_first_match(struct carray *a, int (*match_fn)(void *, void *),
                         void *arg) {
  int i;
  void *p;
  for (i = 0; i < carray_count(a); i++) {
    p = carray_at(a, i);
    if (match_fn(arg, p))
      return p;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<carray_first_match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <carray_count@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
cmp    %ecx,%eax
jge    119e <carray_first_match+0x7e>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1040 <carray_at@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
je     118b <carray_first_match+0x6b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a6 <carray_first_match+0x86>
jmp    1190 <carray_first_match+0x70>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113b <carray_first_match+0x1b>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<carray_first_match>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
call   1030 <carray_count@plt>
xor    %r14d,%r14d
test   %eax,%eax
jle    117e <carray_first_match+0x5e>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %ebp,%esi
call   1040 <carray_at@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
call   *%r12
test   %eax,%eax
jne    117b <carray_first_match+0x5b>
add    $0x1,%ebp
mov    %r13,%rdi
call   1030 <carray_count@plt>
cmp    %eax,%ebp
jl     1150 <carray_first_match+0x30>
jmp    117e <carray_first_match+0x5e>
mov    %rbx,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.carray = type { i32 }
define i8* @carray_first_match(%struct.carray* %0, i32 (i8*, i8*)* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca %struct.carray*
%6 = alloca i32 (i8*, i8*)*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store %struct.carray* %0, %struct.carray** %5
store i32 (i8*, i8*)* %1, i32 (i8*, i8*)** %6
store i8* %2, i8** %7
store i32 0, i32* %8
br label %10
10: ; preds = %27, %3
%11 = load i32, i32* %8
%12 = load %struct.carray*, %struct.carray** %5
%13 = call i32 @carray_count(%struct.carray* %12)
%14 = icmp slt i32 %11, %13
br i1 %14, label %15, label %30
15: ; preds = %10
%16 = load %struct.carray*, %struct.carray** %5
%17 = load i32, i32* %8
%18 = call i8* @carray_at(%struct.carray* %16, i32 %17)
store i8* %18, i8** %9
%19 = load i32 (i8*, i8*)*, i32 (i8*, i8*)** %6
%20 = load i8*, i8** %7
%21 = load i8*, i8** %9
%22 = call i32 %19(i8* %20, i8* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = load i8*, i8** %9
store i8* %25, i8** %4
br label %31
26: ; preds = %15
br label %27
27: ; preds = %26
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %10
30: ; preds = %10
store i8* null, i8** %4
br label %31
31: ; preds = %30, %24
%32 = load i8*, i8** %4
ret i8* %32
}

LLVM IR (-O2):
%struct.carray = type { i32 }
define i8* @carray_first_match(%struct.carray* %0, i32 (i8*, i8*)* nocapture %1, i8* %2) {
%4 = tail call i32 @carray_count(%struct.carray* %0)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %9, label %15
6: ; preds = %9
%7 = tail call i32 @carray_count(%struct.carray* %0)
%8 = icmp slt i32 %14, %7
br i1 %8, label %9, label %15
9: ; preds = %3, %6
%10 = phi i32 [ %14, %6 ], [ 0, %3 ]
%11 = tail call i8* @carray_at(%struct.carray* %0, i32 %10)
%12 = tail call i32 %1(i8* %2, i8* %11)
%13 = icmp eq i32 %12, 0
%14 = add nuw nsw i32 %10, 1
br i1 %13, label %6, label %15
15: ; preds = %9, %6, %3
%16 = phi i8* [ null, %3 ], [ null, %6 ], [ %11, %9 ]
ret i8* %16
}