#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 strlen(char *);
void setCharAt(int cursorPosition, char toChar, char currentCommand[]) {
  int i = 0, commandLength = strlen(currentCommand);
  if (commandLength == cursorPosition)
    currentCommand[commandLength] = toChar;
  else if (commandLength > cursorPosition) {
    while (i < strlen(currentCommand) - cursorPosition) {
      int lastPos = strlen(currentCommand) - i;
      currentCommand[lastPos] = currentCommand[lastPos - 1];
      i++;
    }
    currentCommand[cursorPosition] = toChar;
  }
}


x86 assembly (clang, -O0):
<setCharAt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    1157 <setCharAt+0x47>
mov    -0x5(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11d2 <setCharAt+0xc2>
mov    -0x18(%rbp),%eax
cmp    -0x4(%rbp),%eax
jle    11cd <setCharAt+0xbd>
jmp    1168 <setCharAt+0x58>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%eax
sub    -0x4(%rbp),%ecx
cmp    %ecx,%eax
jge    11bf <setCharAt+0xaf>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
sub    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1168 <setCharAt+0x58>
mov    -0x5(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x4(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11d2 <setCharAt+0xc2>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setCharAt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %esi,%r14d
mov    %edi,%r15d
mov    %rdx,%rdi
call   1030 <strlen@plt>
cmp    %r15d,%eax
je     1184 <setCharAt+0x74>
jle    118b <setCharAt+0x7b>
mov    %r12,%rdi
call   1030 <strlen@plt>
cmp    %r15d,%eax
jle    1184 <setCharAt+0x74>
xor    %r13d,%r13d
mov    $0xffffffff,%ebx
mov    %r12,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rbx,1),%ecx
movslq %ecx,%rcx
movzbl (%r12,%rcx,1),%ecx
cltq
mov    %cl,0x0(%rbp,%rax,1)
add    $0x1,%r13d
mov    %r12,%rdi
call   1030 <strlen@plt>
sub    %r15d,%eax
add    $0xffffffff,%ebx
add    $0xffffffffffffffff,%rbp
cmp    %eax,%r13d
jl     1150 <setCharAt+0x40>
movslq %r15d,%rax
mov    %r14b,(%r12,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @setCharAt(i32 %0, i8 signext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8 %1, i8* %5
store i8* %2, i8** %6
store i32 0, i32* %7
%10 = load i8*, i8** %6
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = load i32, i32* %4
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %21
15: ; preds = %3
%16 = load i8, i8* %5
%17 = load i8*, i8** %6
%18 = load i32, i32* %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
store i8 %16, i8* %20
br label %57
21: ; preds = %3
%22 = load i32, i32* %8
%23 = load i32, i32* %4
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %56
25: ; preds = %21
br label %26
26: ; preds = %33, %25
%27 = load i32, i32* %7
%28 = load i8*, i8** %6
%29 = call i32 @strlen(i8* %28)
%30 = load i32, i32* %4
%31 = sub nsw i32 %29, %30
%32 = icmp slt i32 %27, %31
br i1 %32, label %33, label %50
33: ; preds = %26
%34 = load i8*, i8** %6
%35 = call i32 @strlen(i8* %34)
%36 = load i32, i32* %7
%37 = sub nsw i32 %35, %36
store i32 %37, i32* %9
%38 = load i8*, i8** %6
%39 = load i32, i32* %9
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = load i8, i8* %42
%44 = load i8*, i8** %6
%45 = load i32, i32* %9
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 %43, i8* %47
%48 = load i32, i32* %7
%49 = add nsw i32 %48, 1
store i32 %49, i32* %7
br label %26
50: ; preds = %26
%51 = load i8, i8* %5
%52 = load i8*, i8** %6
%53 = load i32, i32* %4
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
store i8 %51, i8* %55
br label %56
56: ; preds = %50, %21
br label %57
57: ; preds = %56, %15
ret void
}

LLVM IR (-O2):
define void @setCharAt(i32 %0, i8 signext %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %2)
%5 = icmp eq i32 %4, %0
br i1 %5, label %25, label %6
6: ; preds = %3
%7 = icmp sgt i32 %4, %0
br i1 %7, label %8, label %28
8: ; preds = %6
%9 = tail call i32 @strlen(i8* %2)
%10 = icmp sgt i32 %9, %0
br i1 %10, label %11, label %25
11: ; preds = %8, %11
%12 = phi i32 [ %21, %11 ], [ 0, %8 ]
%13 = tail call i32 @strlen(i8* %2)
%14 = sub nsw i32 %13, %12
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %2, i64 %16
%18 = load i8, i8* %17
%19 = sext i32 %14 to i64
%20 = getelementptr inbounds i8, i8* %2, i64 %19
store i8 %18, i8* %20
%21 = add nuw nsw i32 %12, 1
%22 = tail call i32 @strlen(i8* %2)
%23 = sub nsw i32 %22, %0
%24 = icmp slt i32 %21, %23
br i1 %24, label %11, label %25
25: ; preds = %11, %8, %3
%26 = sext i32 %0 to i64
%27 = getelementptr inbounds i8, i8* %2, i64 %26
store i8 %1, i8* %27
br label %28
28: ; preds = %25, %6
ret void
}