#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 atoi(char *);
int errno;
int /*<<< orphan*/ printf(char *, ...);
int rename(char *, char *);
int main(int argc, char **argv) {
  char *file1;
  char *file2;
  int count = 0;
  int ret = 0;
  if (argc == 1) {
    file1 = "/mnt/benchmarks/file1";
    file2 = "/mnt/benchmarks/file2";
    count = 500000;
  } else if (argc < 4) {
    printf("use: rename-test file1 file2 count\n");
    return 0;
  } else {
    file1 = argv[1];
    file2 = argv[2];
    count = atoi(argv[3]);
  }
  printf("file1: %s, file2: %s, count: %d\n", file1, file2, count);
  while (count >= 0) {
    if (count % 2) {
      ret = rename(file1, file2);
      if (ret) {
        printf("rename error: %d\n", errno);
        return ret;
      }
    } else {
      ret = rename(file2, file1);
      if (ret) {
        printf("rename error: %d\n", errno);
        return ret;
      }
    }
    count--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    1180 <main+0x50>
lea    0xe9b(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xea6(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x7a120,-0x24(%rbp)
jmp    11d1 <main+0xa1>
cmpl   $0x4,-0x8(%rbp)
jge    11a4 <main+0x74>
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    129f <main+0x16f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x24(%rbp)
jmp    11d1 <main+0xa1>
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
jl     1298 <main+0x168>
mov    -0x24(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1249 <main+0x119>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <rename@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1244 <main+0x114>
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <main+0x16f>
jmp    128a <main+0x15a>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <rename@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     1285 <main+0x155>
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129f <main+0x16f>
jmp    128a <main+0x15a>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    11ea <main+0xba>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x1,%edi
jne    117a <main+0x3a>
lea    0xed9(%rip),%rdi
lea    0xea6(%rip),%r14
lea    0xeb5(%rip),%r15
mov    $0x7a120,%ebx
mov    %r14,%rsi
mov    %r15,%rdx
mov    $0x7a120,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c0 <main+0x80>
cmp    $0x3,%edi
jg     118d <main+0x4d>
lea    0xed9(%rip),%rdi
call   1030 <puts@plt>
jmp    11e8 <main+0xa8>
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%r15
mov    0x18(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
lea    0xe85(%rip),%rdi
xor    %ebp,%ebp
mov    %r14,%rsi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
js     1205 <main+0xc5>
nopl   0x0(%rax)
test   $0x1,%bl
jne    11d0 <main+0x90>
mov    %r15,%rdi
mov    %r14,%rsi
jmp    11d6 <main+0x96>
nopl   (%rax)
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <rename@plt>
test   %eax,%eax
jne    11ec <main+0xac>
lea    -0x1(%rbx),%eax
test   %ebx,%ebx
mov    %eax,%ebx
jg     11c0 <main+0x80>
xor    %ebp,%ebp
jmp    1205 <main+0xc5>
mov    %eax,%ebp
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"/mnt/benchmarks/file1\00"
@.str.1 = [22 x i8] c"/mnt/benchmarks/file2\00"
@.str.2 = [36 x i8] c"use: rename-test file1 file2 count\0A\00"
@.str.3 = [33 x i8] c"file1: %s, file2: %s, count: %d\0A\00"
@.str.4 = [18 x i8] c"rename error: %d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %9
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %13
12: ; preds = %2
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8** %6
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8** %7
store i32 500000, i32* %8
br label %30
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 4
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %68
18: ; preds = %13
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
store i8* %24, i8** %7
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 3
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %8
br label %29
29: ; preds = %18
br label %30
30: ; preds = %29, %12
%31 = load i8*, i8** %6
%32 = load i8*, i8** %7
%33 = load i32, i32* %8
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %31, i8* %32, i32 %33)
br label %35
35: ; preds = %64, %30
%36 = load i32, i32* %8
%37 = icmp sge i32 %36, 0
br i1 %37, label %38, label %67
38: ; preds = %35
%39 = load i32, i32* %8
%40 = srem i32 %39, 2
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %53
42: ; preds = %38
%43 = load i8*, i8** %6
%44 = load i8*, i8** %7
%45 = call i32 @rename(i8* %43, i8* %44)
store i32 %45, i32* %9
%46 = load i32, i32* %9
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = load i32, i32* @errno
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %9
store i32 %51, i32* %3
br label %68
52: ; preds = %42
br label %64
53: ; preds = %38
%54 = load i8*, i8** %7
%55 = load i8*, i8** %6
%56 = call i32 @rename(i8* %54, i8* %55)
store i32 %56, i32* %9
%57 = load i32, i32* %9
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %53
%60 = load i32, i32* @errno
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %9
store i32 %62, i32* %3
br label %68
63: ; preds = %53
br label %64
64: ; preds = %63, %52
%65 = load i32, i32* %8
%66 = add nsw i32 %65, -1
store i32 %66, i32* %8
br label %35
67: ; preds = %35
store i32 0, i32* %3
br label %68
68: ; preds = %67, %59, %48, %16
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str = [22 x i8] c"/mnt/benchmarks/file1\00"
@.str.1 = [22 x i8] c"/mnt/benchmarks/file2\00"
@.str.3 = [33 x i8] c"file1: %s, file2: %s, count: %d\0A\00"
@.str.4 = [18 x i8] c"rename error: %d\0A\00"
@errno = global i32 0
@str = [35 x i8] c"use: rename-test file1 file2 count\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 500000)
br label %20
6: ; preds = %2
%7 = icmp slt i32 %0, 4
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %43
10: ; preds = %6
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = getelementptr inbounds i8*, i8** %1, i64 3
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %12, i8* %14, i32 %17)
%19 = icmp sgt i32 %17, -1
br i1 %19, label %20, label %43
20: ; preds = %4, %10
%21 = phi i32 [ 500000, %4 ], [ %17, %10 ]
%22 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %4 ], [ %14, %10 ]
%23 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), %4 ], [ %12, %10 ]
br label %24
24: ; preds = %20, %40
%25 = phi i32 [ %41, %40 ], [ %21, %20 ]
%26 = and i32 %25, 1
%27 = icmp eq i32 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %24
%29 = tail call i32 @rename(i8* %23, i8* %22)
%30 = icmp eq i32 %29, 0
br i1 %30, label %40, label %31
31: ; preds = %28
%32 = load i32, i32* @errno
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %32)
br label %43
34: ; preds = %24
%35 = tail call i32 @rename(i8* %22, i8* %23)
%36 = icmp eq i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = load i32, i32* @errno
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %38)
br label %43
40: ; preds = %34, %28
%41 = add nsw i32 %25, -1
%42 = icmp sgt i32 %25, 0
br i1 %42, label %24, label %43
43: ; preds = %40, %10, %37, %31, %8
%44 = phi i32 [ %29, %31 ], [ %35, %37 ], [ 0, %8 ], [ 0, %10 ], [ 0, %40 ]
ret i32 %44
}