#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 */
struct linenoiseState {
  int dummy;
};

/* Variables and functions */
size_t columnPos(struct linenoiseState *, char *, size_t, size_t);
scalar_t__ isAnsiEscape(char const *, size_t, size_t *);
size_t promptTextColumnLen(struct linenoiseState *l, 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(l, buf, buf_len, buf_len);
}


x86 assembly (clang, -O0):
<promptTextColumnLen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x1028(%rbp)
movq   $0x0,-0x1030(%rbp)
mov    -0x1030(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11e1 <promptTextColumnLen+0xc1>
mov    -0x10(%rbp),%rdi
add    -0x1030(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    -0x1030(%rbp),%rsi
lea    -0x1038(%rbp),%rdx
call   1030 <isAnsiEscape@plt>
cmp    $0x0,%rax
je     11a4 <promptTextColumnLen+0x84>
mov    -0x1038(%rbp),%rax
add    -0x1030(%rbp),%rax
mov    %rax,-0x1030(%rbp)
jmp    114d <promptTextColumnLen+0x2d>
mov    -0x10(%rbp),%rax
mov    -0x1030(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x1030(%rbp)
mov    (%rax,%rcx,1),%cl
mov    -0x1028(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x1028(%rbp)
mov    %cl,-0x1020(%rbp,%rax,1)
jmp    114d <promptTextColumnLen+0x2d>
mov    -0x8(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1028(%rbp),%rdx
mov    -0x1028(%rbp),%rcx
call   1040 <columnPos@plt>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<promptTextColumnLen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rdi,(%rsp)
test   %rdx,%rdx
je     118b <promptTextColumnLen+0x6b>
mov    %rdx,%rbx
mov    %rsi,%r12
xor    %r14d,%r14d
lea    0x8(%rsp),%r13
xor    %r15d,%r15d
jmp    116a <promptTextColumnLen+0x4a>
nopl   (%rax)
movzbl 0x0(%rbp),%eax
mov    %al,0x10(%rsp,%r15,1)
add    $0x1,%r15
mov    $0x1,%eax
add    %rax,%r14
cmp    %rbx,%r14
jae    118e <promptTextColumnLen+0x6e>
lea    (%r12,%r14,1),%rbp
mov    %rbx,%rsi
sub    %r14,%rsi
mov    %rbp,%rdi
mov    %r13,%rdx
call   1030 <isAnsiEscape@plt>
test   %rax,%rax
je     1150 <promptTextColumnLen+0x30>
mov    0x8(%rsp),%rax
jmp    1162 <promptTextColumnLen+0x42>
xor    %r15d,%r15d
lea    0x10(%rsp),%rsi
mov    (%rsp),%rdi
mov    %r15,%rdx
mov    %r15,%rcx
call   1040 <columnPos@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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