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

/* Variables and functions */
int S_IFLNK;
int S_IFMT;
char *basename(int /*<<< orphan*/);
char *dirname(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
scalar_t__ lstat(char const *, struct stat *);
int readlink(char const *, char *, int);
int /*<<< orphan*/ rename(char const *, char const *);
int /*<<< orphan*/ stderr;
char *strcat(char *, char *);
int /*<<< orphan*/ strdup(char const *);
scalar_t__ symlink(char *, char const *);
int /*<<< orphan*/ unlink(char const *);
void mgr_rename(const char *oldpath, const char *newpath) {
  struct stat sb;
  char ropath[256];
  if (lstat(oldpath, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFLNK) {
    int n = readlink(oldpath, ropath, 255);
    if (n > 0) {
      ropath[n] = 0;
      char *rnpath = strcat(strcat(dirname(strdup(ropath)), "/"),
                            basename(strdup(newpath)));
      rename(ropath, rnpath);
      unlink(oldpath);
      if (symlink(rnpath, newpath) != 0)
        fprintf(stderr, "symlink failed: %s -> %s\n", rnpath, newpath);
    } else {
      fprintf(stderr, "readlink failed: %s\n", oldpath);
    }
  } else {
    rename(oldpath, newpath);
  }
}


x86 assembly (clang, -O0):
<mgr_rename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <lstat@plt>
cmp    $0x0,%rax
jne    12e8 <mgr_rename+0x148>
mov    -0x18(%rbp),%eax
mov    0x2e04(%rip),%rcx
and    (%rcx),%eax
mov    0x2e0b(%rip),%rcx
cmp    (%rcx),%eax
jne    12e8 <mgr_rename+0x148>
mov    -0x8(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1040 <readlink@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jle    12c8 <mgr_rename+0x128>
movslq -0x124(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %eax,%edi
call   1060 <dirname@plt>
mov    %rax,%rdi
lea    0xdc7(%rip),%rsi
call   10b0 <strcat@plt>
mov    %rax,-0x138(%rbp)
mov    -0x10(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %eax,%edi
call   1090 <basename@plt>
mov    -0x138(%rbp),%rdi
mov    %rax,%rsi
call   10b0 <strcat@plt>
mov    %rax,-0x130(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x130(%rbp),%rsi
call   10a0 <rename@plt>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <symlink@plt>
cmp    $0x0,%rax
je     12c3 <mgr_rename+0x123>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    -0x130(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xd46(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    12e3 <mgr_rename+0x143>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd40(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    12f5 <mgr_rename+0x155>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <rename@plt>
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mgr_rename>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1080 <lstat@plt>
test   %rax,%rax
jne    12a3 <mgr_rename+0xf3>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%eax
jne    12a3 <mgr_rename+0xf3>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0xff,%edx
call   1040 <readlink@plt>
test   %eax,%eax
jle    12bc <mgr_rename+0x10c>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%r12
mov    %r12,%rdi
call   10d0 <strdup@plt>
mov    %eax,%edi
call   1070 <dirname@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <strlen@plt>
movw   $0x2f,(%r15,%rax,1)
mov    %r14,%rdi
call   10d0 <strdup@plt>
mov    %eax,%edi
call   10a0 <basename@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   10c0 <strcat@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   10b0 <rename@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <symlink@plt>
test   %rax,%rax
je     1294 <mgr_rename+0xe4>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xd79(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    %rbx,%rdi
mov    %r14,%rsi
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    10b0 <rename@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1090 <fprintf@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@S_IFLNK = global i32 0
@.str = [2 x i8] c"/\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"symlink failed: %s -> %s\0A\00"
@.str.2 = [21 x i8] c"readlink failed: %s\0A\00"
define void @mgr_rename(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i64 @lstat(i8* %9, %struct.stat* %5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %57
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @S_IFMT
%16 = and i32 %14, %15
%17 = load i32, i32* @S_IFLNK
%18 = icmp eq i32 %16, %17
br i1 %18, label %19, label %57
19: ; preds = %12
%20 = load i8*, i8** %3
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%22 = call i32 @readlink(i8* %20, i8* %21, i32 255)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %52
25: ; preds = %19
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%30 = call i32 @strdup(i8* %29)
%31 = call i8* @dirname(i32 %30)
%32 = call i8* @strcat(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%33 = load i8*, i8** %4
%34 = call i32 @strdup(i8* %33)
%35 = call i8* @basename(i32 %34)
%36 = call i8* @strcat(i8* %32, i8* %35)
store i8* %36, i8** %8
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%38 = load i8*, i8** %8
%39 = call i32 @rename(i8* %37, i8* %38)
%40 = load i8*, i8** %3
%41 = call i32 @unlink(i8* %40)
%42 = load i8*, i8** %8
%43 = load i8*, i8** %4
%44 = call i64 @symlink(i8* %42, i8* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %25
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %8
%49 = load i8*, i8** %4
%50 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %48, i8* %49)
br label %51
51: ; preds = %46, %25
br label %56
52: ; preds = %19
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %3
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %52, %51
br label %61
57: ; preds = %12, %2
%58 = load i8*, i8** %3
%59 = load i8*, i8** %4
%60 = call i32 @rename(i8* %58, i8* %59)
br label %61
61: ; preds = %57, %56
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@S_IFLNK = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"symlink failed: %s -> %s\0A\00"
@.str.2 = [21 x i8] c"readlink failed: %s\0A\00"
define void @mgr_rename(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [256 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = call i64 @lstat(i8* %0, %struct.stat* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %40
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @S_IFMT
%13 = and i32 %12, %11
%14 = load i32, i32* @S_IFLNK
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %40
16: ; preds = %9
%17 = call i32 @readlink(i8* %0, i8* %6, i32 255)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %16
%20 = zext i32 %17 to i64
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 @strdup(i8* %6)
%23 = call i8* @dirname(i32 %22)
%24 = call i64 @strlen(i8* dereferenceable(1) %23)
%25 = getelementptr i8, i8* %23, i64 %24
%26 = bitcast i8* %25 to i16*
store i16 47, i16* %26
%27 = call i32 @strdup(i8* %1)
%28 = call i8* @basename(i32 %27)
%29 = call i8* @strcat(i8* dereferenceable(1) %23, i8* dereferenceable(1) %28)
%30 = call i32 @rename(i8* %6, i8* %23)
%31 = call i32 @unlink(i8* %0)
%32 = call i64 @symlink(i8* %23, i8* %1)
%33 = icmp eq i64 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %19
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %1)
br label %42
37: ; preds = %16
%38 = load i32, i32* @stderr
%39 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %42
40: ; preds = %9, %2
%41 = tail call i32 @rename(i8* %0, i8* %1)
br label %42
42: ; preds = %37, %34, %19, %40
ret void
}