#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 */
size_t get_char_screen_width(char const *, size_t const);
size_t get_char_width(char const *);
size_t get_screen_string_length(const char str[]) {
  size_t length = 0;
  while (*str != '\0') {
    const size_t char_width = get_char_width(str);
    const size_t char_screen_width = get_char_screen_width(str, char_width);
    str += char_width;
    length += char_screen_width;
  }
  return length;
}


x86 assembly (clang, -O0):
<get_screen_string_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     117f <get_screen_string_length+0x5f>
mov    -0x8(%rbp),%rdi
call   1030 <get_char_width@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <get_char_screen_width@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1134 <get_screen_string_length+0x14>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_screen_string_length>:
push   %r15
push   %r14
push   %rbx
cmpb   $0x0,(%rdi)
je     1156 <get_screen_string_length+0x36>
mov    %rdi,%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1030 <get_char_width@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <get_char_screen_width@plt>
add    %rax,%r14
cmpb   $0x0,(%rbx,%r15,1)
lea    (%rbx,%r15,1),%rbx
jne    1130 <get_screen_string_length+0x10>
jmp    1159 <get_screen_string_length+0x39>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @get_screen_string_length(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* %3
br label %6
6: ; preds = %11, %1
%7 = load i8*, i8** %2
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %6
%12 = load i8*, i8** %2
%13 = call i64 @get_char_width(i8* %12)
store i64 %13, i64* %4
%14 = load i8*, i8** %2
%15 = load i64, i64* %4
%16 = call i64 @get_char_screen_width(i8* %14, i64 %15)
store i64 %16, i64* %5
%17 = load i64, i64* %4
%18 = load i8*, i8** %2
%19 = getelementptr inbounds i8, i8* %18, i64 %17
store i8* %19, i8** %2
%20 = load i64, i64* %5
%21 = load i64, i64* %3
%22 = add i64 %21, %20
store i64 %22, i64* %3
br label %6
23: ; preds = %6
%24 = load i64, i64* %3
ret i64 %24
}

LLVM IR (-O2):
define i64 @get_screen_string_length(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %13, label %4
4: ; preds = %1, %4
%5 = phi i8* [ %9, %4 ], [ %0, %1 ]
%6 = phi i64 [ %10, %4 ], [ 0, %1 ]
%7 = tail call i64 @get_char_width(i8* %5)
%8 = tail call i64 @get_char_screen_width(i8* %5, i64 %7)
%9 = getelementptr inbounds i8, i8* %5, i64 %7
%10 = add i64 %8, %6
%11 = load i8, i8* %9
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %4
13: ; preds = %4, %1
%14 = phi i64 [ 0, %1 ], [ %10, %4 ]
ret i64 %14
}