#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
scalar_t__ renameat2(int, char const *, int, char const *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strrchr(char *, char);
int main(int argc, char *argv[]) {
  int xc = 0;
  int rc = -1;
  int exchange = 0;
  const char *program = "renametool";
  const char *fromfile = (const char *)0;
  const char *tofile = (const char *)0;
  int ndx = 0;
  do {
    program = ((program = strrchr(argv[ndx], '/')) == (char *)0) ? argv[ndx]
                                                                 : program + 1;
    ndx += 1;
    if (argc < 3) {
      fprintf(stderr, "usage: %s [ -x ] FROMFILE TOFILE\n", program);
      fprintf(stderr, "       -x        Exhange FROMFILE and TOFILE.\n");
      xc = 2;
      break;
    }
    if (strcmp(argv[ndx], "-x") == 0) {
      exchange = !0;
      ndx += 1;
    }
    if (argc != (ndx + 2)) {
      fprintf(stderr, "usage: %s [ -x ] FROMFILE TOFILE\n", program);
      fprintf(stderr, "       -x        Exhange FROMFILE and TOFILE.\n");
      xc = 2;
      break;
    }
    fromfile = argv[ndx++];
    tofile = argv[ndx++];
    if (renameat2((-100), fromfile, (-100), tofile,
                  exchange ? (1 << 1) : (1 << 0)) < 0) {
      perror("renameat2");
      xc = 1;
      break;
    }
  } while (0);
  exit(xc);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    0xe6e(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11e6 <main+0x86>
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x48(%rbp)
jmp    11f2 <main+0x92>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    124b <main+0xeb>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x14(%rbp)
jmp    1350 <main+0x1f0>
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdfe(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    127d <main+0x11d>
movl   $0x1,-0x1c(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x3c(%rbp),%ecx
add    $0x2,%ecx
cmp    %ecx,%eax
je     12cc <main+0x16c>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd69(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd74(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x2,-0x14(%rbp)
jmp    1350 <main+0x1f0>
mov    -0x10(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x3c(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x3c(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rcx
mov    -0x1c(%rbp),%edx
mov    $0x1,%r8d
mov    $0x2,%eax
cmp    $0x0,%edx
cmovne %eax,%r8d
mov    $0xffffff9c,%edx
mov    %edx,%edi
call   1060 <renameat2@plt>
cmp    $0x0,%rax
jge    134b <main+0x1eb>
lea    0xd25(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x14(%rbp)
jmp    1350 <main+0x1f0>
jmp    1350 <main+0x1f0>
mov    -0x14(%rbp),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r15d
mov    (%rsi),%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
lea    0x1(%rax),%r14
test   %rax,%rax
cmove  %rbp,%r14
cmp    $0x2,%r15d
jle    11eb <main+0x8b>
mov    0x8(%rbx),%rdi
lea    0xeb8(%rip),%rsi
call   1040 <strcmp@plt>
xor    %r8d,%r8d
test   %rax,%rax
sete   %r8b
lea    0x3(%r8),%eax
cmp    %r15d,%eax
jne    11eb <main+0x8b>
add    $0x1,%r8d
mov    %r8d,%eax
mov    (%rbx,%rax,8),%rsi
mov    0x8(%rbx,%rax,8),%rcx
mov    $0xffffff9c,%edi
mov    $0xffffff9c,%edx
call   1060 <renameat2@plt>
test   %rax,%rax
jns    121f <main+0xbf>
lea    0xe78(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dee(%rip),%rbx
mov    (%rbx),%edi
lea    0xe05(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe14(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"renametool\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"usage: %s [ -x ] FROMFILE TOFILE\0A\00"
@.str.2 = [47 x i8] c" -x Exhange FROMFILE and TOFILE.\0A\00"
@.str.3 = [3 x i8] c"-x\00"
@.str.4 = [10 x i8] c"renameat2\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %8
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %9
store i8* null, i8** %10
store i8* null, i8** %11
store i32 0, i32* %12
br label %13
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = load i32, i32* %12
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8*, i8** %14, i64 %16
%18 = load i8*, i8** %17
%19 = call i8* @strrchr(i8* %18, i8 signext 47)
store i8* %19, i8** %9
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %27
21: ; preds = %13
%22 = load i8**, i8*** %5
%23 = load i32, i32* %12
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
%26 = load i8*, i8** %25
br label %30
27: ; preds = %13
%28 = load i8*, i8** %9
%29 = getelementptr inbounds i8, i8* %28, i64 1
br label %30
30: ; preds = %27, %21
%31 = phi i8* [ %26, %21 ], [ %29, %27 ]
store i8* %31, i8** %9
%32 = load i32, i32* %12
%33 = add nsw i32 %32, 1
store i32 %33, i32* %12
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 3
br i1 %35, label %36, label %42
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %9
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %6
br label %88
42: ; preds = %30
%43 = load i8**, i8*** %5
%44 = load i32, i32* %12
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8*, i8** %43, i64 %45
%47 = load i8*, i8** %46
%48 = call i64 @strcmp(i8* %47, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %42
store i32 1, i32* %8
%51 = load i32, i32* %12
%52 = add nsw i32 %51, 1
store i32 %52, i32* %12
br label %53
53: ; preds = %50, %42
%54 = load i32, i32* %4
%55 = load i32, i32* %12
%56 = add nsw i32 %55, 2
%57 = icmp ne i32 %54, %56
br i1 %57, label %58, label %64
58: ; preds = %53
%59 = load i32, i32* @stderr
%60 = load i8*, i8** %9
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %60)
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %6
br label %88
64: ; preds = %53
%65 = load i8**, i8*** %5
%66 = load i32, i32* %12
%67 = add nsw i32 %66, 1
store i32 %67, i32* %12
%68 = sext i32 %66 to i64
%69 = getelementptr inbounds i8*, i8** %65, i64 %68
%70 = load i8*, i8** %69
store i8* %70, i8** %10
%71 = load i8**, i8*** %5
%72 = load i32, i32* %12
%73 = add nsw i32 %72, 1
store i32 %73, i32* %12
%74 = sext i32 %72 to i64
%75 = getelementptr inbounds i8*, i8** %71, i64 %74
%76 = load i8*, i8** %75
store i8* %76, i8** %11
%77 = load i8*, i8** %10
%78 = load i8*, i8** %11
%79 = load i32, i32* %8
%80 = icmp ne i32 %79, 0
%81 = zext i1 %80 to i64
%82 = select i1 %80, i32 2, i32 1
%83 = call i64 @renameat2(i32 -100, i8* %77, i32 -100, i8* %78, i32 %82)
%84 = icmp slt i64 %83, 0
br i1 %84, label %85, label %87
85: ; preds = %64
%86 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %6
br label %88
87: ; preds = %64
br label %88
88: ; preds = %87, %85, %58, %36
%89 = load i32, i32* %6
%90 = call i32 @exit(i32 %89)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [34 x i8] c"usage: %s [ -x ] FROMFILE TOFILE\0A\00"
@.str.2 = [47 x i8] c" -x Exhange FROMFILE and TOFILE.\0A\00"
@.str.3 = [3 x i8] c"-x\00"
@.str.4 = [10 x i8] c"renameat2\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %3, i8 signext 47)
%5 = icmp eq i8* %4, null
%6 = getelementptr inbounds i8, i8* %4, i64 1
%7 = select i1 %5, i8* %3, i8* %6
%8 = icmp slt i32 %0, 3
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%12 = load i32, i32* @stderr
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %39
14: ; preds = %2
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = tail call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
%19 = select i1 %18, i32 2, i32 1
%20 = add nuw nsw i32 %19, 2
%21 = icmp eq i32 %20, %0
br i1 %21, label %27, label %22
22: ; preds = %14
%23 = load i32, i32* @stderr
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
br label %39
27: ; preds = %14
%28 = add nuw nsw i32 %19, 1
%29 = zext i32 %19 to i64
%30 = getelementptr inbounds i8*, i8** %1, i64 %29
%31 = load i8*, i8** %30
%32 = zext i32 %28 to i64
%33 = getelementptr inbounds i8*, i8** %1, i64 %32
%34 = load i8*, i8** %33
%35 = tail call i64 @renameat2(i32 -100, i8* %31, i32 -100, i8* %34, i32 %19)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %27, %37, %22, %9
%40 = phi i32 [ 2, %9 ], [ 2, %22 ], [ 1, %37 ], [ 0, %27 ]
%41 = tail call i32 @exit(i32 %40)
unreachable
}