#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 *matchNth(char *message, char *target, int which) {
  int idx;
  char *start = message;
  for (idx = 0; idx < which; idx++) {
    start = strstr(start, target) + 1;
    if (start == NULL) {
      return NULL;
    }
  }
  --start;
  start[0] = '\000';
  return start;
}


x86 assembly (clang, -O0):
<matchNth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    117e <matchNth+0x6e>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strstr@plt>
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    116b <matchNth+0x5b>
movq   $0x0,-0x8(%rbp)
jmp    1199 <matchNth+0x89>
jmp    1170 <matchNth+0x60>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1132 <matchNth+0x22>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matchNth>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rax
test   %edx,%edx
jle    1134 <matchNth+0x24>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strstr@plt>
add    $0x1,%rax
add    $0xffffffff,%ebx
jne    1120 <matchNth+0x10>
movb   $0x0,-0x1(%rax)
add    $0xffffffffffffffff,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @matchNth(i8* %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %12
5: ; preds = %3, %5
%6 = phi i8* [ %9, %5 ], [ %0, %3 ]
%7 = phi i32 [ %10, %5 ], [ 0, %3 ]
%8 = tail call i8* @strstr(i8* dereferenceable(1) %6, i8* dereferenceable(1) %1)
%9 = getelementptr inbounds i8, i8* %8, i64 1
%10 = add nuw nsw i32 %7, 1
%11 = icmp eq i32 %10, %2
br i1 %11, label %12, label %5
12: ; preds = %5, %3
%13 = phi i8* [ %0, %3 ], [ %9, %5 ]
%14 = getelementptr inbounds i8, i8* %13, i64 -1
store i8 0, i8* %14
ret i8* %14
}