char *lcd_getutf8char(char *str, int pos) {
  static char utf8[4];
  int x;
  x = 0;
  utf8[x++] = str[pos];
  if ((utf8[0] & '\xC0') == '\xC0') {
    while ((str[pos + x] & '\xC0') == '\x80' && x < 3) {
      utf8[x] = str[pos + x];
      x++;
    }
  }
  utf8[x] = '\0';
  return (char *)utf8;
}


x86 assembly (clang, -O0):
<lcd_getutf8char>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x10(%rbp)
movslq %eax,%rcx
lea    0x2eef(%rip),%rax
mov    %dl,(%rax,%rcx,1)
movsbl 0x2ee5(%rip),%eax
and    $0xffffffc0,%eax
cmp    $0xffffffc0,%eax
jne    11ba <lcd_getutf8char+0xba>
jmp    114d <lcd_getutf8char+0x4d>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    -0x10(%rbp),%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
and    $0xffffffc0,%ecx
xor    %eax,%eax
cmp    $0xffffff80,%ecx
mov    %al,-0x11(%rbp)
jne    1179 <lcd_getutf8char+0x79>
cmpl   $0x3,-0x10(%rbp)
setl   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1189 <lcd_getutf8char+0x89>
jmp    11b5 <lcd_getutf8char+0xb5>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    -0x10(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
movslq -0x10(%rbp),%rcx
lea    0x2e7d(%rip),%rax
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    114d <lcd_getutf8char+0x4d>
jmp    11ba <lcd_getutf8char+0xba>
movslq -0x10(%rbp),%rcx
lea    0x2e5c(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e51(%rip),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<lcd_getutf8char>:
movslq %esi,%rax
mov    (%rdi,%rax,1),%dl
mov    %dl,0x2f15(%rip)
mov    $0x1,%ecx
cmp    $0xc0,%dl
jb     114a <lcd_getutf8char+0x4a>
mov    0x1(%rax,%rdi,1),%sil
mov    %esi,%edx
and    $0xc0,%dl
cmp    $0x80,%dl
jne    114a <lcd_getutf8char+0x4a>
mov    %sil,0x2ef6(%rip)
mov    0x2(%rax,%rdi,1),%al
mov    %eax,%edx
and    $0xc0,%dl
mov    $0x2,%ecx
cmp    $0x80,%dl
jne    114a <lcd_getutf8char+0x4a>
mov    %al,0x2ede(%rip)
mov    $0x3,%ecx
lea    0x2ed0(%rip),%rax
movb   $0x0,(%rcx,%rax,1)
ret

LLVM IR (-O0):
@lcd_getutf8char.utf8 = internal global [4 x i8] zeroinitializer
define i8* @lcd_getutf8char(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%6 = load i8*, i8** %3
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %6, i64 %8
%10 = load i8, i8* %9
%11 = load i32, i32* %5
%12 = add nsw i32 %11, 1
store i32 %12, i32* %5
%13 = sext i32 %11 to i64
%14 = getelementptr inbounds [4 x i8], [4 x i8]* @lcd_getutf8char.utf8, i64 0, i64 %13
store i8 %10, i8* %14
%15 = load i8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @lcd_getutf8char.utf8, i64 0, i64 0)
%16 = sext i8 %15 to i32
%17 = and i32 %16, -64
%18 = icmp eq i32 %17, -64
br i1 %18, label %19, label %50
19: ; preds = %2
br label %20
20: ; preds = %36, %19
%21 = load i8*, i8** %3
%22 = load i32, i32* %4
%23 = load i32, i32* %5
%24 = add nsw i32 %22, %23
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %21, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = and i32 %28, -64
%30 = icmp eq i32 %29, -128
br i1 %30, label %31, label %34
31: ; preds = %20
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 3
br label %34
34: ; preds = %31, %20
%35 = phi i1 [ false, %20 ], [ %33, %31 ]
br i1 %35, label %36, label %49
36: ; preds = %34
%37 = load i8*, i8** %3
%38 = load i32, i32* %4
%39 = load i32, i32* %5
%40 = add nsw i32 %38, %39
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %37, i64 %41
%43 = load i8, i8* %42
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [4 x i8], [4 x i8]* @lcd_getutf8char.utf8, i64 0, i64 %45
store i8 %43, i8* %46
%47 = load i32, i32* %5
%48 = add nsw i32 %47, 1
store i32 %48, i32* %5
br label %20
49: ; preds = %34
br label %50
50: ; preds = %49, %2
%51 = load i32, i32* %5
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [4 x i8], [4 x i8]* @lcd_getutf8char.utf8, i64 0, i64 %52
store i8 0, i8* %53
ret i8* getelementptr inbounds ([4 x i8], [4 x i8]* @lcd_getutf8char.utf8, i64 0, i64 0)
}

LLVM IR (-O2):
@lcd_getutf8char.utf8 = internal global [4 x i8] zeroinitializer
define i8* @lcd_getutf8char(i8* nocapture %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = getelementptr inbounds i8, i8* %0, i64 %3
%5 = load i8, i8* %4
store i8 %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @lcd_getutf8char.utf8, i64 0, i64 0)
%6 = icmp ugt i8 %5, -65
br i1 %6, label %7, label %21
7: ; preds = %2
%8 = add nsw i32 %1, 1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = and i8 %11, -64
%13 = icmp eq i8 %12, -128
br i1 %13, label %14, label %21
14: ; preds = %7
store i8 %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @lcd_getutf8char.utf8, i64 0, i64 1)
%15 = add nsw i64 %3, 2
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = and i8 %17, -64
%19 = icmp eq i8 %18, -128
br i1 %19, label %20, label %21
20: ; preds = %14
store i8 %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @lcd_getutf8char.utf8, i64 0, i64 2)
br label %21
21: ; preds = %14, %20, %7, %2
%22 = phi i64 [ 1, %2 ], [ 1, %7 ], [ 2, %14 ], [ 3, %20 ]
%23 = getelementptr inbounds [4 x i8], [4 x i8]* @lcd_getutf8char.utf8, i64 0, i64 %22
store i8 0, i8* %23
ret i8* getelementptr inbounds ([4 x i8], [4 x i8]* @lcd_getutf8char.utf8, i64 0, i64 0)
}