#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(scalar_t__);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *strrchr(char *, char);
char *generate_backup_name(char *filename) {
  char *aux_str;
  char *dot_pos;
  if (filename == NULL) {
    printf("Error on generating backup file. Filename it's NULL!\n");
    return NULL;
  }
  aux_str = malloc(strlen(filename) + 1);
  strcpy(aux_str, filename);
  dot_pos = strrchr(aux_str, '.');
  if (dot_pos != NULL) {
    *dot_pos = '\0';
  } else {
    printf("Error on finding dot on filename\n");
  }
  strcat(aux_str, ".bkp");
  return aux_str;
}


x86 assembly (clang, -O0):
<generate_backup_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1190 <generate_backup_name+0x30>
lea    0xe82(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1203 <generate_backup_name+0xa3>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11df <generate_backup_name+0x7f>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    11eb <generate_backup_name+0x8b>
lea    0xe50(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_backup_name>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     119f <generate_backup_name+0x3f>
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11b0 <generate_backup_name+0x50>
movb   $0x0,(%rax)
jmp    11bc <generate_backup_name+0x5c>
lea    0xe80(%rip),%rdi
call   1040 <puts@plt>
xor    %r14d,%r14d
jmp    11cb <generate_backup_name+0x6b>
lea    0xe4e(%rip),%rdi
call   1040 <puts@plt>
lea    0xe3d(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcat@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [54 x i8] c"Error on generating backup file. Filename it's NULL!\0A\00"
@.str.1 = [34 x i8] c"Error on finding dot on filename\0A\00"
@.str.2 = [5 x i8] c".bkp\00"
define i8* @generate_backup_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %30
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i64 @strlen(i8* %11)
%13 = add nsw i64 %12, 1
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = load i8*, i8** %3
%17 = call i32 @strcpy(i8* %15, i8* %16)
%18 = load i8*, i8** %4
%19 = call i8* @strrchr(i8* %18, i8 signext 46)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %10
%23 = load i8*, i8** %5
store i8 0, i8* %23
br label %26
24: ; preds = %10
%25 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %22
%27 = load i8*, i8** %4
%28 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%29 = load i8*, i8** %4
store i8* %29, i8** %2
br label %30
30: ; preds = %26, %8
%31 = load i8*, i8** %2
ret i8* %31
}

LLVM IR (-O2):
@.str.2 = [5 x i8] c".bkp\00"
@str = [33 x i8] c"Error on finding dot on filename\00"
@str.3 = [53 x i8] c"Error on generating backup file. Filename it's NULL!\00"
define i8* @generate_backup_name(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str.3, i64 0, i64 0))
br label %17
5: ; preds = %1
%6 = tail call i64 @strlen(i8* %0)
%7 = add nsw i64 %6, 1
%8 = tail call i8* @malloc(i64 %7)
%9 = tail call i32 @strcpy(i8* %8, i8* %0)
%10 = tail call i8* @strrchr(i8* dereferenceable(1) %8, i8 signext 46)
%11 = icmp eq i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %5
store i8 0, i8* %10
br label %15
13: ; preds = %5
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %15
15: ; preds = %13, %12
%16 = tail call i32 @strcat(i8* %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %3
%18 = phi i8* [ null, %3 ], [ %8, %15 ]
ret i8* %18
}