#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 unsigned char int32_t;

/* Variables and functions */
size_t utf8_check_first(char const);
int /*<<< orphan*/ utf8_check_full(char const *, size_t, unsigned char *);
const char *utf8_iterate(const char *buffer, size_t bufsize,
                         int32_t *codepoint) {
  size_t count;
  int32_t value;
  if (!bufsize)
    return buffer;
  count = utf8_check_first(buffer[0]);
  if (count <= 0)
    return NULL;
  if (count == 1)
    value = (unsigned char)buffer[0];
  else {
    if (count > bufsize || !utf8_check_full(buffer, count, &value))
      return NULL;
  }
  if (codepoint)
    *codepoint = value;
  return buffer + count;
}


x86 assembly (clang, -O0):
<utf8_iterate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    114c <utf8_iterate+0x2c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e7 <utf8_iterate+0xc7>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1030 <utf8_check_first@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
ja     1174 <utf8_iterate+0x54>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <utf8_iterate+0xc7>
cmpq   $0x1,-0x28(%rbp)
jne    118d <utf8_iterate+0x6d>
mov    -0x10(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x29(%rbp)
jmp    11c7 <utf8_iterate+0xa7>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     11b5 <utf8_iterate+0x95>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x29(%rbp),%rdx
call   1040 <utf8_check_full@plt>
cmp    $0x0,%eax
jne    11c2 <utf8_iterate+0xa2>
movq   $0x0,-0x8(%rbp)
jmp    11e7 <utf8_iterate+0xc7>
jmp    11c7 <utf8_iterate+0xa7>
cmpq   $0x0,-0x20(%rbp)
je     11db <utf8_iterate+0xbb>
mov    -0x29(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utf8_iterate>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
test   %rsi,%rsi
je     1185 <utf8_iterate+0x65>
mov    %rdx,%r14
mov    %rsi,%rbx
movsbl (%r12),%edi
call   1030 <utf8_check_first@plt>
test   %rax,%rax
je     1182 <utf8_iterate+0x62>
mov    %rax,%r15
cmp    $0x1,%rax
jne    1158 <utf8_iterate+0x38>
mov    (%r12),%al
mov    %al,0x7(%rsp)
jmp    1171 <utf8_iterate+0x51>
cmp    %rbx,%r15
ja     1182 <utf8_iterate+0x62>
lea    0x7(%rsp),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
call   1040 <utf8_check_full@plt>
test   %eax,%eax
je     1182 <utf8_iterate+0x62>
test   %r14,%r14
je     117d <utf8_iterate+0x5d>
mov    0x7(%rsp),%al
mov    %al,(%r14)
add    %r15,%r12
jmp    1185 <utf8_iterate+0x65>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @utf8_iterate(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%10 = load i64, i64* %6
%11 = icmp ne i64 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = load i8*, i8** %5
store i8* %13, i8** %4
br label %50
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = call i64 @utf8_check_first(i8 signext %17)
store i64 %18, i64* %8
%19 = load i64, i64* %8
%20 = icmp ule i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %14
store i8* null, i8** %4
br label %50
22: ; preds = %14
%23 = load i64, i64* %8
%24 = icmp eq i64 %23, 1
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i8*, i8** %5
%27 = getelementptr inbounds i8, i8* %26, i64 0
%28 = load i8, i8* %27
store i8 %28, i8* %9
br label %40
29: ; preds = %22
%30 = load i64, i64* %8
%31 = load i64, i64* %6
%32 = icmp ugt i64 %30, %31
br i1 %32, label %38, label %33
33: ; preds = %29
%34 = load i8*, i8** %5
%35 = load i64, i64* %8
%36 = call i32 @utf8_check_full(i8* %34, i64 %35, i8* %9)
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %33, %29
store i8* null, i8** %4
br label %50
39: ; preds = %33
br label %40
40: ; preds = %39, %25
%41 = load i8*, i8** %7
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i8, i8* %9
%45 = load i8*, i8** %7
store i8 %44, i8* %45
br label %46
46: ; preds = %43, %40
%47 = load i8*, i8** %5
%48 = load i64, i64* %8
%49 = getelementptr inbounds i8, i8* %47, i64 %48
store i8* %49, i8** %4
br label %50
50: ; preds = %46, %38, %21, %12
%51 = load i8*, i8** %4
ret i8* %51
}

LLVM IR (-O2):
define i8* @utf8_iterate(i8* %0, i64 %1, i8* writeonly %2) {
%4 = alloca i8
%5 = icmp eq i64 %1, 0
br i1 %5, label %22, label %6
6: ; preds = %3
%7 = load i8, i8* %0
%8 = tail call i64 @utf8_check_first(i8 signext %7)
switch i64 %8, label %11 [
i64 0, label %22
i64 1, label %9
]
9: ; preds = %6
%10 = load i8, i8* %0
store i8 %10, i8* %4
br label %16
11: ; preds = %6
%12 = icmp ugt i64 %8, %1
br i1 %12, label %22, label %13
13: ; preds = %11
%14 = call i32 @utf8_check_full(i8* %0, i64 %8, i8* %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %22, label %16
16: ; preds = %13, %9
%17 = icmp eq i8* %2, null
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = load i8, i8* %4
store i8 %19, i8* %2
br label %20
20: ; preds = %18, %16
%21 = getelementptr inbounds i8, i8* %0, i64 %8
br label %22
22: ; preds = %11, %13, %6, %3, %20
%23 = phi i8* [ %21, %20 ], [ %0, %3 ], [ null, %6 ], [ null, %13 ], [ null, %11 ]
ret i8* %23
}