#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 */
char *strstr(char *, char *);
char *nthsub(char *haystack, char *needle, int n) {
  int count = 0;
  char *p;
  p = strstr(haystack, needle);
  while ((p != NULL) && (count < n)) {
    count++;
    p = strstr(p + 1, needle);
  }
  return p;
}


x86 assembly (clang, -O0):
<nthsub>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x21(%rbp)
je     1157 <nthsub+0x47>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1167 <nthsub+0x57>
jmp    118a <nthsub+0x7a>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x20(%rbp)
jmp    113b <nthsub+0x2b>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nthsub>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
call   1030 <strstr@plt>
test   %rax,%rax
je     114e <nthsub+0x3e>
test   %r14d,%r14d
jle    114e <nthsub+0x3e>
mov    $0x1,%ebp
xchg   %ax,%ax
add    $0x1,%rax
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strstr@plt>
test   %rax,%rax
je     114e <nthsub+0x3e>
lea    0x1(%rbp),%ecx
cmp    %r14d,%ebp
mov    %ecx,%ebp
jl     1130 <nthsub+0x20>
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @nthsub(i8* %0, i8* nocapture %1, i32 %2) {
%4 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%5 = icmp ne i8* %4, null
%6 = icmp sgt i32 %2, 0
%7 = and i1 %5, %6
br i1 %7, label %8, label %17
8: ; preds = %3, %8
%9 = phi i8* [ %13, %8 ], [ %4, %3 ]
%10 = phi i32 [ %11, %8 ], [ 0, %3 ]
%11 = add nuw nsw i32 %10, 1
%12 = getelementptr inbounds i8, i8* %9, i64 1
%13 = tail call i8* @strstr(i8* %12, i8* dereferenceable(1) %1)
%14 = icmp ne i8* %13, null
%15 = icmp slt i32 %11, %2
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %8, label %17
17: ; preds = %8, %3
%18 = phi i8* [ %4, %3 ], [ %13, %8 ]
ret i8* %18
}