#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 */
char *mystrstr(char *, char *);
int /*<<< orphan*/ printf(char *, char *, char *, char *);
void test5() {
  char *hay;
  char *needle;
  char *result;
  hay = "Hello world. CS240";
  needle = "ld";
  result = mystrstr(hay, needle);
  printf("mystrstr(\"%s\",\"%s\")=\"%s\"\n", hay, needle,
         result ? result : "NULL");
  hay = "Hello world. CS240";
  needle = "llo";
  result = mystrstr(hay, needle);
  printf("mystrstr(\"%s\",\"%s\")=\"%s\"\n", hay, needle,
         result ? result : "NULL");
  hay = "Hello world. CS240";
  needle = "CS251";
  result = mystrstr(hay, needle);
  printf("mystrstr(\"%s\",\"%s\")=\"%s\"\n", hay, needle,
         result ? result : "NULL");
  needle = "Hello world. CS240";
  hay = "CS240";
  result = mystrstr(hay, needle);
  printf("mystrstr(\"%s\",\"%s\")=\"%s\"\n", hay, needle,
         result ? result : "NULL");
}


x86 assembly (clang, -O0):
<test5>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
lea    0xece(%rip),%rax
mov    %rax,-0x8(%rbp)
lea    0xed6(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <mystrstr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     117a <test5+0x5a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    118a <test5+0x6a>
lea    0xeaf(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    118a <test5+0x6a>
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rcx
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
lea    0xe57(%rip),%rax
mov    %rax,-0x8(%rbp)
lea    0xe81(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <mystrstr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f1 <test5+0xd1>
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    1201 <test5+0xe1>
lea    0xe38(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    1201 <test5+0xe1>
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rcx
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
lea    0xde0(%rip),%rax
mov    %rax,-0x8(%rbp)
lea    0xe0e(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <mystrstr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1268 <test5+0x148>
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
jmp    1278 <test5+0x158>
lea    0xdc1(%rip),%rax
mov    %rax,-0x60(%rbp)
jmp    1278 <test5+0x158>
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rsi
mov    -0x60(%rbp),%rcx
lea    0xd8b(%rip),%rdi
call   1040 <printf@plt>
lea    0xd69(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xd6b(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <mystrstr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12df <test5+0x1bf>
mov    -0x18(%rbp),%rax
mov    %rax,-0x78(%rbp)
jmp    12ef <test5+0x1cf>
lea    0xd4a(%rip),%rax
mov    %rax,-0x78(%rbp)
jmp    12ef <test5+0x1cf>
mov    -0x68(%rbp),%rdx
mov    -0x70(%rbp),%rsi
mov    -0x78(%rbp),%rcx
lea    0xd14(%rip),%rdi
call   1040 <printf@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test5>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xed1(%rip),%r14
lea    0xedd(%rip),%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <mystrstr@plt>
test   %rax,%rax
lea    0xee5(%rip),%rbx
cmove  %rbx,%rax
lea    0xec0(%rip),%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %rax,%rcx
call   1040 <printf@plt>
lea    0xec7(%rip),%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <mystrstr@plt>
test   %rax,%rax
cmove  %rbx,%rax
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %rax,%rcx
call   1040 <printf@plt>
lea    0xea1(%rip),%r12
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <mystrstr@plt>
test   %rax,%rax
cmove  %rbx,%rax
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
mov    %rax,%rcx
call   1040 <printf@plt>
lea    0xe4b(%rip),%r12
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <mystrstr@plt>
test   %rax,%rax
cmovne %rax,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Hello world. CS240\00"
@.str.1 = [3 x i8] c"ld\00"
@.str.2 = [26 x i8] c"mystrstr(\22%s\22,\22%s\22)=\22%s\22\0A\00"
@.str.3 = [5 x i8] c"NULL\00"
@.str.4 = [4 x i8] c"llo\00"
@.str.5 = [6 x i8] c"CS251\00"
@.str.6 = [6 x i8] c"CS240\00"
define void @test5() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %1
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %2
%4 = load i8*, i8** %1
%5 = load i8*, i8** %2
%6 = call i8* @mystrstr(i8* %4, i8* %5)
store i8* %6, i8** %3
%7 = load i8*, i8** %1
%8 = load i8*, i8** %2
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = load i8*, i8** %3
br label %14
13: ; preds = %0
br label %14
14: ; preds = %13, %11
%15 = phi i8* [ %12, %11 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %13 ]
%16 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %7, i8* %8, i8* %15)
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %1
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8** %2
%17 = load i8*, i8** %1
%18 = load i8*, i8** %2
%19 = call i8* @mystrstr(i8* %17, i8* %18)
store i8* %19, i8** %3
%20 = load i8*, i8** %1
%21 = load i8*, i8** %2
%22 = load i8*, i8** %3
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = load i8*, i8** %3
br label %27
26: ; preds = %14
br label %27
27: ; preds = %26, %24
%28 = phi i8* [ %25, %24 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %26 ]
%29 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* %21, i8* %28)
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8** %2
%30 = load i8*, i8** %1
%31 = load i8*, i8** %2
%32 = call i8* @mystrstr(i8* %30, i8* %31)
store i8* %32, i8** %3
%33 = load i8*, i8** %1
%34 = load i8*, i8** %2
%35 = load i8*, i8** %3
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = load i8*, i8** %3
br label %40
39: ; preds = %27
br label %40
40: ; preds = %39, %37
%41 = phi i8* [ %38, %37 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %39 ]
%42 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %33, i8* %34, i8* %41)
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8** %1
%43 = load i8*, i8** %1
%44 = load i8*, i8** %2
%45 = call i8* @mystrstr(i8* %43, i8* %44)
store i8* %45, i8** %3
%46 = load i8*, i8** %1
%47 = load i8*, i8** %2
%48 = load i8*, i8** %3
%49 = icmp ne i8* %48, null
br i1 %49, label %50, label %52
50: ; preds = %40
%51 = load i8*, i8** %3
br label %53
52: ; preds = %40
br label %53
53: ; preds = %52, %50
%54 = phi i8* [ %51, %50 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), %52 ]
%55 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %46, i8* %47, i8* %54)
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"Hello world. CS240\00"
@.str.1 = [3 x i8] c"ld\00"
@.str.2 = [26 x i8] c"mystrstr(\22%s\22,\22%s\22)=\22%s\22\0A\00"
@.str.3 = [5 x i8] c"NULL\00"
@.str.4 = [4 x i8] c"llo\00"
@.str.5 = [6 x i8] c"CS251\00"
@.str.6 = [6 x i8] c"CS240\00"
define void @test5() {
%1 = tail call i8* @mystrstr(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i8* %1, null
%3 = select i1 %2, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %1
%4 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%5 = tail call i8* @mystrstr(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%6 = icmp eq i8* %5, null
%7 = select i1 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %5
%8 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %7)
%9 = tail call i8* @mystrstr(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%10 = icmp eq i8* %9, null
%11 = select i1 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %9
%12 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %11)
%13 = tail call i8* @mystrstr(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i8* %13, null
%15 = select i1 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %13
%16 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %15)
ret void
}