#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 */
scalar_t__ chewing_utf8_byte2len(char);
int /*<<< orphan*/ memcpy(char *, char *, int);
int chewing_utf8_strncpy(char *dest, const char *src, int n, int end) {
  int i = 0, len = 0;
  char *iter = (char *)src;
  for (i = 0; i < n; i++) {
    len += chewing_utf8_byte2len(iter[len]);
  }
  memcpy(dest, iter, len);
  if (end == 1)
    ((char *)dest)[len] = '\0';
  return len;
}


x86 assembly (clang, -O0):
<chewing_utf8_strncpy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118b <chewing_utf8_strncpy+0x6b>
mov    -0x28(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <chewing_utf8_byte2len@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1153 <chewing_utf8_strncpy+0x33>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <memcpy@plt>
cmpl   $0x1,-0x18(%rbp)
jne    11b1 <chewing_utf8_strncpy+0x91>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chewing_utf8_strncpy>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %rsi,%r12
mov    %rdi,%r15
test   %edx,%edx
jle    1156 <chewing_utf8_strncpy+0x36>
mov    %edx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
movslq %ebp,%rbp
movsbl (%r12,%rbp,1),%edi
call   1040 <chewing_utf8_byte2len@plt>
add    %eax,%ebp
add    $0xffffffff,%ebx
jne    1140 <chewing_utf8_strncpy+0x20>
jmp    1158 <chewing_utf8_strncpy+0x38>
xor    %ebp,%ebp
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <memcpy@plt>
cmp    $0x1,%r14d
jne    1173 <chewing_utf8_strncpy+0x53>
movslq %ebp,%rax
movb   $0x0,(%r15,%rax,1)
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @chewing_utf8_strncpy(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %17
6: ; preds = %4, %6
%7 = phi i32 [ %15, %6 ], [ 0, %4 ]
%8 = phi i32 [ %14, %6 ], [ 0, %4 ]
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = load i8, i8* %10
%12 = tail call i64 @chewing_utf8_byte2len(i8 signext %11)
%13 = trunc i64 %12 to i32
%14 = add i32 %8, %13
%15 = add nuw nsw i32 %7, 1
%16 = icmp eq i32 %15, %2
br i1 %16, label %17, label %6
17: ; preds = %6, %4
%18 = phi i32 [ 0, %4 ], [ %14, %6 ]
%19 = tail call i32 @memcpy(i8* %0, i8* %1, i32 %18)
%20 = icmp eq i32 %3, 1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = sext i32 %18 to i64
%23 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %21, %17
ret i32 %18
}