#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 *realloc(char *, size_t);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *extend_string(char str[], const char with[], size_t *len) {
  size_t with_len = strlen(with);
  char *new = realloc(str, *len + with_len + 1);
  if (new == NULL) {
    return str;
  }
  strncpy(new + *len, with, with_len + 1);
  *len += with_len;
  return new;
}


x86 assembly (clang, -O0):
<extend_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
add    -0x28(%rbp),%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1185 <extend_string+0x55>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b7 <extend_string+0x87>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
add    (%rax),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extend_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%r12
mov    0x0(%r13),%rsi
add    %r12,%rsi
mov    %rbx,%rdi
call   1050 <realloc@plt>
test   %rax,%rax
je     1183 <extend_string+0x53>
mov    %rax,%rbp
mov    0x0(%r13),%rdi
add    %rax,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
add    %r15,0x0(%r13)
mov    %rbp,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @extend_string(i8* %0, i8* %1, i64* nocapture %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = load i64, i64* %2
%6 = add i64 %4, 1
%7 = add i64 %6, %5
%8 = tail call i8* @realloc(i8* %0, i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %16, label %10
10: ; preds = %3
%11 = load i64, i64* %2
%12 = getelementptr inbounds i8, i8* %8, i64 %11
%13 = tail call i32 @strncpy(i8* %12, i8* %1, i64 %6)
%14 = load i64, i64* %2
%15 = add i64 %14, %4
store i64 %15, i64* %2
br label %16
16: ; preds = %3, %10
%17 = phi i8* [ %8, %10 ], [ %0, %3 ]
ret i8* %17
}