#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 */
char *gs_change(char *, size_t, size_t, char const *);
scalar_t__ realloc(char *, size_t);
size_t strlen(char const *);
char *gs_change_r(char *str, size_t pos, size_t len, const char *value) {
  if (!str)
    return NULL;
  str = (char *)realloc(str, strlen(str) + strlen(value) - len + 1);
  if (!str)
    return str;
  return gs_change(str, pos, len, value);
}


x86 assembly (clang, -O0):
<gs_change_r>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1160 <gs_change_r+0x30>
movq   $0x0,-0x8(%rbp)
jmp    11cb <gs_change_r+0x9b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdi
add    %rax,%rsi
sub    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b2 <gs_change_r+0x82>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11cb <gs_change_r+0x9b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <gs_change@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gs_change_r>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rdi,%rdi
je     118c <gs_change_r+0x5c>
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1030 <strlen@plt>
sub    %r12,%r13
lea    (%rax,%r13,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1040 <realloc@plt>
test   %rax,%rax
je     118c <gs_change_r+0x5c>
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r14,%rcx
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1050 <gs_change@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @gs_change_r(i8* %0, i64 %1, i64 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%10 = load i8*, i8** %6
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %4
store i8* null, i8** %5
br label %35
13: ; preds = %4
%14 = load i8*, i8** %6
%15 = load i8*, i8** %6
%16 = call i64 @strlen(i8* %15)
%17 = load i8*, i8** %9
%18 = call i64 @strlen(i8* %17)
%19 = add i64 %16, %18
%20 = load i64, i64* %8
%21 = sub i64 %19, %20
%22 = add i64 %21, 1
%23 = call i64 @realloc(i8* %14, i64 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = icmp ne i8* %25, null
br i1 %26, label %29, label %27
27: ; preds = %13
%28 = load i8*, i8** %6
store i8* %28, i8** %5
br label %35
29: ; preds = %13
%30 = load i8*, i8** %6
%31 = load i64, i64* %7
%32 = load i64, i64* %8
%33 = load i8*, i8** %9
%34 = call i8* @gs_change(i8* %30, i64 %31, i64 %32, i8* %33)
store i8* %34, i8** %5
br label %35
35: ; preds = %29, %27, %12
%36 = load i8*, i8** %5
ret i8* %36
}

LLVM IR (-O2):
define i8* @gs_change_r(i8* %0, i64 %1, i64 %2, i8* %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %17, label %6
6: ; preds = %4
%7 = tail call i64 @strlen(i8* %0)
%8 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%9 = sub i64 1, %2
%10 = add i64 %9, %7
%11 = add i64 %10, %8
%12 = tail call i64 @realloc(i8* %0, i64 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %6
%15 = inttoptr i64 %12 to i8*
%16 = tail call i8* @gs_change(i8* %15, i64 %1, i64 %2, i8* %3)
br label %17
17: ; preds = %6, %4, %14
%18 = phi i8* [ %16, %14 ], [ null, %4 ], [ null, %6 ]
ret i8* %18
}