#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 */

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ printf(char *, char *);
int readlink(char const *, char *, int);
int /*<<< orphan*/ remove(char const *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, int);
int strlen(char const *);
int /*<<< orphan*/ symlink(char *, char const *);
void switch_sym_link(const char *file_name_ptr) {
  char buf[(20)];
  int bufsize = 0;
  int file_index = 0;
  bufsize = readlink(file_name_ptr, buf, (20));
  buf[bufsize + 1] = '\0';
  printf("Currently pointing to %s\n", buf);
  remove(file_name_ptr);
  int index_of_int = strlen(file_name_ptr) + 1;
  file_index = atoi(buf + index_of_int);
  file_index++;
  sprintf(buf, "%s.%d", file_name_ptr, file_index);
  symlink(buf, file_name_ptr);
  printf("Now pointing to %s\n", buf);
}


x86 assembly (clang, -O0):
<switch_sym_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x14,%edx
call   1040 <readlink@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0x0,-0x20(%rbp,%rax,1)
lea    -0x20(%rbp),%rsi
lea    0xe49(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1030 <remove@plt>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
lea    -0x20(%rbp),%rdi
movslq -0x2c(%rbp),%rax
add    %rax,%rdi
call   1080 <atoi@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x28(%rbp),%ecx
lea    0xe18(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <symlink@plt>
lea    -0x20(%rbp),%rsi
lea    0xe01(%rip),%rdi
call   1060 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<switch_sym_link>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    %rsp,%r14
mov    %r14,%rsi
mov    $0x14,%edx
call   1040 <readlink@plt>
cltq
movb   $0x0,0x1(%rax,%r14,1)
lea    0xe67(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <remove@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
lea    (%rax,%r14,1),%rdi
add    $0x1,%rdi
call   1080 <atoi@plt>
lea    0x1(%rax),%ecx
lea    0xe50(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1090 <sprintf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1070 <symlink@plt>
lea    0xe39(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Currently pointing to %s\0A\00"
@.str.1 = [6 x i8] c"%s.%d\00"
@.str.2 = [20 x i8] c"Now pointing to %s\0A\00"
define void @switch_sym_link(i8* %0) {
%2 = alloca i8*
%3 = alloca [20 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
store i32 0, i32* %5
%7 = load i8*, i8** %2
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%9 = call i32 @readlink(i8* %7, i8* %8, i32 20)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 %12
store i8 0, i8* %13
%14 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%15 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i8*, i8** %2
%17 = call i32 @remove(i8* %16)
%18 = load i8*, i8** %2
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %19, 1
store i32 %20, i32* %6
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
%28 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%29 = load i8*, i8** %2
%30 = load i32, i32* %5
%31 = call i32 @sprintf(i8* %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %29, i32 %30)
%32 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%33 = load i8*, i8** %2
%34 = call i32 @symlink(i8* %32, i8* %33)
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%36 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %35)
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"Currently pointing to %s\0A\00"
@.str.1 = [6 x i8] c"%s.%d\00"
@.str.2 = [20 x i8] c"Now pointing to %s\0A\00"
define void @switch_sym_link(i8* %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = call i32 @readlink(i8* %0, i8* %3, i32 20)
%5 = add nsw i32 %4, 1
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 %6
store i8 0, i8* %7
%8 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %3)
%9 = call i32 @remove(i8* %0)
%10 = call i32 @strlen(i8* %0)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 %12
%14 = call i32 @atoi(i8* %13)
%15 = add nsw i32 %14, 1
%16 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %15)
%17 = call i32 @symlink(i8* %3, i8* %0)
%18 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %3)
ret void
}