#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 /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strstr(char *, char *);
int count_perfect_matches(char *upstream_sequence, char *motif) {
  int i;
  char *search_string;
  char current_string[2000];
  int n = 0;
  int motif_length;
  motif_length = strlen(motif);
  strcpy(current_string, upstream_sequence);
  while (search_string != NULL) {
    search_string = strstr(current_string, motif);
    if (search_string != NULL) {
      n++;
      strcpy(current_string, search_string + motif_length);
    }
  }
  return n;
}


x86 assembly (clang, -O0):
<count_perfect_matches>:
push   %rbp
mov    %rsp,%rbp
sub    $0x800,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x7f4(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x7f8(%rbp)
lea    -0x7f0(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
cmpq   $0x0,-0x20(%rbp)
je     11c4 <count_perfect_matches+0x94>
lea    -0x7f0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11bf <count_perfect_matches+0x8f>
mov    -0x7f4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7f4(%rbp)
lea    -0x7f0(%rbp),%rdi
mov    -0x20(%rbp),%rsi
movslq -0x7f8(%rbp),%rax
add    %rax,%rsi
call   1030 <strcpy@plt>
jmp    116c <count_perfect_matches+0x3c>
mov    -0x7f4(%rbp),%eax
add    $0x800,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_perfect_matches>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x7d8,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
je     11a3 <count_perfect_matches+0x73>
movslq %r15d,%r15
xor    %ebp,%ebp
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    %r15,%rax
add    $0x1,%ebp
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
jne    1180 <count_perfect_matches+0x50>
jmp    11a5 <count_perfect_matches+0x75>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x7d8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @count_perfect_matches(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [2000 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %8
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %9
%12 = getelementptr inbounds [2000 x i8], [2000 x i8]* %7, i64 0, i64 0
%13 = load i8*, i8** %3
%14 = call i32 @strcpy(i8* %12, i8* %13)
br label %15
15: ; preds = %33, %2
%16 = load i8*, i8** %6
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %34
18: ; preds = %15
%19 = getelementptr inbounds [2000 x i8], [2000 x i8]* %7, i64 0, i64 0
%20 = load i8*, i8** %4
%21 = call i8* @strstr(i8* %19, i8* %20)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %33
24: ; preds = %18
%25 = load i32, i32* %8
%26 = add nsw i32 %25, 1
store i32 %26, i32* %8
%27 = getelementptr inbounds [2000 x i8], [2000 x i8]* %7, i64 0, i64 0
%28 = load i8*, i8** %6
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = call i32 @strcpy(i8* %27, i8* %31)
br label %33
33: ; preds = %24, %18
br label %15
34: ; preds = %15
%35 = load i32, i32* %8
ret i32 %35
}

LLVM IR (-O2):
define i32 @count_perfect_matches(i8* %0, i8* %1) {
%3 = alloca [2000 x i8]
%4 = getelementptr inbounds [2000 x i8], [2000 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* %1)
%6 = call i32 @strcpy(i8* %4, i8* %0)
%7 = sext i32 %5 to i64
%8 = call i8* @strstr(i8* %4, i8* dereferenceable(1) %1)
%9 = icmp eq i8* %8, null
br i1 %9, label %18, label %10
10: ; preds = %2, %10
%11 = phi i8* [ %16, %10 ], [ %8, %2 ]
%12 = phi i32 [ %13, %10 ], [ 0, %2 ]
%13 = add nuw nsw i32 %12, 1
%14 = getelementptr inbounds i8, i8* %11, i64 %7
%15 = call i32 @strcpy(i8* %4, i8* %14)
%16 = call i8* @strstr(i8* %4, i8* dereferenceable(1) %1)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %10
18: ; preds = %10, %2
%19 = phi i32 [ 0, %2 ], [ %13, %10 ]
ret i32 %19
}