#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);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int rename(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int main(int argc, char **argv) {
  if (argc != 3) {
    printf(
        "%s\n",
        "usage:\tswap FILE1 FILE2\n\texchange names of two files or folders");
    exit(1);
  }
  char *fn1 = argv[1];
  char *fn1_tmp = (char *)(malloc(sizeof(char) * 1000));
  sprintf(fn1_tmp, ".%s.swap.tmp", fn1);
  char *fn2 = argv[2];
  if (rename(fn1, fn1_tmp) == -1) {
    perror("Unable to rename");
    exit(2);
  }
  if (rename(fn2, fn1) == -1) {
    rename(fn1_tmp, fn1);
    perror("Unable to rename");
    exit(2);
  }
  if (rename(fn1_tmp, fn2) == -1) {
    rename(fn1, fn2);
    rename(fn1_tmp, fn1);
    perror("Unable to rename");
    exit(2);
  }
  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)
cmpl   $0x3,-0x8(%rbp)
je     119d <main+0x3d>
lea    0xe79(%rip),%rdi
lea    0xe76(%rip),%rsi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    $0x3e8,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe7e(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <rename@plt>
cmp    $0xffffffff,%eax
jne    1203 <main+0xa3>
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <rename@plt>
cmp    $0xffffffff,%eax
jne    123c <main+0xdc>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <rename@plt>
lea    0xe24(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <rename@plt>
cmp    $0xffffffff,%eax
jne    1282 <main+0x122>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <rename@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <rename@plt>
lea    0xdde(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x3,%edi
jne    11d5 <main+0x75>
mov    %rsi,%rbx
mov    0x8(%rsi),%r14
mov    $0x3e8,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
lea    0xebc(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1070 <sprintf@plt>
mov    0x10(%rbx),%r15
mov    %r14,%rdi
mov    %r12,%rsi
call   1060 <rename@plt>
cmp    $0xffffffff,%eax
je     1208 <main+0xa8>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <rename@plt>
cmp    $0xffffffff,%eax
je     11fd <main+0x9d>
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <rename@plt>
cmp    $0xffffffff,%eax
je     11f2 <main+0x92>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe24(%rip),%rdi
lea    0xe21(%rip),%rsi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <rename@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <rename@plt>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [64 x i8] c"usage:\09swap FILE1 FILE2\0A\09exchange names of two files or folders\00"
@.str.2 = [13 x i8] c".%s.swap.tmp\00"
@.str.3 = [17 x i8] c"Unable to rename\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = call i64 @malloc(i32 1000)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = load i8*, i8** %6
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %8
%26 = load i8*, i8** %6
%27 = load i8*, i8** %7
%28 = call i32 @rename(i8* %26, i8* %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %14
%31 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 2)
unreachable
33: ; preds = %14
%34 = load i8*, i8** %8
%35 = load i8*, i8** %6
%36 = call i32 @rename(i8* %34, i8* %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %44
38: ; preds = %33
%39 = load i8*, i8** %7
%40 = load i8*, i8** %6
%41 = call i32 @rename(i8* %39, i8* %40)
%42 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 2)
unreachable
44: ; preds = %33
%45 = load i8*, i8** %7
%46 = load i8*, i8** %8
%47 = call i32 @rename(i8* %45, i8* %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %58
49: ; preds = %44
%50 = load i8*, i8** %6
%51 = load i8*, i8** %8
%52 = call i32 @rename(i8* %50, i8* %51)
%53 = load i8*, i8** %7
%54 = load i8*, i8** %6
%55 = call i32 @rename(i8* %53, i8* %54)
%56 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @exit(i32 2)
unreachable
58: ; preds = %44
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [64 x i8] c"usage:\09swap FILE1 FILE2\0A\09exchange names of two files or folders\00"
@.str.2 = [13 x i8] c".%s.swap.tmp\00"
@.str.3 = [17 x i8] c"Unable to rename\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @malloc(i32 1000)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @sprintf(i8* dereferenceable(1) %11, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @rename(i8* %9, i8* %11)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @exit(i32 2)
unreachable
20: ; preds = %7
%21 = tail call i32 @rename(i8* %14, i8* %9)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = tail call i32 @rename(i8* %11, i8* %9)
%25 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%26 = tail call i32 @exit(i32 2)
unreachable
27: ; preds = %20
%28 = tail call i32 @rename(i8* %11, i8* %14)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = tail call i32 @rename(i8* %9, i8* %14)
%32 = tail call i32 @rename(i8* %11, i8* %9)
%33 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%34 = tail call i32 @exit(i32 2)
unreachable
35: ; preds = %27
ret i32 0
}