#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 */
typedef int /*<<< orphan*/ va_list;

/* Variables and functions */
int get_part_length(char const *, int /*<<< orphan*/, char const **);
int compute_format_length(const char *format, va_list ap) {
  int result = 0;
  const char *part = format;
  while (*part) {
    const char *next_part = NULL;
    int part_len;
    part_len = get_part_length(part, ap, &next_part);
    if (part_len < 0) {
      return -1;
    }
    result += part_len;
    part = next_part;
  }
  return result;
}


x86 assembly (clang, -O0):
<compute_format_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1182 <compute_format_length+0x72>
movq   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x28(%rbp),%rdx
call   1030 <get_part_length@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    116c <compute_format_length+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <compute_format_length+0x78>
mov    -0x2c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    112e <compute_format_length+0x1e>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compute_format_length>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
xor    %ebx,%ebx
lea    0x8(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpb   $0x0,(%rdi)
je     1158 <compute_format_length+0x48>
movq   $0x0,0x8(%rsp)
mov    %ebp,%esi
mov    %r14,%rdx
call   1030 <get_part_length@plt>
add    %eax,%ebx
mov    0x8(%rsp),%rdi
test   %eax,%eax
jns    1130 <compute_format_length+0x20>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @compute_format_length(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
br label %5
5: ; preds = %10, %2
%6 = phi i32 [ 0, %2 ], [ %13, %10 ]
%7 = phi i8* [ %0, %2 ], [ %14, %10 ]
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %5
store i8* null, i8** %3
%11 = call i32 @get_part_length(i8* %7, i32 %1, i8** %3)
%12 = icmp slt i32 %11, 0
%13 = add nsw i32 %11, %6
%14 = load i8*, i8** %3
br i1 %12, label %15, label %5
15: ; preds = %5, %10
%16 = phi i32 [ -1, %10 ], [ %6, %5 ]
ret i32 %16
}