#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 /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int EINVAL;
int EISDIR;
int ENOENT;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int errno;
scalar_t__ rename(char const *, char const *);
scalar_t__ stat(char const *, struct stat *);
int os_file_rename(const char *src, const char *dest) {
  struct stat src_stat, dest_stat;
  if (src == NULL || dest == NULL)
    return -EINVAL;
  if (stat(src, &src_stat) != 0)
    return -errno;
  if (S_ISDIR(src_stat.st_mode))
    return -EISDIR;
  if (stat(dest, &dest_stat) != 0 && errno != ENOENT)
    return -errno;
  if (errno != ENOENT && S_ISDIR(dest_stat.st_mode))
    return -EISDIR;
  if (rename(src, dest) != 0)
    return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<os_file_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <os_file_rename+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1169 <os_file_rename+0x39>
xor    %eax,%eax
mov    0x2e71(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <os_file_rename+0x138>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     1193 <os_file_rename+0x63>
xor    %eax,%eax
mov    0x2e4f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <os_file_rename+0x138>
mov    -0x20(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11b8 <os_file_rename+0x88>
xor    %eax,%eax
mov    0x2e3a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <os_file_rename+0x138>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     11fa <os_file_rename+0xca>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%eax
je     11fa <os_file_rename+0xca>
xor    %eax,%eax
mov    0x2de8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <os_file_rename+0x138>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%eax
je     1237 <os_file_rename+0x107>
mov    -0x28(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1237 <os_file_rename+0x107>
xor    %eax,%eax
mov    0x2dbb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <os_file_rename+0x138>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <rename@plt>
cmp    $0x0,%rax
je     1261 <os_file_rename+0x131>
xor    %eax,%eax
mov    0x2d81(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1268 <os_file_rename+0x138>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_file_rename>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1161 <os_file_rename+0x31>
mov    %rsi,%r14
test   %rsi,%rsi
je     1161 <os_file_rename+0x31>
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
test   %rax,%rax
je     116a <os_file_rename+0x3a>
mov    0x2e79(%rip),%rcx
jmp    117f <os_file_rename+0x4f>
mov    0x2e68(%rip),%rcx
jmp    117f <os_file_rename+0x4f>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
je     118d <os_file_rename+0x5d>
mov    0x2e69(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <stat@plt>
mov    %rax,%rcx
mov    0x2e36(%rip),%r15
mov    (%r15),%eax
mov    0x2e14(%rip),%rdx
mov    (%rdx),%edx
test   %rcx,%rcx
je     11bb <os_file_rename+0x8b>
cmp    %edx,%eax
je     11cc <os_file_rename+0x9c>
neg    %eax
jmp    1183 <os_file_rename+0x53>
cmp    %edx,%eax
je     11cc <os_file_rename+0x9c>
mov    (%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
jne    1178 <os_file_rename+0x48>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <rename@plt>
test   %rax,%rax
je     11e3 <os_file_rename+0xb3>
xor    %eax,%eax
sub    (%r15),%eax
jmp    1183 <os_file_rename+0x53>
xor    %eax,%eax
jmp    1183 <os_file_rename+0x53>

LLVM IR (-O0):
%struct.stat = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@EISDIR = global i32 0
@ENOENT = global i32 0
define i32 @os_file_rename(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10, %2
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
store i32 %15, i32* %3
br label %63
16: ; preds = %10
%17 = load i8*, i8** %4
%18 = call i64 @stat(i8* %17, %struct.stat* %6)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
store i32 %22, i32* %3
br label %63
23: ; preds = %16
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @S_ISDIR(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i32, i32* @EISDIR
%30 = sub nsw i32 0, %29
store i32 %30, i32* %3
br label %63
31: ; preds = %23
%32 = load i8*, i8** %5
%33 = call i64 @stat(i8* %32, %struct.stat* %7)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = load i32, i32* @errno
%37 = load i32, i32* @ENOENT
%38 = icmp ne i32 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i32, i32* @errno
%41 = sub nsw i32 0, %40
store i32 %41, i32* %3
br label %63
42: ; preds = %35, %31
%43 = load i32, i32* @errno
%44 = load i32, i32* @ENOENT
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %54
46: ; preds = %42
%47 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i64 @S_ISDIR(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %46
%52 = load i32, i32* @EISDIR
%53 = sub nsw i32 0, %52
store i32 %53, i32* %3
br label %63
54: ; preds = %46, %42
%55 = load i8*, i8** %4
%56 = load i8*, i8** %5
%57 = call i64 @rename(i8* %55, i8* %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = load i32, i32* @errno
%61 = sub nsw i32 0, %60
store i32 %61, i32* %3
br label %63
62: ; preds = %54
store i32 0, i32* %3
br label %63
63: ; preds = %62, %59, %51, %39, %28, %20, %13
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@EISDIR = global i32 0
@ENOENT = global i32 0
define i32 @os_file_rename(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp eq i8* %0, null
%8 = icmp eq i8* %1, null
%9 = or i1 %7, %8
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @EINVAL
%12 = sub nsw i32 0, %11
br label %51
13: ; preds = %2
%14 = call i64 @stat(i8* %0, %struct.stat* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
br label %51
19: ; preds = %13
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i64 @S_ISDIR(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i32, i32* @EISDIR
%26 = sub nsw i32 0, %25
br label %51
27: ; preds = %19
%28 = call i64 @stat(i8* %1, %struct.stat* %4)
%29 = icmp eq i64 %28, 0
%30 = load i32, i32* @errno
%31 = load i32, i32* @ENOENT
%32 = icmp eq i32 %30, %31
br i1 %29, label %36, label %33
33: ; preds = %27
br i1 %32, label %45, label %34
34: ; preds = %33
%35 = sub nsw i32 0, %30
br label %51
36: ; preds = %27
br i1 %32, label %45, label %37
37: ; preds = %36
%38 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i64 @S_ISDIR(i32 %39)
%41 = icmp eq i64 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %37
%43 = load i32, i32* @EISDIR
%44 = sub nsw i32 0, %43
br label %51
45: ; preds = %33, %37, %36
%46 = call i64 @rename(i8* %0, i8* %1)
%47 = icmp eq i64 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %45
%49 = load i32, i32* @errno
%50 = sub nsw i32 0, %49
br label %51
51: ; preds = %45, %48, %42, %34, %24, %16, %10
%52 = phi i32 [ %12, %10 ], [ %18, %16 ], [ %26, %24 ], [ %35, %34 ], [ %44, %42 ], [ %50, %48 ], [ 0, %45 ]
ret i32 %52
}