#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_width_esc(char const *);
size_t esc_str_overhead(const char str[]) {
  size_t overhead = 0U;
  while (*str != '\0') {
    const size_t char_width_esc = get_char_width_esc(str);
    if (*str == '\033') {
      overhead += char_width_esc;
    }
    str += char_width_esc;
  }
  return overhead;
}


x86 assembly (clang, -O0):
<esc_str_overhead>:
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     116e <esc_str_overhead+0x5e>
mov    -0x8(%rbp),%rdi
call   1030 <get_char_width_esc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x1b,%eax
jne    115d <esc_str_overhead+0x4d>
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1124 <esc_str_overhead+0x14>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<esc_str_overhead>:
push   %r14
push   %rbx
push   %rax
cmpb   $0x0,(%rdi)
je     1143 <esc_str_overhead+0x33>
mov    %rdi,%rbx
xor    %r14d,%r14d
nop
mov    %rbx,%rdi
call   1030 <get_char_width_esc@plt>
cmpb   $0x1b,(%rbx)
mov    $0x0,%ecx
cmove  %rax,%rcx
add    %rcx,%r14
cmpb   $0x0,(%rbx,%rax,1)
lea    (%rbx,%rax,1),%rbx
jne    1120 <esc_str_overhead+0x10>
jmp    1146 <esc_str_overhead+0x36>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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