#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 columnPos(char *, size_t, size_t);
scalar_t__ isAnsiEscape(char const *, size_t, size_t *);
size_t promptTextColumnLen(const char *prompt, size_t plen) {
  char buf[4096];
  size_t buf_len = 0;
  size_t off = 0;
  while (off < plen) {
    size_t len;
    if (isAnsiEscape(prompt + off, plen - off, &len)) {
      off += len;
      continue;
    }
    buf[buf_len++] = prompt[off++];
  }
  return columnPos(buf, buf_len, buf_len);
}


x86 assembly (clang, -O0):
<promptTextColumnLen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x1018(%rbp)
movq   $0x0,-0x1020(%rbp)
mov    -0x1020(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11dd <promptTextColumnLen+0xbd>
mov    -0x8(%rbp),%rdi
add    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
sub    -0x1020(%rbp),%rsi
lea    -0x1028(%rbp),%rdx
call   1030 <isAnsiEscape@plt>
cmp    $0x0,%rax
je     11a0 <promptTextColumnLen+0x80>
mov    -0x1028(%rbp),%rax
add    -0x1020(%rbp),%rax
mov    %rax,-0x1020(%rbp)
jmp    1149 <promptTextColumnLen+0x29>
mov    -0x8(%rbp),%rax
mov    -0x1020(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x1020(%rbp)
mov    (%rax,%rcx,1),%cl
mov    -0x1018(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x1018(%rbp)
mov    %cl,-0x1010(%rbp,%rax,1)
jmp    1149 <promptTextColumnLen+0x29>
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rsi
mov    -0x1018(%rbp),%rdx
call   1040 <columnPos@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<promptTextColumnLen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
test   %rsi,%rsi
je     118a <promptTextColumnLen+0x6a>
mov    %rsi,%r13
mov    %rdi,%r15
xor    %ebp,%ebp
lea    0x8(%rsp),%r12
xor    %r14d,%r14d
jmp    1169 <promptTextColumnLen+0x49>
nopl   0x0(%rax,%rax,1)
movzbl (%rbx),%eax
mov    %al,0x10(%rsp,%r14,1)
add    $0x1,%r14
mov    $0x1,%eax
add    %rax,%rbp
cmp    %r13,%rbp
jae    118d <promptTextColumnLen+0x6d>
lea    (%r15,%rbp,1),%rbx
mov    %r13,%rsi
sub    %rbp,%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
call   1030 <isAnsiEscape@plt>
test   %rax,%rax
je     1150 <promptTextColumnLen+0x30>
mov    0x8(%rsp),%rax
jmp    1161 <promptTextColumnLen+0x41>
xor    %r14d,%r14d
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
mov    %r14,%rdx
call   1040 <columnPos@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @promptTextColumnLen(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [4096 x i8]
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %3
store i64 %1, i64* %4
store i64 0, i64* %6
store i64 0, i64* %7
br label %9
9: ; preds = %26, %22, %2
%10 = load i64, i64* %7
%11 = load i64, i64* %4
%12 = icmp ult i64 %10, %11
br i1 %12, label %13, label %35
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = load i64, i64* %7
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = load i64, i64* %4
%18 = load i64, i64* %7
%19 = sub i64 %17, %18
%20 = call i64 @isAnsiEscape(i8* %16, i64 %19, i64* %8)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = load i64, i64* %8
%24 = load i64, i64* %7
%25 = add i64 %24, %23
store i64 %25, i64* %7
br label %9
26: ; preds = %13
%27 = load i8*, i8** %3
%28 = load i64, i64* %7
%29 = add i64 %28, 1
store i64 %29, i64* %7
%30 = getelementptr inbounds i8, i8* %27, i64 %28
%31 = load i8, i8* %30
%32 = load i64, i64* %6
%33 = add i64 %32, 1
store i64 %33, i64* %6
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 %32
store i8 %31, i8* %34
br label %9
35: ; preds = %9
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%37 = load i64, i64* %6
%38 = load i64, i64* %6
%39 = call i64 @columnPos(i8* %36, i64 %37, i64 %38)
ret i64 %39
}

LLVM IR (-O2):
define i64 @promptTextColumnLen(i8* %0, i64 %1) {
%3 = alloca [4096 x i8]
%4 = alloca i64
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = icmp eq i64 %1, 0
br i1 %6, label %27, label %7
7: ; preds = %2
%8 = bitcast i64* %4 to i8*
br label %9
9: ; preds = %7, %22
%10 = phi i64 [ 0, %7 ], [ %25, %22 ]
%11 = phi i64 [ 0, %7 ], [ %23, %22 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %10
%13 = sub i64 %1, %10
%14 = call i64 @isAnsiEscape(i8* %12, i64 %13, i64* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %9
%17 = load i64, i64* %4
br label %22
18: ; preds = %9
%19 = load i8, i8* %12
%20 = add i64 %11, 1
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %11
store i8 %19, i8* %21
br label %22
22: ; preds = %18, %16
%23 = phi i64 [ %11, %16 ], [ %20, %18 ]
%24 = phi i64 [ %17, %16 ], [ 1, %18 ]
%25 = add i64 %24, %10
%26 = icmp ult i64 %25, %1
br i1 %26, label %9, label %27
27: ; preds = %22, %2
%28 = phi i64 [ 0, %2 ], [ %23, %22 ]
%29 = call i64 @columnPos(i8* %5, i64 %28, i64 %28)
ret i64 %29
}