#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 */
int FILENAME_MAX;
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
void generate_filename(const char *in_name, char *out_name, char *extension) {
  char tmp_name[FILENAME_MAX];
  int len;
  int i;
  strcpy(tmp_name, in_name);
  len = strlen(tmp_name);
  for (i = len - 1; i > 0; i--) {
    if (tmp_name[i] == '.') {
      break;
    }
  }
  if (i <= 0) {
    i = len;
  }
  tmp_name[i] = '\0';
  sprintf(out_name, "%s.%s", tmp_name, extension);
}


x86 assembly (clang, -O0):
<generate_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jle    11c5 <generate_filename+0x95>
mov    -0x38(%rbp),%rax
movslq -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    11b2 <generate_filename+0x82>
jmp    11c5 <generate_filename+0x95>
jmp    11b7 <generate_filename+0x87>
mov    -0x30(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x30(%rbp)
jmp    118e <generate_filename+0x5e>
cmpl   $0x0,-0x30(%rbp)
jg     11d5 <generate_filename+0xa5>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x38(%rbp),%rdx
movslq -0x30(%rbp),%rax
movb   $0x0,(%rdx,%rax,1)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
lea    0xe10(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_filename>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rsi
mov    0x2e9e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
test   %eax,%eax
mov    $0x1,%edx
cmovle %eax,%edx
lea    -0x1(%rdx),%eax
mov    %rcx,%rsi
nop
cmp    $0x2,%esi
jl     1196 <generate_filename+0x66>
add    $0xffffffffffffffff,%rsi
mov    %esi,%edi
cmpb   $0x2e,(%rbx,%rdi,1)
jne    1180 <generate_filename+0x50>
lea    0x1(%rsi),%edx
mov    %esi,%eax
cmp    $0x2,%edx
cmovge %eax,%ecx
movslq %ecx,%rax
movb   $0x0,(%rbx,%rax,1)
lea    0xe56(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1050 <sprintf@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@.str = [6 x i8] c"%s.%s\00"
define void @generate_filename(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%11 = load i32, i32* @FILENAME_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %7
%14 = alloca i8, i64 %12
store i64 %12, i64* %8
%15 = load i8*, i8** %4
%16 = call i32 @strcpy(i8* %14, i8* %15)
%17 = call i32 @strlen(i8* %14)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = sub nsw i32 %18, 1
store i32 %19, i32* %10
br label %20
20: ; preds = %32, %3
%21 = load i32, i32* %10
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %20
%24 = load i32, i32* %10
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %14, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 46
br i1 %29, label %30, label %31
30: ; preds = %23
br label %35
31: ; preds = %23
br label %32
32: ; preds = %31
%33 = load i32, i32* %10
%34 = add nsw i32 %33, -1
store i32 %34, i32* %10
br label %20
35: ; preds = %30, %20
%36 = load i32, i32* %10
%37 = icmp sle i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = load i32, i32* %9
store i32 %39, i32* %10
br label %40
40: ; preds = %38, %35
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %14, i64 %42
store i8 0, i8* %43
%44 = load i8*, i8** %5
%45 = load i8*, i8** %6
%46 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %45)
%47 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
@.str = [6 x i8] c"%s.%s\00"
define void @generate_filename(i8* %0, i8* nocapture writeonly %1, i8* %2) {
%4 = load i32, i32* @FILENAME_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @strcpy(i8* %6, i8* %0)
%8 = call i32 @strlen(i8* %6)
%9 = zext i32 %8 to i64
%10 = call i32 @llvm.smin.i32(i32 %8, i32 1)
%11 = add i32 %10, -1
br label %12
12: ; preds = %17, %3
%13 = phi i64 [ %14, %17 ], [ %9, %3 ]
%14 = add nsw i64 %13, -1
%15 = trunc i64 %13 to i32
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %25
17: ; preds = %12
%18 = and i64 %14, 4294967295
%19 = getelementptr inbounds i8, i8* %6, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 46
br i1 %21, label %22, label %12
22: ; preds = %17
%23 = trunc i64 %13 to i32
%24 = trunc i64 %14 to i32
br label %25
25: ; preds = %12, %22
%26 = phi i32 [ %23, %22 ], [ %10, %12 ]
%27 = phi i32 [ %24, %22 ], [ %11, %12 ]
%28 = icmp slt i32 %26, 2
%29 = select i1 %28, i32 %8, i32 %27
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %6, i64 %30
store i8 0, i8* %31
%32 = call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %2)
ret void
}