#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__ cst_strdup(unsigned char *);
size_t strlen(char const *);
scalar_t__ strstr(char const *, char const *);
char *cst_string_before(const char *s, const char *c) {
  char *p;
  char *q;
  p = (char *)((unsigned char *)strstr((const char *)s, (const char *)c));
  if (p == NULL)
    return NULL;
  q = (char *)cst_strdup((unsigned char *)s);
  q[strlen(s) - strlen(p)] = '\0';
  return q;
}


x86 assembly (clang, -O0):
<cst_string_before>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1169 <cst_string_before+0x39>
movq   $0x0,-0x8(%rbp)
jmp    11ae <cst_string_before+0x7e>
mov    -0x10(%rbp),%rdi
call   1040 <cst_strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rcx
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cst_string_before>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
call   1050 <strstr@plt>
test   %rax,%rax
je     116d <cst_string_before+0x3d>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <cst_strdup@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
sub    %rax,%rbx
movb   $0x0,(%r14,%rbx,1)
jmp    1170 <cst_string_before+0x40>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @cst_string_before(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = load i8*, i8** %5
%10 = call i64 @strstr(i8* %8, i8* %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %6
%12 = load i8*, i8** %6
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %2
store i8* null, i8** %3
br label %27
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i64 @cst_strdup(i8* %16)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = load i8*, i8** %4
%21 = call i64 @strlen(i8* %20)
%22 = load i8*, i8** %6
%23 = call i64 @strlen(i8* %22)
%24 = sub i64 %21, %23
%25 = getelementptr inbounds i8, i8* %19, i64 %24
store i8 0, i8* %25
%26 = load i8*, i8** %7
store i8* %26, i8** %3
br label %27
27: ; preds = %15, %14
%28 = load i8*, i8** %3
ret i8* %28
}

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