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

/* Variables and functions */
scalar_t__ insert_char(struct current *, int, int);
int utf8_tounicode(char const *, int *);
int insert_chars(struct current *current, int pos, const char *chars) {
  int inserted = 0;
  while (*chars) {
    int ch;
    int n = utf8_tounicode(chars, &ch);
    if (insert_char(current, pos, ch) == 0) {
      break;
    }
    inserted++;
    pos++;
    chars += n;
  }
  return inserted;
}


x86 assembly (clang, -O0):
<insert_chars>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     119d <insert_chars+0x7d>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1030 <utf8_tounicode@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1040 <insert_char@plt>
cmp    $0x0,%rax
jne    1175 <insert_chars+0x55>
jmp    119d <insert_chars+0x7d>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    113a <insert_chars+0x1a>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insert_chars>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmpb   $0x0,(%rdx)
je     117a <insert_chars+0x5a>
mov    %rdx,%r15
mov    %esi,%r14d
mov    %rdi,%r12
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
lea    (%r14,%rbx,1),%r13d
mov    %r15,%rdi
lea    0x4(%rsp),%rsi
call   1030 <utf8_tounicode@plt>
mov    %eax,%ebp
mov    0x4(%rsp),%edx
mov    %r12,%rdi
mov    %r13d,%esi
call   1040 <insert_char@plt>
test   %rax,%rax
je     117c <insert_chars+0x5c>
add    $0x1,%ebx
movslq %ebp,%rax
cmpb   $0x0,(%r15,%rax,1)
lea    (%r15,%rax,1),%r15
jne    1140 <insert_chars+0x20>
jmp    117c <insert_chars+0x5c>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.current = type { i32 }
define i32 @insert_chars(%struct.current* %0, i32 %1, i8* %2) {
%4 = alloca %struct.current*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store %struct.current* %0, %struct.current** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %7
br label %10
10: ; preds = %23, %3
%11 = load i8*, i8** %6
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 0
br i1 %13, label %14, label %32
14: ; preds = %10
%15 = load i8*, i8** %6
%16 = call i32 @utf8_tounicode(i8* %15, i32* %8)
store i32 %16, i32* %9
%17 = load %struct.current*, %struct.current** %4
%18 = load i32, i32* %5
%19 = load i32, i32* %8
%20 = call i64 @insert_char(%struct.current* %17, i32 %18, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %14
br label %32
23: ; preds = %14
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
%28 = load i32, i32* %9
%29 = load i8*, i8** %6
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %6
br label %10
32: ; preds = %22, %10
%33 = load i32, i32* %7
ret i32 %33
}

LLVM IR (-O2):
%struct.current = type { i32 }
define i32 @insert_chars(%struct.current* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = load i8, i8* %2
%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, %18
%10 = phi i32 [ %1, %7 ], [ %20, %18 ]
%11 = phi i32 [ 0, %7 ], [ %19, %18 ]
%12 = phi i8* [ %2, %7 ], [ %22, %18 ]
%13 = call i32 @utf8_tounicode(i8* %12, i32* %4)
%14 = load i32, i32* %4
%15 = call i64 @insert_char(%struct.current* %0, i32 %10, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %9
br label %25
18: ; preds = %9
%19 = add nuw nsw i32 %11, 1
%20 = add nsw i32 %10, 1
%21 = sext i32 %13 to i64
%22 = getelementptr inbounds i8, i8* %12, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %9
25: ; preds = %18, %3, %17
%26 = phi i32 [ %11, %17 ], [ 0, %3 ], [ %19, %18 ]
ret i32 %26
}