#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 *libc_malloc(int);
int /*<<< orphan*/ libc_strcpy(char *, char *);
int libc_strlen(char *);
int /*<<< orphan*/ libc_strncat(char *, char *, int);
char *libc_strstr(char *, char *);
char *libc_strnstr(char *str, char *ser) {
  if (libc_strstr(str, ser)) {
    int len_ser = libc_strlen(ser), len_str = libc_strlen(str), len_nstr;
    char *buff_strn = libc_malloc(libc_strlen(str));
    libc_strcpy(buff_strn, "\0");
    char *estr = libc_strstr(str, ser) + len_ser;
    int iestr = libc_strlen(estr);
    len_nstr = len_str - iestr;
    libc_strncat(buff_strn, str, len_nstr);
    char *out = buff_strn;
    return out;
  } else {
    return "";
  }
}


x86 assembly (clang, -O0):
<libc_strnstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <libc_strstr@plt>
cmp    $0x0,%rax
je     1205 <libc_strnstr+0xb5>
mov    -0x18(%rbp),%rdi
call   1040 <libc_strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <libc_strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <libc_strlen@plt>
mov    %eax,%edi
call   1070 <libc_malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   1060 <libc_strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <libc_strstr@plt>
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <libc_strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x20(%rbp),%eax
sub    -0x3c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <libc_strncat@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1210 <libc_strnstr+0xc0>
lea    0xdf6(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<libc_strnstr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <libc_strstr@plt>
test   %rax,%rax
je     11c8 <libc_strnstr+0x78>
mov    %r14,%rdi
call   1040 <libc_strlen@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   1040 <libc_strlen@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1040 <libc_strlen@plt>
mov    %eax,%edi
call   1070 <libc_malloc@plt>
mov    %rax,%rbp
lea    0xe69(%rip),%rsi
mov    %rax,%rdi
call   1060 <libc_strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <libc_strstr@plt>
movslq %r12d,%rdi
add    %rax,%rdi
call   1040 <libc_strlen@plt>
sub    %eax,%r15d
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1050 <libc_strncat@plt>
jmp    11cf <libc_strnstr+0x7f>
lea    0xe33(%rip),%rbp
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] zeroinitializer
@.str.1 = [1 x i8] zeroinitializer
define i8* @libc_strnstr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = load i8*, i8** %4
%14 = load i8*, i8** %5
%15 = call i8* @libc_strstr(i8* %13, i8* %14)
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %44
17: ; preds = %2
%18 = load i8*, i8** %5
%19 = call i32 @libc_strlen(i8* %18)
store i32 %19, i32* %6
%20 = load i8*, i8** %4
%21 = call i32 @libc_strlen(i8* %20)
store i32 %21, i32* %7
%22 = load i8*, i8** %4
%23 = call i32 @libc_strlen(i8* %22)
%24 = call i8* @libc_malloc(i32 %23)
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = call i32 @libc_strcpy(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = load i8*, i8** %4
%28 = load i8*, i8** %5
%29 = call i8* @libc_strstr(i8* %27, i8* %28)
%30 = load i32, i32* %6
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8* %32, i8** %10
%33 = load i8*, i8** %10
%34 = call i32 @libc_strlen(i8* %33)
store i32 %34, i32* %11
%35 = load i32, i32* %7
%36 = load i32, i32* %11
%37 = sub nsw i32 %35, %36
store i32 %37, i32* %8
%38 = load i8*, i8** %9
%39 = load i8*, i8** %4
%40 = load i32, i32* %8
%41 = call i32 @libc_strncat(i8* %38, i8* %39, i32 %40)
%42 = load i8*, i8** %9
store i8* %42, i8** %12
%43 = load i8*, i8** %12
store i8* %43, i8** %3
br label %45
44: ; preds = %2
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), i8** %3
br label %45
45: ; preds = %44, %17
%46 = load i8*, i8** %3
ret i8* %46
}

LLVM IR (-O2):
@.str = [2 x i8] zeroinitializer
@.str.1 = [1 x i8] zeroinitializer
define i8* @libc_strnstr(i8* %0, i8* %1) {
%3 = tail call i8* @libc_strstr(i8* %0, i8* %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %17, label %5
5: ; preds = %2
%6 = tail call i32 @libc_strlen(i8* %1)
%7 = tail call i32 @libc_strlen(i8* %0)
%8 = tail call i32 @libc_strlen(i8* %0)
%9 = tail call i8* @libc_malloc(i32 %8)
%10 = tail call i32 @libc_strcpy(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = tail call i8* @libc_strstr(i8* %0, i8* %1)
%12 = sext i32 %6 to i64
%13 = getelementptr inbounds i8, i8* %11, i64 %12
%14 = tail call i32 @libc_strlen(i8* %13)
%15 = sub nsw i32 %7, %14
%16 = tail call i32 @libc_strncat(i8* %9, i8* %0, i32 %15)
br label %17
17: ; preds = %2, %5
%18 = phi i8* [ %9, %5 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0), %2 ]
ret i8* %18
}