#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 *);
char *strstr(char const *, char const *);
int strfragnum(const char *str, const char *delimiter) {
  const char *p, *q;
  int num = 0;
  int len;
  len = strlen(delimiter);
  p = str;
  while ((q = strstr(p, delimiter)) != NULL) {
    num++;
    p = q;
    p += len;
  }
  if (*p)
    num++;
  return num;
}


x86 assembly (clang, -O0):
<strfragnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     118d <strfragnum+0x6d>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    114b <strfragnum+0x2b>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11a3 <strfragnum+0x83>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strfragnum>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
je     116b <strfragnum+0x4b>
movslq %ebp,%r15
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %rax,%rbx
add    %r15,%rbx
add    $0x1,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strstr@plt>
test   %rax,%rax
jne    1150 <strfragnum+0x30>
jmp    116d <strfragnum+0x4d>
xor    %ebp,%ebp
cmpb   $0x1,(%rbx)
sbb    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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