#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 */
typedef int /*<<< orphan*/ indexname;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ strncat(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
void getBackupFileName(const char *basename, unsigned char index,
                       char *backupname, size_t size) {
  char indexname[5];
  assert(size >= strlen(basename) + sizeof(indexname));
  strncpy(backupname, basename, size);
  if (index > 0) {
    sprintf(indexname, ".%d", index);
    strncat(backupname, indexname, strlen(indexname));
  }
}


x86 assembly (clang, -O0):
<getBackupFileName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    $0x5,%rcx
cmp    %rcx,%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
movzbl -0x9(%rbp),%eax
cmp    $0x0,%eax
jle    11f2 <getBackupFileName+0xa2>
lea    -0x25(%rbp),%rdi
lea    0xe3f(%rip),%rsi
movzbl -0x9(%rbp),%edx
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x25(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x25(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1050 <strncat@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getBackupFileName>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbp
call   1040 <strlen@plt>
cltq
add    $0x5,%rax
xor    %edi,%edi
cmp    %rbx,%rax
setbe  %dil
call   1060 <assert@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
test   %r15d,%r15d
je     11bc <getBackupFileName+0x6c>
movzbl %r15b,%edx
lea    0xe66(%rip),%rsi
lea    0x3(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <strncat@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c".%d\00"
define void @getBackupFileName(i8* %0, i8 zeroext %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i64
%9 = alloca [5 x i8]
store i8* %0, i8** %5
store i8 %1, i8* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%10 = load i64, i64* %8
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
%13 = sext i32 %12 to i64
%14 = add i64 %13, 5
%15 = icmp uge i64 %10, %14
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i8*, i8** %7
%19 = load i8*, i8** %5
%20 = load i64, i64* %8
%21 = call i32 @strncpy(i8* %18, i8* %19, i64 %20)
%22 = load i8, i8* %6
%23 = zext i8 %22 to i32
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %34
25: ; preds = %4
%26 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%27 = load i8, i8* %6
%28 = call i32 @sprintf(i8* %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 zeroext %27)
%29 = load i8*, i8** %7
%30 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%31 = getelementptr inbounds [5 x i8], [5 x i8]* %9, i64 0, i64 0
%32 = call i32 @strlen(i8* %31)
%33 = call i32 @strncat(i8* %29, i8* %30, i32 %32)
br label %34
34: ; preds = %25, %4
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c".%d\00"
define void @getBackupFileName(i8* %0, i8 zeroext %1, i8* %2, i64 %3) {
%5 = alloca [5 x i8]
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%7 = tail call i32 @strlen(i8* %0)
%8 = sext i32 %7 to i64
%9 = add nsw i64 %8, 5
%10 = icmp ule i64 %9, %3
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = tail call i32 @strncpy(i8* %2, i8* %0, i64 %3)
%14 = icmp eq i8 %1, 0
br i1 %14, label %19, label %15
15: ; preds = %4
%16 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8 zeroext %1)
%17 = call i32 @strlen(i8* %6)
%18 = call i32 @strncat(i8* %2, i8* %6, i32 %17)
br label %19
19: ; preds = %15, %4
ret void
}