#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 */
int CSlim_IsCharacter(unsigned char const *);
int ByteLength(int characterLength, char const *current) {
  unsigned char const *p;
  int chars = 0;
  int bytes = 0;
  for (p = (unsigned char const *)current; chars <= characterLength; p++) {
    bytes++;
    if (CSlim_IsCharacter(p) == 1)
      chars++;
  }
  if (chars > characterLength)
    bytes--;
  return bytes;
}


x86 assembly (clang, -O0):
<ByteLength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     117b <ByteLength+0x6b>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <CSlim_IsCharacter@plt>
cmp    $0x1,%eax
jne    1165 <ByteLength+0x55>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    116a <ByteLength+0x5a>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1135 <ByteLength+0x25>
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jle    1190 <ByteLength+0x80>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ByteLength>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
js     1151 <ByteLength+0x41>
mov    %rsi,%rbx
mov    %edi,%r14d
xor    %ebp,%ebp
mov    $0xffffffff,%r15d
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1030 <CSlim_IsCharacter@plt>
xor    %ecx,%ecx
cmp    $0x1,%eax
sete   %cl
add    %ecx,%ebp
add    $0x1,%rbx
add    $0x1,%r15d
cmp    %r14d,%ebp
jle    1130 <ByteLength+0x20>
jmp    1157 <ByteLength+0x47>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ByteLength(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %6
store i32 0, i32* %7
%8 = load i8*, i8** %4
store i8* %8, i8** %5
br label %9
9: ; preds = %23, %2
%10 = load i32, i32* %6
%11 = load i32, i32* %3
%12 = icmp sle i32 %10, %11
br i1 %12, label %13, label %26
13: ; preds = %9
%14 = load i32, i32* %7
%15 = add nsw i32 %14, 1
store i32 %15, i32* %7
%16 = load i8*, i8** %5
%17 = call i32 @CSlim_IsCharacter(i8* %16)
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* %6
%21 = add nsw i32 %20, 1
store i32 %21, i32* %6
br label %22
22: ; preds = %19, %13
br label %23
23: ; preds = %22
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %5
br label %9
26: ; preds = %9
%27 = load i32, i32* %6
%28 = load i32, i32* %3
%29 = icmp sgt i32 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %26
%31 = load i32, i32* %7
%32 = add nsw i32 %31, -1
store i32 %32, i32* %7
br label %33
33: ; preds = %30, %26
%34 = load i32, i32* %7
ret i32 %34
}

LLVM IR (-O2):
define i32 @ByteLength(i32 %0, i8* %1) {
%3 = icmp slt i32 %0, 0
br i1 %3, label %15, label %4
4: ; preds = %2, %4
%5 = phi i32 [ %8, %4 ], [ 0, %2 ]
%6 = phi i32 [ %12, %4 ], [ 0, %2 ]
%7 = phi i8* [ %13, %4 ], [ %1, %2 ]
%8 = add nuw nsw i32 %5, 1
%9 = tail call i32 @CSlim_IsCharacter(i8* %7)
%10 = icmp eq i32 %9, 1
%11 = zext i1 %10 to i32
%12 = add nuw nsw i32 %6, %11
%13 = getelementptr inbounds i8, i8* %7, i64 1
%14 = icmp sgt i32 %12, %0
br i1 %14, label %15, label %4
15: ; preds = %4, %2
%16 = phi i32 [ -1, %2 ], [ %5, %4 ]
ret i32 %16
}