#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 *malloc(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *stripCityTicket(char *station) {
  char *lastPlus = strrchr(station, '+');
  if (lastPlus == NULL)
    return station;
  int index = lastPlus - station;
  char *strippedStation = malloc(index + 1);
  strncpy(strippedStation, station, index);
  strippedStation[index] = '\0';
  return strippedStation;
}


x86 assembly (clang, -O0):
<stripCityTicket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2b,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1166 <stripCityTicket+0x36>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a7 <stripCityTicket+0x77>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stripCityTicket>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2b,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     1170 <stripCityTicket+0x40>
mov    %rax,%rbx
sub    %r14,%rbx
lea    0x1(%rbx),%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%r14
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @stripCityTicket(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 43)
%3 = icmp eq i8* %2, null
br i1 %3, label %15, label %4
4: ; preds = %1
%5 = ptrtoint i8* %2 to i64
%6 = ptrtoint i8* %0 to i64
%7 = sub i64 %5, %6
%8 = trunc i64 %7 to i32
%9 = add nsw i32 %8, 1
%10 = tail call i8* @malloc(i32 %9)
%11 = tail call i32 @strncpy(i8* %10, i8* %0, i32 %8)
%12 = shl i64 %7, 32
%13 = ashr exact i64 %12, 32
%14 = getelementptr inbounds i8, i8* %10, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %1, %4
%16 = phi i8* [ %10, %4 ], [ %0, %1 ]
ret i8* %16
}