#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 *copy_substring(char *, unsigned int, unsigned int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
scalar_t__ realloc(char *, size_t);
size_t strlen(char *);
void append_or_concatenate_buffer(char **tokens, char *buf,
                                  unsigned int start_offset, unsigned int pos,
                                  unsigned int tokens_length) {
  char *token = copy_substring(buf, start_offset, pos);
  char *last_token = tokens[tokens_length - 1];
  if (last_token == NULL)
    tokens[tokens_length - 1] = token;
  else {
    size_t last_token_length = strlen(last_token);
    size_t token_length = strlen(token);
    size_t concat_token_length = last_token_length + token_length;
    tokens[tokens_length - 1] =
        (char *)realloc(tokens[tokens_length - 1], concat_token_length + 1);
    memcpy(tokens[tokens_length - 1] + last_token_length, token, token_length);
    tokens[tokens_length - 1][concat_token_length] = '\0';
    free(token);
  }
}


x86 assembly (clang, -O0):
<append_or_concatenate_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1050 <copy_substring@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b5 <append_or_concatenate_buffer+0x65>
mov    -0x28(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    %rdx,(%rax,%rcx,8)
jmp    124d <append_or_concatenate_buffer+0xfd>
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    (%rax,%rcx,8),%rdi
mov    -0x48(%rbp),%rsi
add    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,%rdx
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    (%rax,%rcx,8),%rdi
add    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    (%rax,%rcx,8),%rax
mov    -0x48(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_or_concatenate_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r13d
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %edx,%esi
mov    %ecx,%edx
call   1050 <copy_substring@plt>
mov    %rax,%r15
add    $0xffffffff,%r13d
mov    (%r14,%r13,8),%r12
test   %r12,%r12
je     11e1 <append_or_concatenate_buffer+0x91>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
add    %rbx,%rax
mov    %rax,(%rsp)
lea    (%rbx,%rbp,1),%rsi
add    $0x1,%rsi
mov    %r12,%rdi
call   1070 <realloc@plt>
mov    %rax,(%r14,%r13,8)
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1060 <memcpy@plt>
mov    (%r14,%r13,8),%rax
mov    (%rsp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
mov    %r15,(%r14,%r13,8)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @append_or_concatenate_buffer(i8** %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
store i8** %0, i8*** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%16 = load i8*, i8** %7
%17 = load i32, i32* %8
%18 = load i32, i32* %9
%19 = call i8* @copy_substring(i8* %16, i32 %17, i32 %18)
store i8* %19, i8** %11
%20 = load i8**, i8*** %6
%21 = load i32, i32* %10
%22 = sub i32 %21, 1
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %20, i64 %23
%25 = load i8*, i8** %24
store i8* %25, i8** %12
%26 = load i8*, i8** %12
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %35
28: ; preds = %5
%29 = load i8*, i8** %11
%30 = load i8**, i8*** %6
%31 = load i32, i32* %10
%32 = sub i32 %31, 1
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %30, i64 %33
store i8* %29, i8** %34
br label %79
35: ; preds = %5
%36 = load i8*, i8** %12
%37 = call i64 @strlen(i8* %36)
store i64 %37, i64* %13
%38 = load i8*, i8** %11
%39 = call i64 @strlen(i8* %38)
store i64 %39, i64* %14
%40 = load i64, i64* %13
%41 = load i64, i64* %14
%42 = add i64 %40, %41
store i64 %42, i64* %15
%43 = load i8**, i8*** %6
%44 = load i32, i32* %10
%45 = sub i32 %44, 1
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds i8*, i8** %43, i64 %46
%48 = load i8*, i8** %47
%49 = load i64, i64* %15
%50 = add i64 %49, 1
%51 = call i64 @realloc(i8* %48, i64 %50)
%52 = inttoptr i64 %51 to i8*
%53 = load i8**, i8*** %6
%54 = load i32, i32* %10
%55 = sub i32 %54, 1
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds i8*, i8** %53, i64 %56
store i8* %52, i8** %57
%58 = load i8**, i8*** %6
%59 = load i32, i32* %10
%60 = sub i32 %59, 1
%61 = zext i32 %60 to i64
%62 = getelementptr inbounds i8*, i8** %58, i64 %61
%63 = load i8*, i8** %62
%64 = load i64, i64* %13
%65 = getelementptr inbounds i8, i8* %63, i64 %64
%66 = load i8*, i8** %11
%67 = load i64, i64* %14
%68 = call i32 @memcpy(i8* %65, i8* %66, i64 %67)
%69 = load i8**, i8*** %6
%70 = load i32, i32* %10
%71 = sub i32 %70, 1
%72 = zext i32 %71 to i64
%73 = getelementptr inbounds i8*, i8** %69, i64 %72
%74 = load i8*, i8** %73
%75 = load i64, i64* %15
%76 = getelementptr inbounds i8, i8* %74, i64 %75
store i8 0, i8* %76
%77 = load i8*, i8** %11
%78 = call i32 @free(i8* %77)
br label %79
79: ; preds = %35, %28
ret void
}

LLVM IR (-O2):
define void @append_or_concatenate_buffer(i8** nocapture %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i8* @copy_substring(i8* %1, i32 %2, i32 %3)
%7 = add i32 %4, -1
%8 = zext i32 %7 to i64
%9 = getelementptr inbounds i8*, i8** %0, i64 %8
%10 = load i8*, i8** %9
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %5
store i8* %6, i8** %9
br label %25
13: ; preds = %5
%14 = tail call i64 @strlen(i8* %10)
%15 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%16 = add i64 %15, %14
%17 = add i64 %16, 1
%18 = tail call i64 @realloc(i8* %10, i64 %17)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %9
%20 = getelementptr inbounds i8, i8* %19, i64 %14
%21 = tail call i32 @memcpy(i8* %20, i8* %6, i64 %15)
%22 = load i8*, i8** %9
%23 = getelementptr inbounds i8, i8* %22, i64 %16
store i8 0, i8* %23
%24 = tail call i32 @free(i8* %6)
br label %25
25: ; preds = %13, %12
ret void
}