#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 *);
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);
  errno = 0;
  ret = rename("bar.fileio.test", "dir2.fileio.test");
  printf("rename 2: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  errno = 0;
  ret = rename("dir2.fileio.test", "dir1.fileio.test");
  printf("rename 3: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  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));
  errno = 0;
  ret = rename("nofoo.fileio.test", "foo.fileio.test");
  printf("rename 5: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
}


x86 assembly (clang, -O0):
<test_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x2e91(%rip),%rax
movq   $0x0,(%rax)
lea    0xfa7(%rip),%rdi
lea    0xe9c(%rip),%rsi
call   1060 <rename@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    124a <test_rename+0x10a>
mov    0x2e63(%rip),%rax
movq   $0x0,(%rax)
lea    0xf79(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     122a <test_rename+0xea>
mov    0x2e38(%rip),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    122a <test_rename+0xea>
mov    0x2e1e(%rip),%rax
movq   $0x0,(%rax)
lea    0xe30(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%rdi
call   1030 <strerrno@plt>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    %rax,%rcx
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dc2(%rip),%rax
movq   $0x0,(%rax)
jmp    1245 <test_rename+0x105>
mov    -0x8(%rbp),%esi
mov    0x2dac(%rip),%rax
mov    (%rax),%rdx
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1265 <test_rename+0x125>
mov    -0x8(%rbp),%esi
mov    0x2d8c(%rip),%rax
mov    (%rax),%rdx
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d74(%rip),%rax
movq   $0x0,(%rax)
lea    0xd86(%rip),%rdi
lea    0xdd2(%rip),%rsi
call   1060 <rename@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x58(%rbp)
mov    0x2d3c(%rip),%rax
mov    (%rax),%rdi
call   1030 <strerrno@plt>
mov    -0x5c(%rbp),%esi
mov    -0x58(%rbp),%rdx
mov    %rax,%rcx
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d15(%rip),%rax
movq   $0x0,(%rax)
lea    0xd7a(%rip),%rdi
lea    0xda7(%rip),%rsi
call   1060 <rename@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2ceb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cdd(%rip),%rax
mov    (%rax),%rdi
call   1030 <strerrno@plt>
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2cb6(%rip),%rax
movq   $0x0,(%rax)
lea    0xd4f(%rip),%rdi
lea    0xd7c(%rip),%rsi
call   1060 <rename@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2c8c(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2c7e(%rip),%rax
mov    (%rax),%rdi
call   1030 <strerrno@plt>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c57(%rip),%rax
movq   $0x0,(%rax)
lea    0xd6b(%rip),%rdi
lea    0xd66(%rip),%rsi
call   1060 <rename@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2c2d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c1f(%rip),%rax
mov    (%rax),%rdi
call   1030 <strerrno@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@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    0x2e8d(%rip),%r12
movq   $0x0,(%r12)
lea    0xfa2(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1060 <rename@plt>
test   %eax,%eax
je     1290 <test_rename+0x150>
mov    (%r12),%rdx
lea    0xeb2(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movq   $0x0,(%r12)
lea    0xe67(%rip),%rdi
lea    0xeb3(%rip),%r14
mov    %r14,%rsi
call   1060 <rename@plt>
mov    %eax,%ebp
mov    (%r12),%rbx
mov    %rbx,%rdi
call   1030 <strerrno@plt>
lea    0xea7(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
movq   $0x0,(%r12)
lea    0xeac(%rip),%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <rename@plt>
mov    %eax,%ebp
mov    (%r12),%rbx
mov    %rbx,%rdi
call   1030 <strerrno@plt>
lea    0xe9d(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
movq   $0x0,(%r12)
lea    0xea2(%rip),%rsi
mov    %r15,%rdi
call   1060 <rename@plt>
mov    %eax,%ebx
mov    (%r12),%rbp
mov    %rbp,%rdi
call   1030 <strerrno@plt>
lea    0xea9(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
movq   $0x0,(%r12)
lea    0xeae(%rip),%rdi
lea    0xea9(%rip),%rsi
call   1060 <rename@plt>
mov    %eax,%ebx
mov    (%r12),%rbp
mov    %rbp,%rdi
call   1030 <strerrno@plt>
lea    0xe9f(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
movq   $0x0,(%r12)
lea    0xe65(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
mov    (%r12),%rdx
test   %eax,%eax
je     117a <test_rename+0x3a>
mov    0x2d14(%rip),%rcx
cmp    (%rcx),%rdx
jne    117a <test_rename+0x3a>
movq   $0x0,(%r12)
lea    0xd2c(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
mov    %eax,%ebp
mov    (%r12),%rbx
mov    %rbx,%rdi
call   1030 <strerrno@plt>
lea    0xd1d(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    118a <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
store i64 0, i64* @errno
%32 = 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 %32, i32* %2
%33 = load i32, i32* %2
%34 = load i64, i64* @errno
%35 = load i64, i64* @errno
%36 = call i8* @strerrno(i64 %35)
%37 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %33, i64 %34, i8* %36)
store i64 0, i64* @errno
%38 = 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 %38, i32* %2
%39 = load i32, i32* %2
%40 = load i64, i64* @errno
%41 = load i64, i64* @errno
%42 = call i8* @strerrno(i64 %41)
%43 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %39, i64 %40, i8* %42)
store i64 0, i64* @errno
%44 = 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 %44, i32* %2
%45 = load i32, i32* %2
%46 = load i64, i64* @errno
%47 = load i64, i64* @errno
%48 = call i8* @strerrno(i64 %47)
%49 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0), i32 %45, i64 %46, i8* %48)
store i64 0, i64* @errno
%50 = 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 %50, i32* %2
%51 = load i32, i32* %2
%52 = load i64, i64* @errno
%53 = load i64, i64* @errno
%54 = call i8* @strerrno(i64 %53)
%55 = call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.11, i64 0, i64 0), i32 %51, i64 %52, i8* %54)
%56 = load i32, i32* %1
ret i32 %56
}

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)
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
store i64 0, i64* @errno
%23 = 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))
%24 = load i64, i64* @errno
%25 = tail call i8* @strerrno(i64 %24)
%26 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %23, i64 %24, i8* %25)
store i64 0, i64* @errno
%27 = 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))
%28 = load i64, i64* @errno
%29 = tail call i8* @strerrno(i64 %28)
%30 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %27, i64 %28, i8* %29)
store i64 0, i64* @errno
%31 = 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))
%32 = load i64, i64* @errno
%33 = tail call i8* @strerrno(i64 %32)
%34 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0), i32 %31, i64 %32, i8* %33)
store i64 0, i64* @errno
%35 = 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))
%36 = load i64, i64* @errno
%37 = tail call i8* @strerrno(i64 %36)
%38 = tail call i32 (i8*, i32, i64, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.11, i64 0, i64 0), i32 %35, i64 %36, i8* %37)
ret i32 undef
}