#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 strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
char *cstrstr(char *s, char *substr) {
  int i;
  if (!substr)
    return NULL;
  i = strlen(substr);
  while (s && *s) {
    if (!strncmp(s, substr, i))
      return s;
    s++;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<cstrstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1148 <cstrstr+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11c3 <cstrstr+0xa3>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x10(%rbp)
mov    %al,-0x1d(%rbp)
je     1174 <cstrstr+0x54>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1184 <cstrstr+0x64>
jmp    11bb <cstrstr+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    11aa <cstrstr+0x8a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c3 <cstrstr+0xa3>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1154 <cstrstr+0x34>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cstrstr>:
push   %rbp
push   %r14
push   %rbx
test   %rsi,%rsi
je     116b <cstrstr+0x4b>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
test   %rbx,%rbx
je     116b <cstrstr+0x4b>
cmpb   $0x0,(%rbx)
je     116b <cstrstr+0x4b>
mov    %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     116d <cstrstr+0x4d>
cmpb   $0x0,0x1(%rbx)
lea    0x1(%rbx),%rbx
jne    1150 <cstrstr+0x30>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @cstrstr(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 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
store i8* null, i8** %3
br label %35
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %6
br label %13
13: ; preds = %31, %10
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i8*, i8** %4
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 0
br label %21
21: ; preds = %16, %13
%22 = phi i1 [ false, %13 ], [ %20, %16 ]
br i1 %22, label %23, label %34
23: ; preds = %21
%24 = load i8*, i8** %4
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = call i32 @strncmp(i8* %24, i8* %25, i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = load i8*, i8** %4
store i8* %30, i8** %3
br label %35
31: ; preds = %23
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %4
br label %13
34: ; preds = %21
store i8* null, i8** %3
br label %35
35: ; preds = %34, %29, %9
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
define i8* @cstrstr(i8* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %18, label %4
4: ; preds = %2
%5 = tail call i32 @strlen(i8* %1)
%6 = icmp eq i8* %0, null
br i1 %6, label %18, label %7
7: ; preds = %4
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %7, %14
%11 = phi i8* [ %15, %14 ], [ %0, %7 ]
%12 = tail call i32 @strncmp(i8* %11, i8* %1, i32 %5)
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = getelementptr inbounds i8, i8* %11, i64 1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %10
18: ; preds = %14, %10, %7, %4, %2
%19 = phi i8* [ null, %2 ], [ null, %4 ], [ null, %7 ], [ %11, %10 ], [ null, %14 ]
ret i8* %19
}