#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 *strchr(char const *, char);
const char *find_nth_chr(const char *str, char c, int n) {
  str--;
  while (n-- > 0 && (str = strchr(str + 1, c)) != NULL) {
  }
  return str;
}


x86 assembly (clang, -O0):
<find_nth_chr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x11(%rbp)
jle    1169 <find_nth_chr+0x59>
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
movsbl -0x9(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1179 <find_nth_chr+0x69>
jmp    117e <find_nth_chr+0x6e>
jmp    1131 <find_nth_chr+0x21>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_nth_chr>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rdi,%rax
add    $0xffffffffffffffff,%rax
add    $0x1,%ebx
movsbl %sil,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffff,%ebx
test   %ebx,%ebx
jle    114c <find_nth_chr+0x3c>
add    $0x1,%rax
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    1130 <find_nth_chr+0x20>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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