#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 */
typedef int /*<<< orphan*/ wchar_t;

/* Variables and functions */
int /*<<< orphan*/ MB_CUR_MAX;
int mbtowc(int /*<<< orphan*/ *, char const *, int /*<<< orphan*/);
scalar_t__ wcsncasecmp(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
const char *strcasechr(const char *s1, const char *s2) {
  wchar_t wc1, wc2;
  switch (mbtowc(&wc2, s2, MB_CUR_MAX)) {
  case -1:
    mbtowc(NULL, NULL, MB_CUR_MAX);
  case 0:
    return NULL;
  }
  for (; *s1 != '\0'; s1++)
    if (mbtowc(&wc1, s1, MB_CUR_MAX) == -1)
      mbtowc(NULL, NULL, MB_CUR_MAX);
    else if (wcsncasecmp(&wc1, &wc2, 1) == 0)
      return s1;
  return NULL;
}


x86 assembly (clang, -O0):
<strcasechr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    0x2ebd(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rdi
call   1040 <mbtowc@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
sub    $0xffffffff,%eax
je     1169 <strcasechr+0x49>
jmp    1159 <strcasechr+0x39>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     117e <strcasechr+0x5e>
jmp    118b <strcasechr+0x6b>
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
mov    %rsi,%rdi
call   1040 <mbtowc@plt>
movq   $0x0,-0x8(%rbp)
jmp    1225 <strcasechr+0x105>
jmp    1190 <strcasechr+0x70>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     121d <strcasechr+0xfd>
mov    -0x10(%rbp),%rsi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rdi
call   1040 <mbtowc@plt>
cmp    $0xffffffff,%eax
jne    11d9 <strcasechr+0xb9>
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
mov    %rsi,%rdi
call   1040 <mbtowc@plt>
jmp    1207 <strcasechr+0xe7>
lea    -0x1c(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1030 <wcsncasecmp@plt>
cmp    $0x0,%rax
jne    1202 <strcasechr+0xe2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1225 <strcasechr+0x105>
jmp    1207 <strcasechr+0xe7>
jmp    120c <strcasechr+0xec>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1190 <strcasechr+0x70>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strcasechr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2ec6(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rdi
call   1040 <mbtowc@plt>
cmp    $0xffffffff,%eax
je     11a6 <strcasechr+0x86>
test   %eax,%eax
je     11a2 <strcasechr+0x82>
cmpb   $0x0,(%rbx)
je     11a2 <strcasechr+0x82>
lea    0x4(%rsp),%r14
mov    %rsp,%r15
jmp    1177 <strcasechr+0x57>
cs nopw 0x0(%rax,%rax,1)
mov    (%r12),%edx
xor    %edi,%edi
xor    %esi,%esi
call   1040 <mbtowc@plt>
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
je     11a2 <strcasechr+0x82>
mov    (%r12),%edx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <mbtowc@plt>
cmp    $0xffffffff,%eax
je     1160 <strcasechr+0x40>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <wcsncasecmp@plt>
test   %rax,%rax
jne    116d <strcasechr+0x4d>
jmp    11b5 <strcasechr+0x95>
xor    %ebx,%ebx
jmp    11b5 <strcasechr+0x95>
mov    (%r12),%edx
xor    %ebx,%ebx
xor    %edi,%edi
xor    %esi,%esi
call   1040 <mbtowc@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@MB_CUR_MAX = global i32 0
define i8* @strcasechr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = load i32, i32* @MB_CUR_MAX
%10 = call i32 @mbtowc(i32* %7, i8* %8, i32 %9)
switch i32 %10, label %15 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %2
%12 = load i32, i32* @MB_CUR_MAX
%13 = call i32 @mbtowc(i32* null, i8* null, i32 %12)
br label %14
14: ; preds = %2, %11
store i8* null, i8** %3
br label %40
15: ; preds = %2
br label %16
16: ; preds = %36, %15
%17 = load i8*, i8** %4
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %39
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = load i32, i32* @MB_CUR_MAX
%24 = call i32 @mbtowc(i32* %6, i8* %22, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @MB_CUR_MAX
%28 = call i32 @mbtowc(i32* null, i8* null, i32 %27)
br label %35
29: ; preds = %21
%30 = call i64 @wcsncasecmp(i32* %6, i32* %7, i32 1)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i8*, i8** %4
store i8* %33, i8** %3
br label %40
34: ; preds = %29
br label %35
35: ; preds = %34, %26
br label %36
36: ; preds = %35
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %4
br label %16
39: ; preds = %16
store i8* null, i8** %3
br label %40
40: ; preds = %39, %32, %14
%41 = load i8*, i8** %3
ret i8* %41
}

LLVM IR (-O2):
@MB_CUR_MAX = global i32 0
define i8* @strcasechr(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @MB_CUR_MAX
%8 = call i32 @mbtowc(i32* %4, i8* %1, i32 %7)
switch i32 %8, label %9 [
i32 -1, label %12
i32 0, label %30
]
9: ; preds = %2
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 0
br i1 %11, label %30, label %15
12: ; preds = %2
%13 = load i32, i32* @MB_CUR_MAX
%14 = call i32 @mbtowc(i32* null, i8* null, i32 %13)
br label %30
15: ; preds = %9, %26
%16 = phi i8* [ %27, %26 ], [ %0, %9 ]
%17 = load i32, i32* @MB_CUR_MAX
%18 = call i32 @mbtowc(i32* %3, i8* %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* @MB_CUR_MAX
%22 = call i32 @mbtowc(i32* null, i8* null, i32 %21)
br label %26
23: ; preds = %15
%24 = call i64 @wcsncasecmp(i32* %3, i32* %4, i32 1)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %20, %23
%27 = getelementptr inbounds i8, i8* %16, i64 1
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %15
30: ; preds = %23, %26, %9, %12, %2
%31 = phi i8* [ null, %2 ], [ null, %12 ], [ null, %9 ], [ %16, %23 ], [ null, %26 ]
ret i8* %31
}