#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 */
struct dirent {
  int dummy;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ S_IRWXU;
int closedir(int /*<<< orphan*/ *);
int errno;
int mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int rename(char *, char *);
int rmdir(char *);
int test_rename(void) {
  int rc;
  DIR *dp;
  struct dirent *dirp;
  char *tmpname1 = "test-rename1.dir";
  char *tmpname2 = "test-rename2.dir";
  rc = mkdir(tmpname1, S_IRWXU);
  if (rc) {
    printf("Fail to create directory %s, errno = %d at line %d!\n", tmpname1,
           errno, 108);
    return (1);
  }
  dp = opendir(".");
  if (dp == NULL) {
    printf("Fail to open current directory, errno = %d at line %d!\n", errno,
           114);
    return (1);
  }
  while ((dirp = readdir(dp)) != NULL)
    ;
  rc = closedir(dp);
  if (rc < 0) {
    printf("Fail to close current directory, errno = %d at line %d!\n", errno,
           120);
    return (1);
  }
  rc = rename(tmpname1, tmpname2);
  if (rc) {
    printf("Fail to rename directory %s, errno = %d at line %d!\n", tmpname1,
           errno, 125);
    return (1);
  }
  rc = mkdir(tmpname1, S_IRWXU);
  if (rc) {
    printf("Fail to create directory %s, errno = %d at line %d!\n", tmpname1,
           errno, 131);
    return (1);
  }
  rc = rmdir(tmpname1);
  if (rc) {
    printf("Fail to remove directory %s, errno = %d at line %d!\n", tmpname1,
           errno, 136);
    return (1);
  }
  rc = rmdir(tmpname2);
  if (rc) {
    printf("Fail to remove directory %s, errno = %d at line %d!\n", tmpname2,
           errno, 141);
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<test_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe81(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe87(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     11d9 <test_rename+0x69>
mov    -0x20(%rbp),%rsi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
lea    0xe61(%rip),%rdi
mov    $0x6c,%ecx
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <test_rename+0x227>
lea    0xe77(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    121c <test_rename+0xac>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
mov    $0x72,%edx
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <test_rename+0x227>
jmp    1221 <test_rename+0xb1>
mov    -0x10(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     123d <test_rename+0xcd>
jmp    1221 <test_rename+0xb1>
mov    -0x10(%rbp),%rdi
call   1070 <closedir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    127b <test_rename+0x10b>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
lea    0xe2e(%rip),%rdi
mov    $0x78,%edx
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <test_rename+0x227>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <rename@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     12c1 <test_rename+0x151>
mov    -0x20(%rbp),%rsi
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
lea    0xe21(%rip),%rdi
mov    $0x7d,%ecx
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <test_rename+0x227>
mov    -0x20(%rbp),%rdi
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     130c <test_rename+0x19c>
mov    -0x20(%rbp),%rsi
mov    0x2ced(%rip),%rax
mov    (%rax),%edx
lea    0xd2e(%rip),%rdi
mov    $0x83,%ecx
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <test_rename+0x227>
mov    -0x20(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     134e <test_rename+0x1de>
mov    -0x20(%rbp),%rsi
mov    0x2cab(%rip),%rax
mov    (%rax),%edx
lea    0xdc9(%rip),%rdi
mov    $0x88,%ecx
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <test_rename+0x227>
mov    -0x28(%rbp),%rdi
call   1050 <rmdir@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1390 <test_rename+0x220>
mov    -0x28(%rbp),%rsi
mov    0x2c69(%rip),%rax
mov    (%rax),%edx
lea    0xd87(%rip),%rdi
mov    $0x8d,%ecx
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1397 <test_rename+0x227>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_rename>:
push   %r14
push   %rbx
push   %rax
mov    0x2e6d(%rip),%r14
mov    (%r14),%esi
lea    0xe7b(%rip),%rdi
call   1030 <mkdir@plt>
test   %eax,%eax
je     11b6 <test_rename+0x46>
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
lea    0xe84(%rip),%rdi
lea    0xe5b(%rip),%rsi
mov    $0x6c,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1251 <test_rename+0xe1>
lea    0xe9a(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     121e <test_rename+0xae>
mov    %rax,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11d0 <test_rename+0x60>
mov    %rbx,%rdi
call   1070 <closedir@plt>
test   %eax,%eax
js     1235 <test_rename+0xc5>
lea    0xe10(%rip),%rdi
lea    0xe1a(%rip),%rsi
call   1090 <rename@plt>
test   %eax,%eax
je     125e <test_rename+0xee>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
lea    0xeba(%rip),%rdi
lea    0xde9(%rip),%rsi
mov    $0x7d,%ecx
jmp    11aa <test_rename+0x3a>
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
lea    0xe2b(%rip),%rdi
mov    $0x72,%edx
jmp    124a <test_rename+0xda>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdi
mov    $0x78,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%r14),%esi
lea    0xd98(%rip),%rdi
call   1030 <mkdir@plt>
test   %eax,%eax
je     1292 <test_rename+0x122>
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
lea    0xda1(%rip),%rdi
lea    0xd78(%rip),%rsi
mov    $0x83,%ecx
jmp    11aa <test_rename+0x3a>
lea    0xd67(%rip),%rdi
call   1050 <rmdir@plt>
test   %eax,%eax
je     12c3 <test_rename+0x153>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
lea    0xe4d(%rip),%rdi
lea    0xd47(%rip),%rsi
mov    $0x88,%ecx
jmp    11aa <test_rename+0x3a>
lea    0xd47(%rip),%rdi
call   1050 <rmdir@plt>
test   %eax,%eax
je     12f4 <test_rename+0x184>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edx
lea    0xe1c(%rip),%rdi
lea    0xd27(%rip),%rsi
mov    $0x8d,%ecx
jmp    11aa <test_rename+0x3a>
xor    %eax,%eax
jmp    1256 <test_rename+0xe6>

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [17 x i8] c"test-rename1.dir\00"
@.str.1 = [17 x i8] c"test-rename2.dir\00"
@S_IRWXU = global i32 0
@.str.2 = [53 x i8] c"Fail to create directory %s, errno = %d at line %d!\0A\00"
@errno = global i32 0
@.str.3 = [2 x i8] c".\00"
@.str.4 = [56 x i8] c"Fail to open current directory, errno = %d at line %d!\0A\00"
@.str.5 = [57 x i8] c"Fail to close current directory, errno = %d at line %d!\0A\00"
@.str.6 = [53 x i8] c"Fail to rename directory %s, errno = %d at line %d!\0A\00"
@.str.7 = [53 x i8] c"Fail to remove directory %s, errno = %d at line %d!\0A\00"
define i32 @test_rename() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.dirent*
%5 = alloca i8*
%6 = alloca i8*
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %5
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8** %6
%7 = load i8*, i8** %5
%8 = load i32, i32* @S_IRWXU
%9 = call i32 @mkdir(i8* %7, i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = load i8*, i8** %5
%14 = load i32, i32* @errno
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %13, i32 %14, i32 108)
store i32 1, i32* %1
br label %76
16: ; preds = %0
%17 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %17, i32** %3
%18 = load i32*, i32** %3
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @errno
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0), i32 %21, i32 114)
store i32 1, i32* %1
br label %76
23: ; preds = %16
br label %24
24: ; preds = %28, %23
%25 = load i32*, i32** %3
%26 = call %struct.dirent* @readdir(i32* %25)
store %struct.dirent* %26, %struct.dirent** %4
%27 = icmp ne %struct.dirent* %26, null
br i1 %27, label %28, label %29
28: ; preds = %24
br label %24
29: ; preds = %24
%30 = load i32*, i32** %3
%31 = call i32 @closedir(i32* %30)
store i32 %31, i32* %2
%32 = load i32, i32* %2
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i32 %35, i32 120)
store i32 1, i32* %1
br label %76
37: ; preds = %29
%38 = load i8*, i8** %5
%39 = load i8*, i8** %6
%40 = call i32 @rename(i8* %38, i8* %39)
store i32 %40, i32* %2
%41 = load i32, i32* %2
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %37
%44 = load i8*, i8** %5
%45 = load i32, i32* @errno
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.6, i64 0, i64 0), i8* %44, i32 %45, i32 125)
store i32 1, i32* %1
br label %76
47: ; preds = %37
%48 = load i8*, i8** %5
%49 = load i32, i32* @S_IRWXU
%50 = call i32 @mkdir(i8* %48, i32 %49)
store i32 %50, i32* %2
%51 = load i32, i32* %2
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %47
%54 = load i8*, i8** %5
%55 = load i32, i32* @errno
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %54, i32 %55, i32 131)
store i32 1, i32* %1
br label %76
57: ; preds = %47
%58 = load i8*, i8** %5
%59 = call i32 @rmdir(i8* %58)
store i32 %59, i32* %2
%60 = load i32, i32* %2
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %57
%63 = load i8*, i8** %5
%64 = load i32, i32* @errno
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0), i8* %63, i32 %64, i32 136)
store i32 1, i32* %1
br label %76
66: ; preds = %57
%67 = load i8*, i8** %6
%68 = call i32 @rmdir(i8* %67)
store i32 %68, i32* %2
%69 = load i32, i32* %2
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %66
%72 = load i8*, i8** %6
%73 = load i32, i32* @errno
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0), i8* %72, i32 %73, i32 141)
store i32 1, i32* %1
br label %76
75: ; preds = %66
store i32 0, i32* %1
br label %76
76: ; preds = %75, %71, %62, %53, %43, %34, %20, %12
%77 = load i32, i32* %1
ret i32 %77
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [17 x i8] c"test-rename1.dir\00"
@.str.1 = [17 x i8] c"test-rename2.dir\00"
@S_IRWXU = global i32 0
@.str.2 = [53 x i8] c"Fail to create directory %s, errno = %d at line %d!\0A\00"
@errno = global i32 0
@.str.3 = [2 x i8] c".\00"
@.str.4 = [56 x i8] c"Fail to open current directory, errno = %d at line %d!\0A\00"
@.str.5 = [57 x i8] c"Fail to close current directory, errno = %d at line %d!\0A\00"
@.str.6 = [53 x i8] c"Fail to rename directory %s, errno = %d at line %d!\0A\00"
@.str.7 = [53 x i8] c"Fail to remove directory %s, errno = %d at line %d!\0A\00"
define i32 @test_rename() {
%1 = load i32, i32* @S_IRWXU
%2 = tail call i32 @mkdir(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = load i32, i32* @errno
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5, i32 108)
br label %47
7: ; preds = %0
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* @errno
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0), i32 %11, i32 114)
br label %47
13: ; preds = %7, %13
%14 = tail call %struct.dirent* @readdir(i32* %8)
%15 = icmp eq %struct.dirent* %14, null
br i1 %15, label %16, label %13
16: ; preds = %13
%17 = tail call i32 @closedir(i32* %8)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @.str.5, i64 0, i64 0), i32 %20, i32 120)
br label %47
22: ; preds = %16
%23 = tail call i32 @rename(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp eq i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %26, i32 125)
br label %47
28: ; preds = %22
%29 = load i32, i32* @S_IRWXU
%30 = tail call i32 @mkdir(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load i32, i32* @errno
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %33, i32 131)
br label %47
35: ; preds = %28
%36 = tail call i32 @rmdir(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%37 = icmp eq i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %39, i32 136)
br label %47
41: ; preds = %35
%42 = tail call i32 @rmdir(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%43 = icmp eq i32 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %41
%45 = load i32, i32* @errno
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %45, i32 141)
br label %47
47: ; preds = %41, %44, %38, %32, %25, %19, %10, %4
%48 = phi i32 [ 1, %4 ], [ 1, %10 ], [ 1, %19 ], [ 1, %25 ], [ 1, %32 ], [ 1, %38 ], [ 1, %44 ], [ 0, %41 ]
ret i32 %48
}