#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__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strrchr(char const *, char);
const char *figure_outfilename(const char *f, const char *newext) {
  char *newname;
  char *ext;
  ext = strrchr(f, '.');
  if (ext == NULL) {
    newname = (char *)malloc(strlen(f) + strlen(newext) + 2);
    if (newname) {
      sprintf(newname, "%s.%s", f, newext);
    }
  } else {
    int len;
    len = (ext - f) + strlen(newext) + 2;
    newname = (char *)malloc(len);
    if (newname) {
      strcpy(newname, f);
      ext = strrchr(newname, '.') + 1;
      strcpy(ext, newext);
    }
  }
  return newname;
}


x86 assembly (clang, -O0):
<figure_outfilename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11cb <figure_outfilename+0x7b>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x28(%rbp),%edi
add    %eax,%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c6 <figure_outfilename+0x76>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe3f(%rip),%rsi
call   1070 <sprintf@plt>
jmp    1242 <figure_outfilename+0xf2>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x2,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     123d <figure_outfilename+0xed>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1242 <figure_outfilename+0xf2>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<figure_outfilename>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11b1 <figure_outfilename+0x61>
mov    %rax,%rbx
sub    %r15d,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ef <figure_outfilename+0x9f>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
lea    0x1(%rax),%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    11ef <figure_outfilename+0x9f>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ed <figure_outfilename+0x9d>
mov    %rax,%rbx
lea    0xe23(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <sprintf@plt>
jmp    11ef <figure_outfilename+0x9f>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"%s.%s\00"
define i8* @figure_outfilename(i8* %0, i8* %1) {
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %15
5: ; preds = %2
%6 = tail call i32 @strlen(i8* %0)
%7 = tail call i32 @strlen(i8* %1)
%8 = add i32 %6, 2
%9 = add i32 %8, %7
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = icmp eq i64 %10, 0
br i1 %12, label %31, label %13
13: ; preds = %5
%14 = tail call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %31
15: ; preds = %2
%16 = ptrtoint i8* %3 to i64
%17 = ptrtoint i8* %0 to i64
%18 = sub i64 %16, %17
%19 = tail call i32 @strlen(i8* %1)
%20 = trunc i64 %18 to i32
%21 = add i32 %20, 2
%22 = add i32 %21, %19
%23 = tail call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
%25 = icmp eq i64 %23, 0
br i1 %25, label %31, label %26
26: ; preds = %15
%27 = tail call i32 @strcpy(i8* %24, i8* %0)
%28 = tail call i8* @strrchr(i8* %24, i8 signext 46)
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = tail call i32 @strcpy(i8* %29, i8* %1)
br label %31
31: ; preds = %15, %26, %5, %13
%32 = phi i8* [ %11, %13 ], [ null, %5 ], [ %24, %26 ], [ %24, %15 ]
ret i8* %32
}