#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*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ rename(char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ unlink(char *);
void testexist(char *filename) {
  int fd;
  char *p, newfile[20], *index();
  if ((fd = open(filename, 0)) != -1) {
    close(fd);
    strcpy(newfile, filename);
    if (p = index(newfile, '.'))
      *p = '\0';
    strcat(newfile, ".bak");
    unlink(newfile);
    rename(filename, newfile);
  }
}


x86 assembly (clang, -O0):
<testexist>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
je     11f1 <testexist+0x81>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
lea    -0x30(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <index@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     11cb <testexist+0x5b>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x30(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1090 <strcat@plt>
lea    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <rename@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testexist>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
xor    %esi,%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11d5 <testexist+0x65>
mov    %eax,%edi
call   1060 <close@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
mov    $0x2e,%esi
call   1050 <index@plt>
test   %rax,%rax
je     11b0 <testexist+0x40>
movb   $0x0,(%rax)
lea    0xe49(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1090 <strcat@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <rename@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [5 x i8] c".bak\00"
define void @testexist(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [20 x i8]
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i32 @open(i8* %6, i32 0)
store i32 %7, i32* %3
%8 = icmp ne i32 %7, -1
br i1 %8, label %9, label %28
9: ; preds = %1
%10 = load i32, i32* %3
%11 = call i32 @close(i32 %10)
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%13 = load i8*, i8** %2
%14 = call i32 @strcpy(i8* %12, i8* %13)
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%16 = call i8* @index(i8* %15, i32 46)
store i8* %16, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %9
%19 = load i8*, i8** %4
store i8 0, i8* %19
br label %20
20: ; preds = %18, %9
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%22 = call i32 @strcat(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%23 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%24 = call i32 @unlink(i8* %23)
%25 = load i8*, i8** %2
%26 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%27 = call i32 @rename(i8* %25, i8* %26)
br label %28
28: ; preds = %20, %1
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c".bak\00"
define void @testexist(i8* %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @open(i8* %0, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %16, label %6
6: ; preds = %1
%7 = tail call i32 @close(i32 %4)
%8 = call i32 @strcpy(i8* %3, i8* %0)
%9 = call i8* @index(i8* %3, i32 46)
%10 = icmp eq i8* %9, null
br i1 %10, label %12, label %11
11: ; preds = %6
store i8 0, i8* %9
br label %12
12: ; preds = %11, %6
%13 = call i32 @strcat(i8* %3, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @unlink(i8* %3)
%15 = call i32 @rename(i8* %0, i8* %3)
br label %16
16: ; preds = %12, %1
ret void
}