#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 *);
int carray_count_match(struct carray *a, int (*match_fn)(void *, void *),
                       void *arg) {
  int i, count = 0;
  for (i = 0; i < carray_count(a); i++)
    if (match_fn(arg, carray_at(a, i)))
      count++;
  return count;
}


x86 assembly (clang, -O0):
<carray_count_match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <carray_count@plt>
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    11ac <carray_count_match+0x8c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <carray_at@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
mov    -0x30(%rbp),%rax
call   *%rax
cmp    $0x0,%eax
je     1199 <carray_count_match+0x79>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    119e <carray_count_match+0x7e>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1142 <carray_count_match+0x22>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<carray_count_match>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1030 <carray_count@plt>
xor    %ebp,%ebp
test   %eax,%eax
jle    1168 <carray_count_match+0x48>
xor    %ebx,%ebx
xchg   %ax,%ax
mov    %r12,%rdi
mov    %ebx,%esi
call   1040 <carray_at@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   *%r15
cmp    $0x1,%eax
sbb    $0xffffffff,%ebp
add    $0x1,%ebx
mov    %r12,%rdi
call   1030 <carray_count@plt>
cmp    %eax,%ebx
jl     1140 <carray_count_match+0x20>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.carray = type { i32 }
define i32 @carray_count_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 %6, label %17
6: ; preds = %3, %6
%7 = phi i32 [ %13, %6 ], [ 0, %3 ]
%8 = phi i32 [ %14, %6 ], [ 0, %3 ]
%9 = tail call i8* @carray_at(%struct.carray* %0, i32 %8)
%10 = tail call i32 %1(i8* %2, i8* %9)
%11 = icmp ne i32 %10, 0
%12 = zext i1 %11 to i32
%13 = add nuw nsw i32 %7, %12
%14 = add nuw nsw i32 %8, 1
%15 = tail call i32 @carray_count(%struct.carray* %0)
%16 = icmp slt i32 %14, %15
br i1 %16, label %6, label %17
17: ; preds = %6, %3
%18 = phi i32 [ 0, %3 ], [ %13, %6 ]
ret i32 %18
}