#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 *);
char *strstr(char *, char *);
int mfm_subs_count(char *orig, int files_len) {
  int res = strlen(orig);
  char *cur = orig;
  while (cur = strstr(cur, "%f")) {
    if (cur == orig) {
      res = res - 2 + files_len;
    } else if (cur[-1] == '%') {
      res--;
    } else {
      res = res - 2 + files_len;
    }
    cur += 2;
  }
  return res;
}


x86 assembly (clang, -O0):
<mfm_subs_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11c1 <mfm_subs_count+0xa1>
mov    -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    1180 <mfm_subs_count+0x60>
mov    -0x10(%rbp),%eax
sub    $0x2,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11b0 <mfm_subs_count+0x90>
mov    -0x18(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x25,%eax
jne    119f <mfm_subs_count+0x7f>
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
jmp    11ab <mfm_subs_count+0x8b>
mov    -0x10(%rbp),%eax
sub    $0x2,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11b0 <mfm_subs_count+0x90>
mov    -0x18(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    1143 <mfm_subs_count+0x23>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mfm_subs_count>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ebp
lea    0xec5(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strstr@plt>
test   %rax,%rax
je     1187 <mfm_subs_count+0x67>
add    $0xfffffffe,%r14d
lea    0xead(%rip),%r15
jmp    1177 <mfm_subs_count+0x57>
cs nopw 0x0(%rax,%rax,1)
nop
add    %r14d,%ebp
add    $0x2,%rax
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     1187 <mfm_subs_count+0x67>
cmp    %rbx,%rax
je     1160 <mfm_subs_count+0x40>
cmpb   $0x25,-0x1(%rax)
jne    1160 <mfm_subs_count+0x40>
add    $0xffffffff,%ebp
jmp    1163 <mfm_subs_count+0x43>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%f\00"
define i32 @mfm_subs_count(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %5
%9 = load i8*, i8** %3
store i8* %9, i8** %6
br label %10
10: ; preds = %38, %2
%11 = load i8*, i8** %6
%12 = call i8* @strstr(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %41
14: ; preds = %10
%15 = load i8*, i8** %6
%16 = load i8*, i8** %3
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i32, i32* %5
%20 = sub nsw i32 %19, 2
%21 = load i32, i32* %4
%22 = add nsw i32 %20, %21
store i32 %22, i32* %5
br label %38
23: ; preds = %14
%24 = load i8*, i8** %6
%25 = getelementptr inbounds i8, i8* %24, i64 -1
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 37
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* %5
%31 = add nsw i32 %30, -1
store i32 %31, i32* %5
br label %37
32: ; preds = %23
%33 = load i32, i32* %5
%34 = sub nsw i32 %33, 2
%35 = load i32, i32* %4
%36 = add nsw i32 %34, %35
store i32 %36, i32* %5
br label %37
37: ; preds = %32, %29
br label %38
38: ; preds = %37, %18
%39 = load i8*, i8** %6
%40 = getelementptr inbounds i8, i8* %39, i64 2
store i8* %40, i8** %6
br label %10
41: ; preds = %10
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
@.str = [3 x i8] c"%f\00"
define i32 @mfm_subs_count(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %27, label %6
6: ; preds = %2
%7 = add i32 %1, -2
br label %8
8: ; preds = %6, %22
%9 = phi i8* [ %4, %6 ], [ %25, %22 ]
%10 = phi i32 [ %3, %6 ], [ %23, %22 ]
%11 = icmp eq i8* %9, %0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = add i32 %7, %10
br label %22
14: ; preds = %8
%15 = getelementptr inbounds i8, i8* %9, i64 -1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 37
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = add nsw i32 %10, -1
br label %22
20: ; preds = %14
%21 = add i32 %7, %10
br label %22
22: ; preds = %18, %20, %12
%23 = phi i32 [ %13, %12 ], [ %19, %18 ], [ %21, %20 ]
%24 = getelementptr inbounds i8, i8* %9, i64 2
%25 = tail call i8* @strstr(i8* dereferenceable(1) %24, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %8
27: ; preds = %22, %2
%28 = phi i32 [ %3, %2 ], [ %23, %22 ]
ret i32 %28
}