#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 */
char *fcitx_utf8_get_char(char const *, int *);
__attribute__((visibility("default"))) int
fcitx_utf8_strncmp(const char *s1, const char *s2, int n) {
  int c1, c2;
  int i;
  for (i = 0; i < n; i++) {
    if (!(*s1 & 0x80)) {
      if (*s1 != *s2)
        return 1;
      if (*s1 == 0)
        return 0;
      s1++;
      s2++;
    } else {
      s1 = fcitx_utf8_get_char(s1, &c1);
      s2 = fcitx_utf8_get_char(s2, &c2);
      if (c1 != c2)
        return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<fcitx_utf8_strncmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11f8 <fcitx_utf8_strncmp+0xe8>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    11a6 <fcitx_utf8_strncmp+0x96>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     116d <fcitx_utf8_strncmp+0x5d>
movl   $0x1,-0x4(%rbp)
jmp    11ff <fcitx_utf8_strncmp+0xef>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1189 <fcitx_utf8_strncmp+0x79>
movl   $0x0,-0x4(%rbp)
jmp    11ff <fcitx_utf8_strncmp+0xef>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11e5 <fcitx_utf8_strncmp+0xd5>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <fcitx_utf8_get_char@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1030 <fcitx_utf8_get_char@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x24(%rbp),%eax
je     11e0 <fcitx_utf8_strncmp+0xd0>
movl   $0x1,-0x4(%rbp)
jmp    11ff <fcitx_utf8_strncmp+0xef>
jmp    11e5 <fcitx_utf8_strncmp+0xd5>
jmp    11ea <fcitx_utf8_strncmp+0xda>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    112a <fcitx_utf8_strncmp+0x1a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcitx_utf8_strncmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %edx,%edx
jle    118a <fcitx_utf8_strncmp+0x7a>
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0x4(%rsp),%r12
mov    %rsp,%r13
jmp    116a <fcitx_utf8_strncmp+0x5a>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <fcitx_utf8_get_char@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r13,%rsi
call   1030 <fcitx_utf8_get_char@plt>
mov    %rax,%r15
mov    0x4(%rsp),%eax
cmp    (%rsp),%eax
jne    1184 <fcitx_utf8_strncmp+0x74>
add    $0xffffffff,%ebp
je     118a <fcitx_utf8_strncmp+0x7a>
movzbl (%rbx),%eax
test   %al,%al
js     1140 <fcitx_utf8_strncmp+0x30>
cmp    (%r15),%al
jne    1184 <fcitx_utf8_strncmp+0x74>
test   %al,%al
je     118a <fcitx_utf8_strncmp+0x7a>
add    $0x1,%rbx
add    $0x1,%r15
jmp    1165 <fcitx_utf8_strncmp+0x55>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @fcitx_utf8_strncmp(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %10
br label %11
11: ; preds = %52, %3
%12 = load i32, i32* %10
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %55
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = and i32 %18, 128
%20 = icmp ne i32 %19, 0
br i1 %20, label %41, label %21
21: ; preds = %15
%22 = load i8*, i8** %5
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %24, %27
br i1 %28, label %29, label %30
29: ; preds = %21
store i32 1, i32* %4
br label %56
30: ; preds = %21
%31 = load i8*, i8** %5
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %30
store i32 0, i32* %4
br label %56
36: ; preds = %30
%37 = load i8*, i8** %5
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %5
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %6
br label %51
41: ; preds = %15
%42 = load i8*, i8** %5
%43 = call i8* @fcitx_utf8_get_char(i8* %42, i32* %8)
store i8* %43, i8** %5
%44 = load i8*, i8** %6
%45 = call i8* @fcitx_utf8_get_char(i8* %44, i32* %9)
store i8* %45, i8** %6
%46 = load i32, i32* %8
%47 = load i32, i32* %9
%48 = icmp ne i32 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %41
store i32 1, i32* %4
br label %56
50: ; preds = %41
br label %51
51: ; preds = %50, %36
br label %52
52: ; preds = %51
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %11
55: ; preds = %11
store i32 0, i32* %4
br label %56
56: ; preds = %55, %49, %35, %29
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
define i32 @fcitx_utf8_strncmp(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %34
9: ; preds = %3, %29
%10 = phi i32 [ %32, %29 ], [ 0, %3 ]
%11 = phi i8* [ %31, %29 ], [ %1, %3 ]
%12 = phi i8* [ %30, %29 ], [ %0, %3 ]
%13 = load i8, i8* %12
%14 = icmp sgt i8 %13, -1
br i1 %14, label %15, label %23
15: ; preds = %9
%16 = load i8, i8* %11
%17 = icmp eq i8 %13, %16
br i1 %17, label %18, label %34
18: ; preds = %15
%19 = icmp eq i8 %13, 0
br i1 %19, label %34, label %20
20: ; preds = %18
%21 = getelementptr inbounds i8, i8* %12, i64 1
%22 = getelementptr inbounds i8, i8* %11, i64 1
br label %29
23: ; preds = %9
%24 = call i8* @fcitx_utf8_get_char(i8* %12, i32* %4)
%25 = call i8* @fcitx_utf8_get_char(i8* %11, i32* %5)
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %34
29: ; preds = %20, %23
%30 = phi i8* [ %24, %23 ], [ %21, %20 ]
%31 = phi i8* [ %25, %23 ], [ %22, %20 ]
%32 = add nuw nsw i32 %10, 1
%33 = icmp eq i32 %32, %2
br i1 %33, label %34, label %9
34: ; preds = %15, %18, %23, %29, %3
%35 = phi i32 [ 0, %3 ], [ 0, %29 ], [ 1, %23 ], [ 0, %18 ], [ 1, %15 ]
ret i32 %35
}