#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__ GetNextName(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char *, char);
int BuildName(char *name, char *namewop, const char *ext, const char *file,
              char *Error) {
  char *c;
  strncpy(name, file, 256);
  c = strrchr(name, '.');
  if (c) {
    strcpy(c, ext);
  } else {
    strcat(name, ext);
  }
  if (GetNextName(name, namewop)) {
    sprintf(Error, "Couldn't find a free filename based on '%s'.\n", name);
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<BuildName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11bd <BuildName+0x5d>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11ca <BuildName+0x6a>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1070 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <GetNextName@plt>
cmp    $0x0,%rax
je     1201 <BuildName+0xa1>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe10(%rip),%rsi
call   1080 <sprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1208 <BuildName+0xa8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BuildName>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rcx,%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11a0 <BuildName+0x40>
mov    %rax,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
jmp    11ab <BuildName+0x4b>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <GetNextName@plt>
test   %rax,%rax
je     11d4 <BuildName+0x74>
lea    0xe3e(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1080 <sprintf@plt>
mov    $0xffffffff,%eax
jmp    11d6 <BuildName+0x76>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [46 x i8] c"Couldn't find a free filename based on '%s'.\0A\00"
define i32 @BuildName(i8* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%13 = load i8*, i8** %7
%14 = load i8*, i8** %10
%15 = call i32 @strncpy(i8* %13, i8* %14, i32 256)
%16 = load i8*, i8** %7
%17 = call i8* @strrchr(i8* %16, i8 signext 46)
store i8* %17, i8** %12
%18 = load i8*, i8** %12
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %24
20: ; preds = %5
%21 = load i8*, i8** %12
%22 = load i8*, i8** %9
%23 = call i32 @strcpy(i8* %21, i8* %22)
br label %28
24: ; preds = %5
%25 = load i8*, i8** %7
%26 = load i8*, i8** %9
%27 = call i32 @strcat(i8* %25, i8* %26)
br label %28
28: ; preds = %24, %20
%29 = load i8*, i8** %7
%30 = load i8*, i8** %8
%31 = call i64 @GetNextName(i8* %29, i8* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i8*, i8** %11
%35 = load i8*, i8** %7
%36 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %35)
store i32 -1, i32* %6
br label %38
37: ; preds = %28
store i32 0, i32* %6
br label %38
38: ; preds = %37, %33
%39 = load i32, i32* %6
ret i32 %39
}

LLVM IR (-O2):
@.str = [46 x i8] c"Couldn't find a free filename based on '%s'.\0A\00"
define i32 @BuildName(i8* %0, i8* %1, i8* %2, i8* %3, i8* nocapture writeonly %4) {
%6 = tail call i32 @strncpy(i8* %0, i8* %3, i32 256)
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %5
%10 = tail call i32 @strcpy(i8* %7, i8* %2)
br label %13
11: ; preds = %5
%12 = tail call i32 @strcat(i8* %0, i8* %2)
br label %13
13: ; preds = %11, %9
%14 = tail call i64 @GetNextName(i8* %0, i8* %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @sprintf(i8* dereferenceable(1) %4, i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %13, %16
%19 = phi i32 [ -1, %16 ], [ 0, %13 ]
ret i32 %19
}