#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 bx_unicode_sqlite3StrNICmp(unsigned char const *, unsigned char const *,
                               int);
int bx_unicode_sqlite3StrNICmp16(void const *, void const *, int);
extern int bx_unicode_sqlite3_unicode_collate(void *encoding, int nKey1,
                                              const void *pKey1, int nKey2,
                                              const void *pKey2) {
  int r = 0;
  if ((void *)1 == encoding)
    r = bx_unicode_sqlite3StrNICmp((const unsigned char *)pKey1,
                                   (const unsigned char *)pKey2,
                                   (nKey1 < nKey2) ? nKey1 : nKey2);
  else if ((void *)4 == encoding)
    r = bx_unicode_sqlite3StrNICmp16((const void *)pKey1, (const void *)pKey2,
                                     (nKey1 < nKey2) ? nKey1 : nKey2);
  if (0 == r) {
    r = nKey1 - nKey2;
  }
  return r;
}


x86 assembly (clang, -O0):
<bx_unicode_sqlite3_unicode_collate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    $0x1,%eax
cmp    -0x8(%rbp),%rax
jne    1195 <bx_unicode_sqlite3_unicode_collate+0x75>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1177 <bx_unicode_sqlite3_unicode_collate+0x57>
mov    -0xc(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    117d <bx_unicode_sqlite3_unicode_collate+0x5d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%edx
call   1040 <bx_unicode_sqlite3StrNICmp@plt>
mov    %eax,-0x2c(%rbp)
jmp    11e9 <bx_unicode_sqlite3_unicode_collate+0xc9>
mov    $0x4,%eax
cmp    -0x8(%rbp),%rax
jne    11e4 <bx_unicode_sqlite3_unicode_collate+0xc4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11cb <bx_unicode_sqlite3_unicode_collate+0xab>
mov    -0xc(%rbp),%eax
mov    %eax,-0x5c(%rbp)
jmp    11d1 <bx_unicode_sqlite3_unicode_collate+0xb1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rdi
mov    -0x5c(%rbp),%edx
call   1030 <bx_unicode_sqlite3StrNICmp16@plt>
mov    %eax,-0x2c(%rbp)
jmp    11e9 <bx_unicode_sqlite3_unicode_collate+0xc9>
xor    %eax,%eax
cmp    -0x2c(%rbp),%eax
jne    11fd <bx_unicode_sqlite3_unicode_collate+0xdd>
mov    -0xc(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bx_unicode_sqlite3_unicode_collate>:
push   %rbp
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %esi,%ebp
cmp    $0x4,%rdi
je     114d <bx_unicode_sqlite3_unicode_collate+0x2d>
cmp    $0x1,%rdi
jne    1165 <bx_unicode_sqlite3_unicode_collate+0x45>
cmp    %ebx,%ebp
mov    %ebx,%eax
cmovl  %ebp,%eax
mov    %rdx,%rdi
mov    %r8,%rsi
mov    %eax,%edx
call   1040 <bx_unicode_sqlite3StrNICmp@plt>
test   %eax,%eax
jne    1169 <bx_unicode_sqlite3_unicode_collate+0x49>
jmp    1165 <bx_unicode_sqlite3_unicode_collate+0x45>
cmp    %ebx,%ebp
mov    %ebx,%eax
cmovl  %ebp,%eax
mov    %rdx,%rdi
mov    %r8,%rsi
mov    %eax,%edx
call   1030 <bx_unicode_sqlite3StrNICmp16@plt>
test   %eax,%eax
jne    1169 <bx_unicode_sqlite3_unicode_collate+0x49>
sub    %ebx,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bx_unicode_sqlite3_unicode_collate(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i8* %4, i8** %10
store i32 0, i32* %11
%12 = load i8*, i8** %6
%13 = icmp eq i8* inttoptr (i64 1 to i8*), %12
br i1 %13, label %14, label %27
14: ; preds = %5
%15 = load i8*, i8** %8
%16 = load i8*, i8** %10
%17 = load i32, i32* %7
%18 = load i32, i32* %9
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = load i32, i32* %7
br label %24
22: ; preds = %14
%23 = load i32, i32* %9
br label %24
24: ; preds = %22, %20
%25 = phi i32 [ %21, %20 ], [ %23, %22 ]
%26 = call i32 @bx_unicode_sqlite3StrNICmp(i8* %15, i8* %16, i32 %25)
store i32 %26, i32* %11
br label %44
27: ; preds = %5
%28 = load i8*, i8** %6
%29 = icmp eq i8* inttoptr (i64 4 to i8*), %28
br i1 %29, label %30, label %43
30: ; preds = %27
%31 = load i8*, i8** %8
%32 = load i8*, i8** %10
%33 = load i32, i32* %7
%34 = load i32, i32* %9
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = load i32, i32* %7
br label %40
38: ; preds = %30
%39 = load i32, i32* %9
br label %40
40: ; preds = %38, %36
%41 = phi i32 [ %37, %36 ], [ %39, %38 ]
%42 = call i32 @bx_unicode_sqlite3StrNICmp16(i8* %31, i8* %32, i32 %41)
store i32 %42, i32* %11
br label %43
43: ; preds = %40, %27
br label %44
44: ; preds = %43, %24
%45 = load i32, i32* %11
%46 = icmp eq i32 0, %45
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i32, i32* %7
%49 = load i32, i32* %9
%50 = sub nsw i32 %48, %49
store i32 %50, i32* %11
br label %51
51: ; preds = %47, %44
%52 = load i32, i32* %11
ret i32 %52
}

LLVM IR (-O2):
define i32 @bx_unicode_sqlite3_unicode_collate(i8* %0, i32 %1, i8* %2, i32 %3, i8* %4) {
%6 = ptrtoint i8* %0 to i64
switch i64 %6, label %18 [
i64 1, label %7
i64 4, label %11
]
7: ; preds = %5
%8 = icmp slt i32 %1, %3
%9 = select i1 %8, i32 %1, i32 %3
%10 = tail call i32 @bx_unicode_sqlite3StrNICmp(i8* %2, i8* %4, i32 %9)
br label %15
11: ; preds = %5
%12 = icmp slt i32 %1, %3
%13 = select i1 %12, i32 %1, i32 %3
%14 = tail call i32 @bx_unicode_sqlite3StrNICmp16(i8* %2, i8* %4, i32 %13)
br label %15
15: ; preds = %11, %7
%16 = phi i32 [ %10, %7 ], [ %14, %11 ]
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %5, %15
%19 = sub nsw i32 %1, %3
br label %20
20: ; preds = %15, %18
%21 = phi i32 [ %19, %18 ], [ %16, %15 ]
ret i32 %21
}