#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 */
typedef int /*<<< orphan*/ uint32_t;

/* Variables and functions */
char *fcitx_utf8_get_char(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
__attribute__((visibility("default"))) void
fcitx_utf8_strncpy(char *str, const char *s, size_t byte) {
  while (*s) {
    uint32_t chr;
    const char *next = fcitx_utf8_get_char(s, &chr);
    size_t diff = next - s;
    if (byte < diff)
      break;
    memcpy(str, s, diff);
    str += diff;
    byte -= diff;
    s = next;
  }
  while (byte--) {
    *str = '\0';
    str++;
  }
}


x86 assembly (clang, -O0):
<fcitx_utf8_strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11ad <fcitx_utf8_strncpy+0x8d>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <fcitx_utf8_get_char@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1174 <fcitx_utf8_strncpy+0x54>
jmp    11ad <fcitx_utf8_strncpy+0x8d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1134 <fcitx_utf8_strncpy+0x14>
jmp    11b2 <fcitx_utf8_strncpy+0x92>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x18(%rbp)
cmp    $0x0,%rax
je     11e3 <fcitx_utf8_strncpy+0xc3>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    11b2 <fcitx_utf8_strncpy+0x92>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcitx_utf8_strncpy>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rdi,%r14
cmpb   $0x0,(%rsi)
je     1189 <fcitx_utf8_strncpy+0x59>
mov    %rsi,%r15
mov    %r13,%r12
nopl   0x0(%rax)
mov    %r15,%rdi
lea    0x4(%rsp),%rsi
call   1030 <fcitx_utf8_get_char@plt>
mov    %rax,%rbp
sub    %r15,%rbp
sub    %rbp,%r12
jb     11ad <fcitx_utf8_strncpy+0x7d>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1050 <memcpy@plt>
add    %rbp,%r14
cmpb   $0x0,(%rbx)
mov    %rbx,%r15
mov    %r12,%r13
jne    1150 <fcitx_utf8_strncpy+0x20>
jmp    118c <fcitx_utf8_strncpy+0x5c>
mov    %r13,%r12
test   %r12,%r12
je     119e <fcitx_utf8_strncpy+0x6e>
mov    %r14,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1040 <memset@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%r12
test   %r12,%r12
jne    1191 <fcitx_utf8_strncpy+0x61>
jmp    119e <fcitx_utf8_strncpy+0x6e>

LLVM IR (-O0):
define void @fcitx_utf8_strncpy(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %10
10: ; preds = %26, %3
%11 = load i8*, i8** %5
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 0
br i1 %13, label %14, label %38
14: ; preds = %10
%15 = load i8*, i8** %5
%16 = call i8* @fcitx_utf8_get_char(i8* %15, i32* %7)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = load i8*, i8** %5
%19 = ptrtoint i8* %17 to i64
%20 = ptrtoint i8* %18 to i64
%21 = sub i64 %19, %20
store i64 %21, i64* %9
%22 = load i64, i64* %6
%23 = load i64, i64* %9
%24 = icmp ult i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %14
br label %38
26: ; preds = %14
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = load i64, i64* %9
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
%31 = load i64, i64* %9
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i64 %31
store i8* %33, i8** %4
%34 = load i64, i64* %9
%35 = load i64, i64* %6
%36 = sub i64 %35, %34
store i64 %36, i64* %6
%37 = load i8*, i8** %8
store i8* %37, i8** %5
br label %10
38: ; preds = %25, %10
br label %39
39: ; preds = %43, %38
%40 = load i64, i64* %6
%41 = add i64 %40, -1
store i64 %41, i64* %6
%42 = icmp ne i64 %40, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i8*, i8** %4
store i8 0, i8* %44
%45 = load i8*, i8** %4
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %4
br label %39
47: ; preds = %39
ret void
}

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