#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 next_character(char *, int);
int count_characters(char *buffer, int length) {
  int n_characters = 0, code, buffer_i = 0;
  while (buffer_i < length) {
    if ((code = next_character(buffer + buffer_i, length - buffer_i)) < 0)
      return -1;
    buffer_i += code;
    n_characters++;
  }
  return n_characters;
}


x86 assembly (clang, -O0):
<count_characters>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    117e <count_characters+0x6e>
mov    -0x10(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x20(%rbp),%esi
call   1030 <next_character@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1167 <count_characters+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    1184 <count_characters+0x74>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112d <count_characters+0x1d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_characters>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    1151 <count_characters+0x41>
mov    %esi,%r15d
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebp,%rax
lea    (%r14,%rax,1),%rdi
mov    %r15d,%esi
sub    %eax,%esi
call   1030 <next_character@plt>
test   %eax,%eax
js     1155 <count_characters+0x45>
add    %eax,%ebp
add    $0x1,%ebx
cmp    %r15d,%ebp
jl     1130 <count_characters+0x20>
jmp    115a <count_characters+0x4a>
xor    %ebx,%ebx
jmp    115a <count_characters+0x4a>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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