#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*/ strcat(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *get_bin_name(char *input) {
  char *c = strrchr(input, '.');
  char *output;
  int index;
  if (!c) {
    index = strlen(input);
    output = malloc(strlen(input) + 2);
  } else {
    index = (int)(c - input);
    output = malloc(strlen(input) + 1);
  }
  strncpy(output, input, index + 1);
  if (!c)
    output[index] = '.';
  strcat(output, "bin");
  return output;
}


x86 assembly (clang, -O0):
<get_bin_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a1 <get_bin_name+0x51>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
jmp    11c6 <get_bin_name+0x76>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1030 <strncpy@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11f0 <get_bin_name+0xa0>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x2e,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1070 <strcat@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_bin_name>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1192 <get_bin_name+0x42>
mov    %rax,%rbx
sub    %r14d,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
add    $0x1,%ebx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
jmp    11c5 <get_bin_name+0x75>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x2(%rax),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    0x1(%rbx),%edx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strncpy@plt>
movslq %ebx,%rax
movb   $0x2e,(%r15,%rax,1)
lea    0xe34(%rip),%rsi
mov    %r15,%rdi
call   1070 <strcat@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"bin\00"
define i8* @get_bin_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8* @strrchr(i8* %6, i8 signext 46)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %17, label %10
10: ; preds = %1
%11 = load i8*, i8** %2
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %5
%13 = load i8*, i8** %2
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 2
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %4
br label %28
17: ; preds = %1
%18 = load i8*, i8** %3
%19 = load i8*, i8** %2
%20 = ptrtoint i8* %18 to i64
%21 = ptrtoint i8* %19 to i64
%22 = sub i64 %20, %21
%23 = trunc i64 %22 to i32
store i32 %23, i32* %5
%24 = load i8*, i8** %2
%25 = call i32 @strlen(i8* %24)
%26 = add nsw i32 %25, 1
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %4
br label %28
28: ; preds = %17, %10
%29 = load i8*, i8** %4
%30 = load i8*, i8** %2
%31 = load i32, i32* %5
%32 = add nsw i32 %31, 1
%33 = call i32 @strncpy(i8* %29, i8* %30, i32 %32)
%34 = load i8*, i8** %3
%35 = icmp ne i8* %34, null
br i1 %35, label %41, label %36
36: ; preds = %28
%37 = load i8*, i8** %4
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 46, i8* %40
br label %41
41: ; preds = %36, %28
%42 = load i8*, i8** %4
%43 = call i32 @strcat(i8* %42, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%44 = load i8*, i8** %4
ret i8* %44
}

LLVM IR (-O2):
@.str = [4 x i8] c"bin\00"
define i8* @get_bin_name(i8* %0) {
%2 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%3 = icmp eq i8* %2, null
br i1 %3, label %14, 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 = tail call i32 @strlen(i8* %0)
%10 = add nsw i32 %9, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = add nsw i32 %8, 1
%13 = tail call i32 @strncpy(i8* %11, i8* %0, i32 %12)
br label %23
14: ; preds = %1
%15 = tail call i32 @strlen(i8* %0)
%16 = tail call i32 @strlen(i8* %0)
%17 = add nsw i32 %16, 2
%18 = tail call i8* @malloc(i32 %17)
%19 = add nsw i32 %15, 1
%20 = tail call i32 @strncpy(i8* %18, i8* %0, i32 %19)
%21 = sext i32 %15 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
store i8 46, i8* %22
br label %23
23: ; preds = %4, %14
%24 = phi i8* [ %18, %14 ], [ %11, %4 ]
%25 = tail call i32 @strcat(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
ret i8* %24
}