#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 *, int, char *);
int /*<<< orphan*/ puts(char *);
char *my_strnchr(char const *str, int ch, int which) {
  char *location = NULL;
  int nr = 0;
  while (*str != '\0') {
    if (*str == ch) {
      nr++;
      location = str;
    }
    str++;
  }
  if (nr > 0 && nr < which) {
    puts("Unfortunately,");
    printf("\tThe last \"%c\" was at %p.\n", ch, location);
  }
  return location;
}


x86 assembly (clang, -O0):
<my_strnchr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1183 <my_strnchr+0x63>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    -0xc(%rbp),%eax
jne    1172 <my_strnchr+0x52>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1141 <my_strnchr+0x21>
cmpl   $0x0,-0x1c(%rbp)
jle    11b8 <my_strnchr+0x98>
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11b8 <my_strnchr+0x98>
lea    0xe60(%rip),%rdi
call   1030 <puts@plt>
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_strnchr>:
push   %rbp
push   %rbx
push   %rax
mov    (%rdi),%cl
xor    %eax,%eax
test   %cl,%cl
je     1174 <my_strnchr+0x54>
mov    %esi,%ebp
xor    %ebx,%ebx
nop
movsbl %cl,%ecx
xor    %esi,%esi
cmp    %ebp,%ecx
sete   %sil
cmove  %rdi,%rbx
add    %esi,%eax
movzbl 0x1(%rdi),%ecx
add    $0x1,%rdi
test   %cl,%cl
jne    1130 <my_strnchr+0x10>
test   %eax,%eax
je     1176 <my_strnchr+0x56>
cmp    %edx,%eax
jge    1176 <my_strnchr+0x56>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
lea    0xea7(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
jmp    1176 <my_strnchr+0x56>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Unfortunately,\00"
@.str.1 = [27 x i8] c"\09The last \22%c\22 was at %p.\0A\00"
define i8* @my_strnchr(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i8* null, i8** %7
store i32 0, i32* %8
br label %9
9: ; preds = %24, %3
%10 = load i8*, i8** %4
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %9
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = load i32, i32* %5
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* %8
%22 = add nsw i32 %21, 1
store i32 %22, i32* %8
%23 = load i8*, i8** %4
store i8* %23, i8** %7
br label %24
24: ; preds = %20, %14
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %4
br label %9
27: ; preds = %9
%28 = load i32, i32* %8
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %27
%31 = load i32, i32* %8
%32 = load i32, i32* %6
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = call i32 @puts(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%36 = load i32, i32* %5
%37 = load i8*, i8** %7
%38 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %36, i8* %37)
br label %39
39: ; preds = %34, %30, %27
%40 = load i8*, i8** %7
ret i8* %40
}

LLVM IR (-O2):
@.str = [15 x i8] c"Unfortunately,\00"
@.str.1 = [27 x i8] c"\09The last \22%c\22 was at %p.\0A\00"
define i8* @my_strnchr(i8* %0, i32 %1, i32 %2) {
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %26, label %6
6: ; preds = %3, %6
%7 = phi i8 [ %17, %6 ], [ %4, %3 ]
%8 = phi i32 [ %15, %6 ], [ 0, %3 ]
%9 = phi i8* [ %13, %6 ], [ null, %3 ]
%10 = phi i8* [ %16, %6 ], [ %0, %3 ]
%11 = sext i8 %7 to i32
%12 = icmp eq i32 %11, %1
%13 = select i1 %12, i8* %10, i8* %9
%14 = zext i1 %12 to i32
%15 = add nuw nsw i32 %8, %14
%16 = getelementptr inbounds i8, i8* %10, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %19, label %6
19: ; preds = %6
%20 = icmp ne i32 %15, 0
%21 = icmp slt i32 %15, %2
%22 = and i1 %20, %21
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%25 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %1, i8* %13)
br label %26
26: ; preds = %3, %23, %19
%27 = phi i8* [ %13, %23 ], [ %13, %19 ], [ null, %3 ]
ret i8* %27
}