#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*/ printf(char *);
char *strchr(char *, char const);
char *my_nstrchr(const char chr, char *str, int n) {
  if (n < 1) {
    printf("my_nstrchr need n>=1\n");
    return NULL;
  }
  char *p1, *p2;
  int i = 1;
  p1 = str;
  do {
    p2 = strchr(p1, chr);
    p1 = p2;
    p1++;
    i++;
  } while (p2 != NULL && i <= n);
  if (i < n) {
    return NULL;
  }
  return p2;
}


x86 assembly (clang, -O0):
<my_nstrchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dil,%al
mov    %al,-0x9(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    1158 <my_nstrchr+0x38>
lea    0xeba(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11dd <my_nstrchr+0xbd>
movl   $0x1,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
movsbl -0x9(%rbp),%esi
call   1030 <strchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x30(%rbp)
mov    %al,-0x35(%rbp)
je     11b1 <my_nstrchr+0x91>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
setle  %al
mov    %al,-0x35(%rbp)
mov    -0x35(%rbp),%al
test   $0x1,%al
jne    1167 <my_nstrchr+0x47>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11d5 <my_nstrchr+0xb5>
movq   $0x0,-0x8(%rbp)
jmp    11dd <my_nstrchr+0xbd>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_nstrchr>:
push   %rbp
push   %r14
push   %rbx
test   %edx,%edx
jle    1168 <my_nstrchr+0x48>
mov    %edx,%r14d
mov    $0x1,%ebx
movsbl %dil,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rsi,%rdi
mov    %ebp,%esi
call   1040 <strchr@plt>
lea    0x1(%rbx),%ecx
test   %rax,%rax
je     115d <my_nstrchr+0x3d>
lea    0x1(%rax),%rsi
cmp    %r14d,%ebx
mov    %ecx,%ebx
jl     1140 <my_nstrchr+0x20>
xor    %edx,%edx
cmp    %r14d,%ecx
cmovl  %rdx,%rax
jmp    1176 <my_nstrchr+0x56>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"my_nstrchr need n>=1\0A\00"
define i8* @my_nstrchr(i8 signext %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i8 %0, i8* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = icmp slt i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %42
15: ; preds = %3
store i32 1, i32* %10
%16 = load i8*, i8** %6
store i8* %16, i8** %8
br label %17
17: ; preds = %33, %15
%18 = load i8*, i8** %8
%19 = load i8, i8* %5
%20 = call i8* @strchr(i8* %18, i8 signext %19)
store i8* %20, i8** %9
%21 = load i8*, i8** %9
store i8* %21, i8** %8
%22 = load i8*, i8** %8
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %8
%24 = load i32, i32* %10
%25 = add nsw i32 %24, 1
store i32 %25, i32* %10
br label %26
26: ; preds = %17
%27 = load i8*, i8** %9
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* %10
%31 = load i32, i32* %7
%32 = icmp sle i32 %30, %31
br label %33
33: ; preds = %29, %26
%34 = phi i1 [ false, %26 ], [ %32, %29 ]
br i1 %34, label %17, label %35
35: ; preds = %33
%36 = load i32, i32* %10
%37 = load i32, i32* %7
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
store i8* null, i8** %4
br label %42
40: ; preds = %35
%41 = load i8*, i8** %9
store i8* %41, i8** %4
br label %42
42: ; preds = %40, %39, %13
%43 = load i8*, i8** %4
ret i8* %43
}

LLVM IR (-O2):
@str = [21 x i8] c"my_nstrchr need n>=1\00"
define i8* @my_nstrchr(i8 signext %0, i8* %1, i32 %2) {
%4 = icmp slt i32 %2, 1
br i1 %4, label %5, label %7
5: ; preds = %3
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %19
7: ; preds = %3, %7
%8 = phi i8* [ %11, %7 ], [ %1, %3 ]
%9 = phi i32 [ %12, %7 ], [ 1, %3 ]
%10 = tail call i8* @strchr(i8* dereferenceable(1) %8, i8 signext %0)
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = add nuw nsw i32 %9, 1
%13 = icmp ne i8* %10, null
%14 = icmp slt i32 %9, %2
%15 = select i1 %13, i1 %14, i1 false
br i1 %15, label %7, label %16
16: ; preds = %7
%17 = icmp slt i32 %12, %2
%18 = select i1 %17, i8* null, i8* %10
br label %19
19: ; preds = %16, %5
%20 = phi i8* [ null, %5 ], [ %18, %16 ]
ret i8* %20
}