#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 cdb {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ cdb_read(struct cdb *, char *, int, unsigned int);
scalar_t__ memcmp(char *, char const *, int);
int match(struct cdb *c, const char *key, unsigned int len, unsigned int pos) {
  char buf[32];
  const int szb = sizeof buf;
  while (len > 0) {
    int n = (szb > len) ? len : szb;
    if (!cdb_read(c, buf, n, pos))
      return -1;
    if (memcmp(buf, key, n))
      return 0;
    pos += n;
    key += n;
    len -= n;
  }
  return 1;
}


x86 assembly (clang, -O0):
<match>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x20,-0x44(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jbe    11eb <match+0xcb>
mov    $0x20,%eax
cmp    -0x1c(%rbp),%eax
jbe    1160 <match+0x40>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    116d <match+0x4d>
mov    $0x20,%eax
mov    %eax,-0x4c(%rbp)
jmp    116d <match+0x4d>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1030 <cdb_read@plt>
cmp    $0x0,%eax
jne    119b <match+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <match+0xd2>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     11c1 <match+0xa1>
movl   $0x0,-0x4(%rbp)
jmp    11f2 <match+0xd2>
mov    -0x48(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x48(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x48(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    113d <match+0x1d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<match>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0x1,%eax
test   %edx,%edx
je     11a3 <match+0x83>
mov    %ecx,%r15d
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %rdi,%r12
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
nop
cmp    $0x20,%ebx
mov    $0x20,%r14d
cmovb  %ebx,%r14d
mov    %r12,%rdi
mov    %r13,%rsi
mov    %r14d,%edx
mov    %r15d,%ecx
call   1030 <cdb_read@plt>
test   %eax,%eax
je     119a <match+0x7a>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
jne    11a1 <match+0x81>
add    %r14d,%r15d
mov    %r14d,%eax
add    %rax,%rbp
sub    %r14d,%ebx
jne    1150 <match+0x30>
mov    $0x1,%eax
jmp    11a3 <match+0x83>
mov    $0xffffffff,%eax
jmp    11a3 <match+0x83>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.cdb = type { i32 }
define i32 @match(%struct.cdb* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.cdb*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [32 x i8]
%11 = alloca i32
%12 = alloca i32
store %struct.cdb* %0, %struct.cdb** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 32, i32* %11
br label %13
13: ; preds = %38, %4
%14 = load i32, i32* %8
%15 = icmp ugt i32 %14, 0
br i1 %15, label %16, label %49
16: ; preds = %13
%17 = load i32, i32* %8
%18 = icmp ugt i32 32, %17
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = load i32, i32* %8
br label %22
21: ; preds = %16
br label %22
22: ; preds = %21, %19
%23 = phi i32 [ %20, %19 ], [ 32, %21 ]
store i32 %23, i32* %12
%24 = load %struct.cdb*, %struct.cdb** %6
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%26 = load i32, i32* %12
%27 = load i32, i32* %9
%28 = call i32 @cdb_read(%struct.cdb* %24, i8* %25, i32 %26, i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %22
store i32 -1, i32* %5
br label %50
31: ; preds = %22
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%33 = load i8*, i8** %7
%34 = load i32, i32* %12
%35 = call i64 @memcmp(i8* %32, i8* %33, i32 %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 0, i32* %5
br label %50
38: ; preds = %31
%39 = load i32, i32* %12
%40 = load i32, i32* %9
%41 = add i32 %40, %39
store i32 %41, i32* %9
%42 = load i32, i32* %12
%43 = load i8*, i8** %7
%44 = sext i32 %42 to i64
%45 = getelementptr inbounds i8, i8* %43, i64 %44
store i8* %45, i8** %7
%46 = load i32, i32* %12
%47 = load i32, i32* %8
%48 = sub i32 %47, %46
store i32 %48, i32* %8
br label %13
49: ; preds = %13
store i32 1, i32* %5
br label %50
50: ; preds = %49, %37, %30
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
%struct.cdb = type { i32 }
define i32 @match(%struct.cdb* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [32 x i8]
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%7 = icmp eq i32 %2, 0
br i1 %7, label %25, label %8
8: ; preds = %4, %19
%9 = phi i8* [ %22, %19 ], [ %1, %4 ]
%10 = phi i32 [ %20, %19 ], [ %3, %4 ]
%11 = phi i32 [ %23, %19 ], [ %2, %4 ]
%12 = icmp ult i32 %11, 32
%13 = select i1 %12, i32 %11, i32 32
%14 = call i32 @cdb_read(%struct.cdb* %0, i8* %6, i32 %13, i32 %10)
%15 = icmp eq i32 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %8
%17 = call i64 @memcmp(i8* %6, i8* %9, i32 %13)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %16
%20 = add i32 %13, %10
%21 = zext i32 %13 to i64
%22 = getelementptr inbounds i8, i8* %9, i64 %21
%23 = sub i32 %11, %13
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %8
25: ; preds = %19, %8, %16, %4
%26 = phi i32 [ 1, %4 ], [ 0, %16 ], [ -1, %8 ], [ 1, %19 ]
ret i32 %26
}