#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 getch();
int utf8len(char);
void get_utf8char(char c[6], int *len) {
  int i;
  c[0] = getch();
  (*len) = utf8len(c[0]);
  for (i = 1; i < (*len); i++)
    c[i] = getch();
}


x86 assembly (clang, -O0):
<get_utf8char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getch@plt>
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1030 <utf8len@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%eax
jge    118b <get_utf8char+0x6b>
mov    $0x0,%al
call   1040 <getch@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115a <get_utf8char+0x3a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_utf8char>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1040 <getch@plt>
mov    %al,(%r15)
movsbl %al,%edi
call   1030 <utf8len@plt>
mov    %eax,(%r14)
cmp    $0x2,%eax
jl     1167 <get_utf8char+0x47>
mov    $0x1,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getch@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
movslq (%r14),%rax
cmp    %rax,%rbx
jl     1150 <get_utf8char+0x30>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @get_utf8char(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%6 = call signext i8 (...) @getch()
%7 = load i8*, i8** %3
%8 = getelementptr inbounds i8, i8* %7, i64 0
store i8 %6, i8* %8
%9 = load i8*, i8** %3
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = call i32 @utf8len(i8 signext %11)
%13 = load i32*, i32** %4
store i32 %12, i32* %13
store i32 1, i32* %5
br label %14
14: ; preds = %25, %2
%15 = load i32, i32* %5
%16 = load i32*, i32** %4
%17 = load i32, i32* %16
%18 = icmp slt i32 %15, %17
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = call signext i8 (...) @getch()
%21 = load i8*, i8** %3
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 %20, i8* %24
br label %25
25: ; preds = %19
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
br label %14
28: ; preds = %14
ret void
}

LLVM IR (-O2):
define void @get_utf8char(i8* nocapture writeonly %0, i32* nocapture %1) {
%3 = tail call signext i8 (...) @getch()
store i8 %3, i8* %0
%4 = tail call i32 @utf8len(i8 signext %3)
store i32 %4, i32* %1
%5 = icmp sgt i32 %4, 1
br i1 %5, label %6, label %14
6: ; preds = %2, %6
%7 = phi i64 [ %10, %6 ], [ 1, %2 ]
%8 = tail call signext i8 (...) @getch()
%9 = getelementptr inbounds i8, i8* %0, i64 %7
store i8 %8, i8* %9
%10 = add nuw nsw i64 %7, 1
%11 = load i32, i32* %1
%12 = sext i32 %11 to i64
%13 = icmp slt i64 %10, %12
br i1 %13, label %6, label %14
14: ; preds = %6, %2
ret void
}