#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 */
scalar_t__ strchr(char *, char const);
size_t strlen(char const *);
size_t serialization_strlen(const char *string) {
  size_t result = 0;
  size_t i = 0, len = strlen(string);
  for (i = 0; i < len; i++) {
    if (strchr("\"\\\b\f\n\r\t", string[i]))
      result += 2;
    else
      result += 1;
  }
  return result;
}


x86 assembly (clang, -O0):
<serialization_strlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11b4 <serialization_strlen+0x94>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
lea    0xe92(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
je     1192 <serialization_strlen+0x72>
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
jmp    119e <serialization_strlen+0x7e>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11a3 <serialization_strlen+0x83>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1151 <serialization_strlen+0x31>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serialization_strlen>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
call   1030 <strlen@plt>
test   %rax,%rax
je     1174 <serialization_strlen+0x54>
mov    %rax,%r15
xor    %ebx,%ebx
lea    0xebe(%rip),%r12
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nop
movsbl (%r14,%r13,1),%esi
mov    %r12,%rdi
call   1040 <strchr@plt>
cmp    $0x1,%rax
sbb    $0x0,%rbx
add    $0x2,%rbx
add    $0x1,%r13
cmp    %r13,%r15
jne    1150 <serialization_strlen+0x30>
jmp    1176 <serialization_strlen+0x56>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [8 x i8] c"\22\\\08\0C\0A\0D\09\00"
define i64 @serialization_strlen(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
store i64 0, i64* %3
store i64 0, i64* %4
%6 = load i8*, i8** %2
%7 = call i64 @strlen(i8* %6)
store i64 %7, i64* %5
store i64 0, i64* %4
br label %8
8: ; preds = %26, %1
%9 = load i64, i64* %4
%10 = load i64, i64* %5
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %29
12: ; preds = %8
%13 = load i8*, i8** %2
%14 = load i64, i64* %4
%15 = getelementptr inbounds i8, i8* %13, i64 %14
%16 = load i8, i8* %15
%17 = call i64 @strchr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8 signext %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i64, i64* %3
%21 = add i64 %20, 2
store i64 %21, i64* %3
br label %25
22: ; preds = %12
%23 = load i64, i64* %3
%24 = add i64 %23, 1
store i64 %24, i64* %3
br label %25
25: ; preds = %22, %19
br label %26
26: ; preds = %25
%27 = load i64, i64* %4
%28 = add i64 %27, 1
store i64 %28, i64* %4
br label %8
29: ; preds = %8
%30 = load i64, i64* %3
ret i64 %30
}

LLVM IR (-O2):
@.str = [8 x i8] c"\22\\\08\0C\0A\0D\09\00"
define i64 @serialization_strlen(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %15, label %4
4: ; preds = %1, %4
%5 = phi i64 [ %12, %4 ], [ 0, %1 ]
%6 = phi i64 [ %13, %4 ], [ 0, %1 ]
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = tail call i64 @strchr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8 signext %8)
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i64 1, i64 2
%12 = add i64 %11, %5
%13 = add nuw i64 %6, 1
%14 = icmp eq i64 %13, %2
br i1 %14, label %15, label %4
15: ; preds = %4, %1
%16 = phi i64 [ 0, %1 ], [ %12, %4 ]
ret i64 %16
}