#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 */
int /*<<< orphan*/ memcpy(char *, char const *, unsigned long);
char *strrchr(char const *, int);
char *substringTo(char *dest, const char *source, int divider) {
  unsigned long index;
  char *to;
  to = strrchr(source, divider);
  if (to == NULL) {
    dest[0] = 0;
    return NULL;
  }
  index = (to + 1) - source;
  memcpy(dest, source, index);
  dest[index] = 0;
  return dest;
}


x86 assembly (clang, -O0):
<substringTo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1030 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1162 <substringTo+0x42>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    119a <substringTo+0x7a>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substringTo>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %edx,%esi
call   1030 <strrchr@plt>
test   %rax,%rax
je     1159 <substringTo+0x39>
mov    %rax,%rbx
sub    %r15,%rbx
lea    0x1(%rbx),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
lea    (%r14,%rbx,1),%rax
add    $0x1,%rax
jmp    115f <substringTo+0x3f>
mov    %r14,%rax
xor    %r14d,%r14d
movb   $0x0,(%rax)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @substringTo(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i8* @strrchr(i8* %10, i32 %11)
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 0, i8* %17
store i8* null, i8** %4
br label %33
18: ; preds = %3
%19 = load i8*, i8** %9
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8*, i8** %6
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
store i64 %24, i64* %8
%25 = load i8*, i8** %5
%26 = load i8*, i8** %6
%27 = load i64, i64* %8
%28 = call i32 @memcpy(i8* %25, i8* %26, i64 %27)
%29 = load i8*, i8** %5
%30 = load i64, i64* %8
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %5
store i8* %32, i8** %4
br label %33
33: ; preds = %18, %15
%34 = load i8*, i8** %4
ret i8* %34
}

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