#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 */
scalar_t__ memcmp(char const *, char const *, int);
char *scanstr2(char const *, char const *);
char const *strchr(char const *, char const);
int strlen(char const *);
char const *scanstrx(char const *tgt, char const *pat) {
  int patlen = strlen(pat);
  if (patlen == 0)
    return tgt;
  if (patlen == 1)
    return strchr(tgt, *pat);
  while ((tgt = scanstr2(tgt, pat)) && memcmp(tgt + 2, pat + 2, patlen - 2))
    ++tgt;
  return tgt;
}


x86 assembly (clang, -O0):
<scanstrx>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1173 <scanstrx+0x33>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    120c <scanstrx+0xcc>
cmpl   $0x1,-0x1c(%rbp)
jne    1196 <scanstrx+0x56>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
call   1040 <strchr@plt>
mov    %rax,-0x8(%rbp)
jmp    120c <scanstrx+0xcc>
jmp    119b <scanstrx+0x5b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <scanstr2@plt>
mov    %rax,%rcx
mov    %rcx,-0x10(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x1d(%rbp)
je     11e3 <scanstrx+0xa3>
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
mov    -0x18(%rbp),%rsi
add    $0x2,%rsi
mov    -0x1c(%rbp),%edx
sub    $0x2,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    11f3 <scanstrx+0xb3>
jmp    1204 <scanstrx+0xc4>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    119b <scanstrx+0x5b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scanstrx>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     11bc <scanstrx+0x7c>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    1176 <scanstrx+0x36>
movsbl (%r14),%esi
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <strchr@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <scanstr2@plt>
test   %rax,%rax
je     11ba <scanstrx+0x7a>
mov    %rax,%rbx
lea    0x2(%r14),%r15
add    $0xfffffffe,%ebp
lea    0x2(%rbx),%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     11bc <scanstrx+0x7c>
add    $0x1,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <scanstr2@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1190 <scanstrx+0x50>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @scanstrx(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %5
%8 = call i32 @strlen(i8* %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i8*, i8** %4
store i8* %12, i8** %3
br label %43
13: ; preds = %2
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = call i8* @strchr(i8* %17, i8 signext %19)
store i8* %20, i8** %3
br label %43
21: ; preds = %13
br label %22
22: ; preds = %38, %21
%23 = load i8*, i8** %4
%24 = load i8*, i8** %5
%25 = call i8* @scanstr2(i8* %23, i8* %24)
store i8* %25, i8** %4
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %36
27: ; preds = %22
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 2
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i64 2
%32 = load i32, i32* %6
%33 = sub nsw i32 %32, 2
%34 = call i64 @memcmp(i8* %29, i8* %31, i32 %33)
%35 = icmp ne i64 %34, 0
br label %36
36: ; preds = %27, %22
%37 = phi i1 [ false, %22 ], [ %35, %27 ]
br i1 %37, label %38, label %41
38: ; preds = %36
%39 = load i8*, i8** %4
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %4
br label %22
41: ; preds = %36
%42 = load i8*, i8** %4
store i8* %42, i8** %3
br label %43
43: ; preds = %41, %16, %11
%44 = load i8*, i8** %3
ret i8* %44
}

LLVM IR (-O2):
define i8* @scanstrx(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
switch i32 %3, label %4 [
i32 0, label %22
i32 1, label %10
]
4: ; preds = %2
%5 = tail call i8* @scanstr2(i8* %0, i8* %1)
%6 = icmp eq i8* %5, null
br i1 %6, label %22, label %7
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %1, i64 2
%9 = add nsw i32 %3, -2
br label %13
10: ; preds = %2
%11 = load i8, i8* %1
%12 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext %11)
br label %22
13: ; preds = %7, %18
%14 = phi i8* [ %5, %7 ], [ %20, %18 ]
%15 = getelementptr inbounds i8, i8* %14, i64 2
%16 = tail call i64 @memcmp(i8* %15, i8* %8, i32 %9)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = getelementptr inbounds i8, i8* %14, i64 1
%20 = tail call i8* @scanstr2(i8* %19, i8* %1)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %13
22: ; preds = %18, %13, %4, %2, %10
%23 = phi i8* [ %12, %10 ], [ %0, %2 ], [ null, %4 ], [ null, %18 ], [ %14, %13 ]
ret i8* %23
}