#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 */
scalar_t__ calloc(int, int);
char *del_ext(char *, int /*<<< orphan*/);
char *get_ext(char *);
scalar_t__ is_valid_separator(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *generate_new_filename(char *main_str, char *new_str, char *separator) {
  int len_main_str = strlen(main_str);
  int len_new_str = strlen(new_str);
  int len_ext_str = strlen(get_ext(main_str));
  char *ext = (char *)calloc(len_ext_str, sizeof(char));
  strcpy(ext, get_ext(main_str));
  char *generated =
      (char *)calloc((len_new_str + len_main_str + 2), sizeof(char));
  strcat(generated, del_ext(main_str, 0));
  strcat(generated, is_valid_separator(separator) ? separator : "_");
  strcat(generated, new_str);
  strcat(generated, ext);
  return generated;
}


x86 assembly (clang, -O0):
<generate_new_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <get_ext@plt>
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <get_ext@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%edi
add    -0x1c(%rbp),%edi
add    $0x2,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1040 <del_ext@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
call   1090 <strcat@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <is_valid_separator@plt>
cmp    $0x0,%rax
je     123c <generate_new_filename+0xcc>
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
jmp    124c <generate_new_filename+0xdc>
lea    0xdbd(%rip),%rax
mov    %rax,-0x58(%rbp)
jmp    124c <generate_new_filename+0xdc>
mov    -0x40(%rbp),%rdi
mov    -0x58(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x38(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_new_filename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r13
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r13,%rdi
call   1080 <get_ext@plt>
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%r12
mov    %r13,%rdi
call   1080 <get_ext@plt>
mov    %r12,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
lea    (%rbx,%rbp,1),%edi
add    $0x2,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
mov    %r13,%rdi
xor    %esi,%esi
call   1040 <del_ext@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1090 <strcat@plt>
mov    %r15,%rdi
call   1070 <is_valid_separator@plt>
test   %rax,%rax
lea    0xdff(%rip),%rsi
cmovne %r15,%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1090 <strcat@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"_\00"
define i8* @generate_new_filename(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %7
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
store i32 %15, i32* %8
%16 = load i8*, i8** %4
%17 = call i8* @get_ext(i8* %16)
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = call i64 @calloc(i32 %19, i32 1)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %10
%22 = load i8*, i8** %10
%23 = load i8*, i8** %4
%24 = call i8* @get_ext(i8* %23)
%25 = call i32 @strcpy(i8* %22, i8* %24)
%26 = load i32, i32* %8
%27 = load i32, i32* %7
%28 = add nsw i32 %26, %27
%29 = add nsw i32 %28, 2
%30 = call i64 @calloc(i32 %29, i32 1)
%31 = inttoptr i64 %30 to i8*
store i8* %31, i8** %11
%32 = load i8*, i8** %11
%33 = load i8*, i8** %4
%34 = call i8* @del_ext(i8* %33, i32 0)
%35 = call i32 @strcat(i8* %32, i8* %34)
%36 = load i8*, i8** %11
%37 = load i8*, i8** %6
%38 = call i64 @is_valid_separator(i8* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %3
%41 = load i8*, i8** %6
br label %43
42: ; preds = %3
br label %43
43: ; preds = %42, %40
%44 = phi i8* [ %41, %40 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %42 ]
%45 = call i32 @strcat(i8* %36, i8* %44)
%46 = load i8*, i8** %11
%47 = load i8*, i8** %5
%48 = call i32 @strcat(i8* %46, i8* %47)
%49 = load i8*, i8** %11
%50 = load i8*, i8** %10
%51 = call i32 @strcat(i8* %49, i8* %50)
%52 = load i8*, i8** %11
ret i8* %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"_\00"
define i8* @generate_new_filename(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i8* @get_ext(i8* %0)
%7 = tail call i32 @strlen(i8* %6)
%8 = tail call i64 @calloc(i32 %7, i32 1)
%9 = inttoptr i64 %8 to i8*
%10 = tail call i8* @get_ext(i8* %0)
%11 = tail call i32 @strcpy(i8* %9, i8* %10)
%12 = add i32 %4, 2
%13 = add i32 %12, %5
%14 = tail call i64 @calloc(i32 %13, i32 1)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i8* @del_ext(i8* %0, i32 0)
%17 = tail call i32 @strcat(i8* %15, i8* %16)
%18 = tail call i64 @is_valid_separator(i8* %2)
%19 = icmp eq i64 %18, 0
%20 = select i1 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %2
%21 = tail call i32 @strcat(i8* %15, i8* %20)
%22 = tail call i32 @strcat(i8* %15, i8* %1)
%23 = tail call i32 @strcat(i8* %15, i8* %9)
ret i8* %15
}