#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 ReadChar(char **, char *);
int CalcRealLength(char *source) {
  int count = 0;
  int len;
  char *src = source;
  char bytes[10];
  while (*src != '\0') {
    len = ReadChar(&src, bytes);
    if (len == 2)
      len--;
    count += len;
  }
  return count;
}


x86 assembly (clang, -O0):
<CalcRealLength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     116c <CalcRealLength+0x5c>
lea    -0x22(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   1030 <ReadChar@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x2,-0x10(%rbp)
jne    115e <CalcRealLength+0x4e>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    112b <CalcRealLength+0x1b>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CalcRealLength>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,(%rsp)
cmpb   $0x0,(%rdi)
je     115e <CalcRealLength+0x4e>
xor    %ebx,%ebx
mov    %rsp,%r14
lea    0xe(%rsp),%r15
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <ReadChar@plt>
cmp    $0x2,%eax
cmove  %ebp,%eax
add    %eax,%ebx
mov    (%rsp),%rax
cmpb   $0x0,(%rax)
jne    1140 <CalcRealLength+0x30>
jmp    1160 <CalcRealLength+0x50>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @CalcRealLength(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [10 x i8]
store i8* %0, i8** %2
store i32 0, i32* %3
%7 = load i8*, i8** %2
store i8* %7, i8** %5
br label %8
8: ; preds = %21, %1
%9 = load i8*, i8** %5
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %8
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%15 = call i32 @ReadChar(i8** %5, i8* %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* %4
%20 = add nsw i32 %19, -1
store i32 %20, i32* %4
br label %21
21: ; preds = %18, %13
%22 = load i32, i32* %4
%23 = load i32, i32* %3
%24 = add nsw i32 %23, %22
store i32 %24, i32* %3
br label %8
25: ; preds = %8
%26 = load i32, i32* %3
ret i32 %26
}

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