#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 n_bytes_for_n_characters(char *buffer, int length, int n_characters) {
  int i = 0, n_bytes = 0, c, code;
  for (c = 0; c < n_characters; c++) {
    if ((code = next_character(buffer + i, length - i)) < 0)
      return -1;
    n_bytes += code;
  }
  return n_bytes;
}


x86 assembly (clang, -O0):
<n_bytes_for_n_characters>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1188 <n_bytes_for_n_characters+0x78>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    -0x14(%rbp),%esi
sub    -0x1c(%rbp),%esi
call   1030 <next_character@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1171 <n_bytes_for_n_characters+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    118e <n_bytes_for_n_characters+0x7e>
mov    -0x28(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1137 <n_bytes_for_n_characters+0x27>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<n_bytes_for_n_characters>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1148 <n_bytes_for_n_characters+0x38>
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r15
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <next_character@plt>
test   %eax,%eax
js     114c <n_bytes_for_n_characters+0x3c>
add    %eax,%ebx
add    $0xffffffff,%ebp
jne    1130 <n_bytes_for_n_characters+0x20>
jmp    1151 <n_bytes_for_n_characters+0x41>
xor    %ebx,%ebx
jmp    1151 <n_bytes_for_n_characters+0x41>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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