#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 *strstr(char *, char const *);
char *StrStrGetLast(char *str1, const char *str2) {
  char *ptr = str1;
  char *prev = NULL;
  while (ptr != NULL) {
    prev = ptr;
    ptr = strstr(ptr + 1, str2);
  }
  return (prev != str1) ? prev : NULL;
}


x86 assembly (clang, -O0):
<StrStrGetLast>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     115d <StrStrGetLast+0x4d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x18(%rbp)
jmp    1130 <StrStrGetLast+0x20>
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     1178 <StrStrGetLast+0x68>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1183 <StrStrGetLast+0x73>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    1183 <StrStrGetLast+0x73>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrStrGetLast>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
test   %rdi,%rdi
je     1146 <StrStrGetLast+0x36>
mov    %rsi,%r15
mov    %r14,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rax,%rbx
lea    0x1(%rax),%rdi
mov    %r15,%rsi
call   1030 <strstr@plt>
test   %rax,%rax
jne    1130 <StrStrGetLast+0x20>
jmp    1148 <StrStrGetLast+0x38>
xor    %ebx,%ebx
xor    %eax,%eax
cmp    %r14,%rbx
cmovne %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @StrStrGetLast(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
store i8* %7, i8** %5
store i8* null, i8** %6
br label %8
8: ; preds = %11, %2
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = load i8*, i8** %5
store i8* %12, i8** %6
%13 = load i8*, i8** %5
%14 = getelementptr inbounds i8, i8* %13, i64 1
%15 = load i8*, i8** %4
%16 = call i8* @strstr(i8* %14, i8* %15)
store i8* %16, i8** %5
br label %8
17: ; preds = %8
%18 = load i8*, i8** %6
%19 = load i8*, i8** %3
%20 = icmp ne i8* %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i8*, i8** %6
br label %24
23: ; preds = %17
br label %24
24: ; preds = %23, %21
%25 = phi i8* [ %22, %21 ], [ null, %23 ]
ret i8* %25
}

LLVM IR (-O2):
define i8* @StrStrGetLast(i8* %0, i8* nocapture %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %9, label %4
4: ; preds = %2, %4
%5 = phi i8* [ %7, %4 ], [ %0, %2 ]
%6 = getelementptr inbounds i8, i8* %5, i64 1
%7 = tail call i8* @strstr(i8* %6, i8* dereferenceable(1) %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %4
9: ; preds = %4, %2
%10 = phi i8* [ null, %2 ], [ %5, %4 ]
%11 = icmp eq i8* %10, %0
%12 = select i1 %11, i8* null, i8* %10
ret i8* %12
}