#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__ malloc(scalar_t__);
int /*<<< orphan*/ memcpy(char *, char *, int);
scalar_t__ strlen(char *);
char *strstr(char *, char *);
char *extract_substring_before_str(char *base_string, char *threshold_string) {
  int num = strstr(base_string, threshold_string) - base_string;
  char *substr = (char *)malloc(strlen(base_string) + 1);
  memcpy(substr, base_string, num + 1);
  substr[num] = '\0';
  return substr;
}


x86 assembly (clang, -O0):
<extract_substring_before_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strstr@plt>
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_substring_before_str>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1060 <strstr@plt>
mov    %rax,%r15
sub    %r14,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0x1(%r15),%edx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @extract_substring_before_str(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = load i8*, i8** %4
%9 = call i8* @strstr(i8* %7, i8* %8)
%10 = load i8*, i8** %3
%11 = ptrtoint i8* %9 to i64
%12 = ptrtoint i8* %10 to i64
%13 = sub i64 %11, %12
%14 = trunc i64 %13 to i32
store i32 %14, i32* %5
%15 = load i8*, i8** %3
%16 = call i64 @strlen(i8* %15)
%17 = add nsw i64 %16, 1
%18 = call i64 @malloc(i64 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = load i8*, i8** %3
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
%24 = call i32 @memcpy(i8* %20, i8* %21, i32 %23)
%25 = load i8*, i8** %6
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %6
ret i8* %29
}

LLVM IR (-O2):
define i8* @extract_substring_before_str(i8* %0, i8* nocapture %1) {
%3 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) %1)
%4 = ptrtoint i8* %3 to i64
%5 = ptrtoint i8* %0 to i64
%6 = sub i64 %4, %5
%7 = trunc i64 %6 to i32
%8 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%9 = add nsw i64 %8, 1
%10 = tail call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
%12 = add nsw i32 %7, 1
%13 = tail call i32 @memcpy(i8* %11, i8* %0, i32 %12)
%14 = shl i64 %6, 32
%15 = ashr exact i64 %14, 32
%16 = getelementptr inbounds i8, i8* %11, i64 %15
store i8 0, i8* %16
ret i8* %11
}