#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 stat {
  int dummy;
};

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, int, scalar_t__, ...);
int rename(char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stop();
char *strerrno(scalar_t__);
int test_rename() {
  int ret;
  struct stat st;
  errno = 0;
  ret = rename("foo.fileio.test", "bar.fileio.test");
  if (!ret) {
    errno = 0;
    ret = stat("foo.fileio.test", &st);
    if (ret && errno == ENOENT) {
      errno = 0;
      ret = stat("bar.fileio.test", &st);
      printf("rename 1: ret = %d, errno = %d %s\n", ret, errno,
             strerrno(errno));
      errno = 0;
    } else
      printf("rename 1: ret = %d, errno = %d\n", ret, errno);
  } else
    printf("rename 1: ret = %d, errno = %d\n", ret, errno);
  stop();
  errno = 0;
  ret = rename("bar.fileio.test", "dir2.fileio.test");
  printf("rename 2: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  stop();
  errno = 0;
  ret = rename("dir2.fileio.test", "dir1.fileio.test");
  printf("rename 3: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  stop();
  errno = 0;
  ret = rename("dir1.fileio.test", "dir1.fileio.test/subdir.fileio.test");
  printf("rename 4: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  stop();
  errno = 0;
  ret = rename("nofoo.fileio.test", "foo.fileio.test");
  printf("rename 5: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  stop();
}


x86 assembly (clang, -O0):
<test_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x2e81(%rip),%rax
movq   $0x0,(%rax)
lea    0xf97(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1070 <rename@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    125a <test_rename+0x10a>
mov    0x2e53(%rip),%rax
movq   $0x0,(%rax)
lea    0xf69(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     123a <test_rename+0xea>
mov    0x2e28(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    123a <test_rename+0xea>
mov    0x2e0e(%rip),%rax
movq   $0x0,(%rax)
lea    0xe20(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%rdi
call   1030 <strerrno@plt>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2db2(%rip),%rax
movq   $0x0,(%rax)
jmp    1255 <test_rename+0x105>
mov    -0x8(%rbp),%esi
mov    0x2d9c(%rip),%rax
mov    (%rax),%rdx
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1275 <test_rename+0x125>
mov    -0x8(%rbp),%esi
mov    0x2d7c(%rip),%rax
mov    (%rax),%rdx
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    0x2d5d(%rip),%rax
movq   $0x0,(%rax)
lea    0xd6f(%rip),%rdi
lea    0xdbb(%rip),%rsi
call   1070 <rename@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2d33(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%rdi
call   1030 <strerrno@plt>
mov    -0x5c(%rbp),%esi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    0x2cf7(%rip),%rax
movq   $0x0,(%rax)
lea    0xd5c(%rip),%rdi
lea    0xd89(%rip),%rsi
call   1070 <rename@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2ccd(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cbf(%rip),%rax
mov    (%rax),%rdi
call   1030 <strerrno@plt>
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    0x2c91(%rip),%rax
movq   $0x0,(%rax)
lea    0xd2a(%rip),%rdi
lea    0xd57(%rip),%rsi
call   1070 <rename@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2c67(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2c59(%rip),%rax
mov    (%rax),%rdi
call   1030 <strerrno@plt>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xd3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    0x2c2b(%rip),%rax
movq   $0x0,(%rax)
lea    0xd3f(%rip),%rdi
lea    0xd3a(%rip),%rsi
call   1070 <rename@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2c01(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2bf3(%rip),%rax
mov    (%rax),%rdi
call   1030 <strerrno@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xd0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <stop@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_rename>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e7d(%rip),%r12
movq   $0x0,(%r12)
lea    0xf92(%rip),%rdi
lea    0xe87(%rip),%rsi
call   1070 <rename@plt>
test   %eax,%eax
je     12c3 <test_rename+0x173>
mov    (%r12),%rdx
lea    0xea2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <stop@plt>
movq   $0x0,(%r12)
lea    0xe50(%rip),%rdi
lea    0xe9c(%rip),%r14
mov    %r14,%rsi
call   1070 <rename@plt>
mov    %eax,%ebp
mov    (%r12),%rbx
mov    %rbx,%rdi
call   1030 <strerrno@plt>
lea    0xe90(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <stop@plt>
movq   $0x0,(%r12)
lea    0xe8e(%rip),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <rename@plt>
mov    %eax,%ebp
mov    (%r12),%rbx
mov    %rbx,%rdi
call   1030 <strerrno@plt>
lea    0xe7f(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <stop@plt>
movq   $0x0,(%r12)
lea    0xe7d(%rip),%rsi
mov    %r15,%rdi
call   1070 <rename@plt>
mov    %eax,%ebx
mov    (%r12),%rbp
mov    %rbp,%rdi
call   1030 <strerrno@plt>
lea    0xe84(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <stop@plt>
movq   $0x0,(%r12)
lea    0xe82(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1070 <rename@plt>
mov    %eax,%ebx
mov    (%r12),%rbp
mov    %rbp,%rdi
call   1030 <strerrno@plt>
lea    0xe73(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <stop@plt>
movq   $0x0,(%r12)
lea    0xe32(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
mov    (%r12),%rdx
test   %eax,%eax
je     118a <test_rename+0x3a>
mov    0x2ce1(%rip),%rcx
cmp    (%rcx),%rdx
jne    118a <test_rename+0x3a>
movq   $0x0,(%r12)
lea    0xcf9(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
mov    %eax,%ebp
mov    (%r12),%rbx
mov    %rbx,%rdi
call   1030 <strerrno@plt>
lea    0xcea(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
movq   $0x0,(%r12)
jmp    119a <test_rename+0x4a>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@.str = [16 x i8] c"foo.fileio.test\00"
@.str.1 = [16 x i8] c"bar.fileio.test\00"
@ENOENT = global i64 0
@.str.2 = [35 x i8] c"rename 1: ret = %d, errno = %d %s\0A\00"
@.str.3 = [32 x i8] c"rename 1: ret = %d, errno = %d\0A\00"
@.str.4 = [17 x i8] c"dir2.fileio.test\00"
@.str.5 = [35 x i8] c"rename 2: ret = %d, errno = %d %s\0A\00"
@.str.6 = [17 x i8] c"dir1.fileio.test\00"
@.str.7 = [35 x i8] c"rename 3: ret = %d, errno = %d %s\0A\00"
@.str.8 = [36 x i8] c"dir1.fileio.test/subdir.fileio.test\00"
@.str.9 = [35 x i8] c"rename 4: ret = %d, errno = %d %s\0A\00"
@.str.10 = [18 x i8] c"nofoo.fileio.test\00"
@.str.11 = [35 x i8] c"rename 5: ret = %d, errno = %d %s\0A\00"
define i32 @test_rename() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.stat
store i64 0, i64* @errno
%4 = call i32 @rename(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = icmp ne i32 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %0
store i64 0, i64* @errno
%8 = call i32 @stat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %7
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOENT
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %22
15: ; preds = %11
store i64 0, i64* @errno
%16 = call i32 @stat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = load i64, i64* @errno
%19 = load i64, i64* @errno
%20 = call i8* @strerrno(i64 %19)
%21 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %17, i64 %18, i8* %20)
store i64 0, i64* @errno
br label %26
22: ; preds = %11, %7
%23 = load i32, i32* %2
%24 = load i64, i64* @errno
%25 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %23, i64 %24)
br label %26
26: ; preds = %22, %15
br label %31
27: ; preds = %0
%28 = load i32, i32* %2
%29 = load i64, i64* @errno
%30 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %28, i64 %29)
br label %31
31: ; preds = %27, %26
%32 = call i32 (...) @stop()
store i64 0, i64* @errno
%33 = call i32 @rename(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
store i32 %33, i32* %2
%34 = load i32, i32* %2
%35 = load i64, i64* @errno
%36 = load i64, i64* @errno
%37 = call i8* @strerrno(i64 %36)
%38 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %34, i64 %35, i8* %37)
%39 = call i32 (...) @stop()
store i64 0, i64* @errno
%40 = call i32 @rename(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
store i32 %40, i32* %2
%41 = load i32, i32* %2
%42 = load i64, i64* @errno
%43 = load i64, i64* @errno
%44 = call i8* @strerrno(i64 %43)
%45 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %41, i64 %42, i8* %44)
%46 = call i32 (...) @stop()
store i64 0, i64* @errno
%47 = call i32 @rename(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0))
store i32 %47, i32* %2
%48 = load i32, i32* %2
%49 = load i64, i64* @errno
%50 = load i64, i64* @errno
%51 = call i8* @strerrno(i64 %50)
%52 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0), i32 %48, i64 %49, i8* %51)
%53 = call i32 (...) @stop()
store i64 0, i64* @errno
%54 = call i32 @rename(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 %54, i32* %2
%55 = load i32, i32* %2
%56 = load i64, i64* @errno
%57 = load i64, i64* @errno
%58 = call i8* @strerrno(i64 %57)
%59 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.11, i64 0, i64 0), i32 %55, i64 %56, i8* %58)
%60 = call i32 (...) @stop()
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@.str = [16 x i8] c"foo.fileio.test\00"
@.str.1 = [16 x i8] c"bar.fileio.test\00"
@ENOENT = global i64 0
@.str.2 = [35 x i8] c"rename 1: ret = %d, errno = %d %s\0A\00"
@.str.3 = [32 x i8] c"rename 1: ret = %d, errno = %d\0A\00"
@.str.4 = [17 x i8] c"dir2.fileio.test\00"
@.str.5 = [35 x i8] c"rename 2: ret = %d, errno = %d %s\0A\00"
@.str.6 = [17 x i8] c"dir1.fileio.test\00"
@.str.7 = [35 x i8] c"rename 3: ret = %d, errno = %d %s\0A\00"
@.str.8 = [36 x i8] c"dir1.fileio.test/subdir.fileio.test\00"
@.str.9 = [35 x i8] c"rename 4: ret = %d, errno = %d %s\0A\00"
@.str.10 = [18 x i8] c"nofoo.fileio.test\00"
@.str.11 = [35 x i8] c"rename 5: ret = %d, errno = %d %s\0A\00"
define i32 @test_rename() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
store i64 0, i64* @errno
%3 = tail call i32 @rename(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %19
5: ; preds = %0
store i64 0, i64* @errno
%6 = call i32 @stat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%7 = icmp ne i32 %6, 0
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
%11 = select i1 %7, i1 %10, i1 false
br i1 %11, label %12, label %17
12: ; preds = %5
store i64 0, i64* @errno
%13 = call i32 @stat(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %1)
%14 = load i64, i64* @errno
%15 = tail call i8* @strerrno(i64 %14)
%16 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i32 %13, i64 %14, i8* %15)
store i64 0, i64* @errno
br label %22
17: ; preds = %5
%18 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %6, i64 %8)
br label %22
19: ; preds = %0
%20 = load i64, i64* @errno
%21 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %3, i64 %20)
br label %22
22: ; preds = %12, %17, %19
%23 = tail call i32 (...) @stop()
store i64 0, i64* @errno
%24 = tail call i32 @rename(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%25 = load i64, i64* @errno
%26 = tail call i8* @strerrno(i64 %25)
%27 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %24, i64 %25, i8* %26)
%28 = tail call i32 (...) @stop()
store i64 0, i64* @errno
%29 = tail call i32 @rename(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%30 = load i64, i64* @errno
%31 = tail call i8* @strerrno(i64 %30)
%32 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %29, i64 %30, i8* %31)
%33 = tail call i32 (...) @stop()
store i64 0, i64* @errno
%34 = tail call i32 @rename(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.8, i64 0, i64 0))
%35 = load i64, i64* @errno
%36 = tail call i8* @strerrno(i64 %35)
%37 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0), i32 %34, i64 %35, i8* %36)
%38 = tail call i32 (...) @stop()
store i64 0, i64* @errno
%39 = tail call i32 @rename(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%40 = load i64, i64* @errno
%41 = tail call i8* @strerrno(i64 %40)
%42 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.11, i64 0, i64 0), i32 %39, i64 %40, i8* %41)
%43 = tail call i32 (...) @stop()
ret i32 undef
}