#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 *get_filename_ext(char *);
scalar_t__ malloc(int);
char *strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *beertorrent_extension(char *s) {
  char *bt;
  char tmp;
  char *dot = get_filename_ext(s);
  size_t size = strlen(s);
  if (dot == NULL)
    dot = s + size;
  else
    dot--;
  bt = (char *)malloc(sizeof(char) * (size + 13));
  tmp = *dot;
  *dot = '\0';
  strcpy(bt, s);
  *dot = tmp;
  bt = strcat(bt, ".beertorrent");
  return bt;
}


x86 assembly (clang, -O0):
<beertorrent_extension>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <get_filename_ext@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1192 <beertorrent_extension+0x42>
mov    -0x8(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    119e <beertorrent_extension+0x4e>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0xd,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x11(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1070 <strcat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<beertorrent_extension>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
call   1060 <get_filename_ext@plt>
mov    %rax,%r14
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%rcx
lea    -0x1(%r14),%r12
test   %r14,%r14
cmove  %rcx,%r12
lea    0xd(%rax),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    (%r12),%bpl
movb   $0x0,(%r12)
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %bpl,(%r12)
mov    %rbx,%rdi
call   1040 <strlen@plt>
movabs $0x726f74726565622e,%rcx
74 6f 72
mov    %rcx,(%rbx,%rax,1)
movabs $0x746e6572726f74,%rcx
mov    %rcx,0x5(%rbx,%rax,1)
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c".beertorrent\00"
define i8* @beertorrent_extension(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i8* @get_filename_ext(i8* %7)
store i8* %8, i8** %5
%9 = load i8*, i8** %2
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %6
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = load i64, i64* %6
%16 = getelementptr inbounds i8, i8* %14, i64 %15
store i8* %16, i8** %5
br label %20
17: ; preds = %1
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i32 -1
store i8* %19, i8** %5
br label %20
20: ; preds = %17, %13
%21 = load i64, i64* %6
%22 = add i64 %21, 13
%23 = mul i64 1, %22
%24 = trunc i64 %23 to i32
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %3
%27 = load i8*, i8** %5
%28 = load i8, i8* %27
store i8 %28, i8* %4
%29 = load i8*, i8** %5
store i8 0, i8* %29
%30 = load i8*, i8** %3
%31 = load i8*, i8** %2
%32 = call i32 @strcpy(i8* %30, i8* %31)
%33 = load i8, i8* %4
%34 = load i8*, i8** %5
store i8 %33, i8* %34
%35 = load i8*, i8** %3
%36 = call i8* @strcat(i8* %35, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i8* %36, i8** %3
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
@.str = [13 x i8] c".beertorrent\00"
define i8* @beertorrent_extension(i8* %0) {
%2 = tail call i8* @get_filename_ext(i8* %0)
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = icmp eq i8* %2, null
%5 = getelementptr inbounds i8, i8* %0, i64 %3
%6 = getelementptr inbounds i8, i8* %2, i64 -1
%7 = select i1 %4, i8* %5, i8* %6
%8 = trunc i64 %3 to i32
%9 = add i32 %8, 13
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = load i8, i8* %7
store i8 0, i8* %7
%13 = tail call i32 @strcpy(i8* %11, i8* %0)
store i8 %12, i8* %7
%14 = tail call i64 @strlen(i8* dereferenceable(1) %11)
%15 = getelementptr i8, i8* %11, i64 %14
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(13) %15, i8* align 1 dereferenceable(13) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 13, i1 false)
ret i8* %11
}