#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 strlen(char const *);
int strlen2(const char *str) {
  int len = 0, i;
  for (i = 0; i < strlen(str); i++)
    if (str[i] == 27) {
      i++;
      while ((i < strlen(str)) && (str[i] != 'm'))
        i++;
    } else {
      len++;
    }
  return len;
}


x86 assembly (clang, -O0):
<strlen2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jge    11d9 <strlen2+0xc9>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x1b,%eax
jne    11bd <strlen2+0xad>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x1c(%rbp),%ecx
mov    %eax,%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x15(%rbp)
jge    119a <strlen2+0x8a>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x6d,%eax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11aa <strlen2+0x9a>
jmp    11b8 <strlen2+0xa8>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1164 <strlen2+0x54>
jmp    11c6 <strlen2+0xb6>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11cb <strlen2+0xbb>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    112a <strlen2+0x1a>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlen2>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <strlen@plt>
test   %eax,%eax
jle    117e <strlen2+0x6e>
xor    %ebp,%ebp
xor    %r14d,%r14d
jmp    1143 <strlen2+0x33>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r14d
add    $0x1,%ebp
mov    %r15,%rdi
call   1030 <strlen@plt>
cmp    %eax,%ebp
jge    1181 <strlen2+0x71>
movslq %ebp,%rbx
cmpb   $0x1b,(%r15,%rbx,1)
jne    1130 <strlen2+0x20>
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebx,%ebp
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jge    1134 <strlen2+0x24>
cmpb   $0x6d,(%r15,%rbx,1)
lea    0x1(%rbx),%rbx
jne    1160 <strlen2+0x50>
jmp    1134 <strlen2+0x24>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @strlen2(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %44, %1
%6 = load i32, i32* %4
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = icmp slt i32 %6, %8
br i1 %9, label %10, label %47
10: ; preds = %5
%11 = load i8*, i8** %2
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 27
br i1 %17, label %18, label %40
18: ; preds = %10
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
store i32 %20, i32* %4
br label %21
21: ; preds = %36, %18
%22 = load i32, i32* %4
%23 = load i8*, i8** %2
%24 = call i32 @strlen(i8* %23)
%25 = icmp slt i32 %22, %24
br i1 %25, label %26, label %34
26: ; preds = %21
%27 = load i8*, i8** %2
%28 = load i32, i32* %4
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 109
br label %34
34: ; preds = %26, %21
%35 = phi i1 [ false, %21 ], [ %33, %26 ]
br i1 %35, label %36, label %39
36: ; preds = %34
%37 = load i32, i32* %4
%38 = add nsw i32 %37, 1
store i32 %38, i32* %4
br label %21
39: ; preds = %34
br label %43
40: ; preds = %10
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 1
store i32 %42, i32* %3
br label %43
43: ; preds = %40, %39
br label %44
44: ; preds = %43
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
br label %5
47: ; preds = %5
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
define i32 @strlen2(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp sgt i32 %2, 0
br i1 %3, label %4, label %31
4: ; preds = %1, %25
%5 = phi i32 [ %28, %25 ], [ 0, %1 ]
%6 = phi i32 [ %26, %25 ], [ 0, %1 ]
%7 = sext i32 %5 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 27
br i1 %10, label %11, label %21
11: ; preds = %4, %17
%12 = phi i64 [ %13, %17 ], [ %7, %4 ]
%13 = add nsw i64 %12, 1
%14 = tail call i32 @strlen(i8* %0)
%15 = sext i32 %14 to i64
%16 = icmp slt i64 %13, %15
br i1 %16, label %17, label %23
17: ; preds = %11
%18 = getelementptr inbounds i8, i8* %0, i64 %13
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 109
br i1 %20, label %23, label %11
21: ; preds = %4
%22 = add nsw i32 %6, 1
br label %25
23: ; preds = %17, %11
%24 = trunc i64 %13 to i32
br label %25
25: ; preds = %23, %21
%26 = phi i32 [ %22, %21 ], [ %6, %23 ]
%27 = phi i32 [ %5, %21 ], [ %24, %23 ]
%28 = add nsw i32 %27, 1
%29 = tail call i32 @strlen(i8* %0)
%30 = icmp slt i32 %28, %29
br i1 %30, label %4, label %31
31: ; preds = %25, %1
%32 = phi i32 [ 0, %1 ], [ %26, %25 ]
ret i32 %32
}