#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*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strncpy(char *, char const *, int);
char *strrchr(char *, char);
char *vmdkFileTemplate(const char *outfile) {
  int outfileLen = strlen(outfile);
  char *template = (char *)malloc(outfileLen + 7);
  char *ptr;
  template = strncpy(template, outfile, outfileLen);
  ptr = strrchr(template, '.');
  if (ptr == NULL) {
    strcat(template, "-s%03d");
    return template;
  }
  const char *ext = outfile + (ptr - template);
  strcpy(ptr, "-s%03d");
  ptr += strlen("-s%03d");
  strcpy(ptr, ext);
  return template;
}


x86 assembly (clang, -O0):
<vmdkFileTemplate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x7,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d5 <vmdkFileTemplate+0x75>
mov    -0x20(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122c <vmdkFileTemplate+0xcc>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xdfe(%rip),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vmdkFileTemplate>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %eax,%ebx
lea    0x7(%rbx),%edi
call   1070 <malloc@plt>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11cf <vmdkFileTemplate+0x6f>
mov    %rax,%rbx
sub    %r14,%rax
add    %rax,%r15
lea    0xe54(%rip),%r12
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
movslq %eax,%rdi
add    %rbx,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
jmp    11de <vmdkFileTemplate+0x7e>
lea    0xe2a(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcat@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"-s%03d\00"
define i8* @vmdkFileTemplate(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, 7
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
%6 = tail call i8* @strncpy(i8* %5, i8* %0, i32 %2)
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %6, i8 signext 46)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @strcat(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %21
11: ; preds = %1
%12 = ptrtoint i8* %7 to i64
%13 = ptrtoint i8* %6 to i64
%14 = sub i64 %12, %13
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = tail call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %7, i64 %18
%20 = tail call i32 @strcpy(i8* %19, i8* %15)
br label %21
21: ; preds = %11, %9
ret i8* %6
}