#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 /*<<< orphan*/ cstrcat(char *, char *, int);
int /*<<< orphan*/ cstrcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *, char *, char *);
int rename(char *, char *);
void rename_file(char *filename) {
  char oldname[1024];
  cstrcpy(oldname, filename, 1024);
  cstrcat(oldname, ".new", 1024);
  if (rename(oldname, filename) == -1)
    printf("failed to rename %s to %s\n", oldname, filename);
}


x86 assembly (clang, -O0):
<rename_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x400,%edx
call   1050 <cstrcpy@plt>
lea    -0x410(%rbp),%rdi
lea    0xe8e(%rip),%rsi
mov    $0x400,%edx
call   1060 <cstrcat@plt>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <rename@plt>
cmp    $0xffffffff,%eax
jne    11ac <rename_file+0x6c>
lea    -0x410(%rbp),%rsi
mov    -0x8(%rbp),%rdx
lea    0xe5e(%rip),%rdi
call   1030 <printf@plt>
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rename_file>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <cstrcpy@plt>
lea    0xe99(%rip),%rsi
mov    %r14,%rdi
mov    $0x400,%edx
call   1060 <cstrcat@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <rename@plt>
cmp    $0xffffffff,%eax
jne    1196 <rename_file+0x56>
lea    0xe7a(%rip),%rdi
mov    %rsp,%rsi
mov    %rbx,%rdx
call   1030 <printf@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [5 x i8] c".new\00"
@.str.1 = [27 x i8] c"failed to rename %s to %s\0A\00"
define void @rename_file(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
store i8* %0, i8** %2
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = load i8*, i8** %2
%6 = call i32 @cstrcpy(i8* %4, i8* %5, i32 1024)
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = call i32 @cstrcat(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1024)
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%10 = load i8*, i8** %2
%11 = call i32 @rename(i8* %9, i8* %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%15 = load i8*, i8** %2
%16 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %14, i8* %15)
br label %17
17: ; preds = %13, %1
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c".new\00"
@.str.1 = [27 x i8] c"failed to rename %s to %s\0A\00"
define void @rename_file(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @cstrcpy(i8* %3, i8* %0, i32 1024)
%5 = call i32 @cstrcat(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1024)
%6 = call i32 @rename(i8* %3, i8* %0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %3, i8* %0)
br label %10
10: ; preds = %8, %1
ret void
}