#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 *grealloc(char *, long);
long str_length(char const *);
char *str_extend(char *a, const char *b) {
  long len = str_length(a) + str_length(b), off = str_length(a);
  if (len < 0)
    return NULL;
  a = grealloc(a, len + 1);
  while (*b)
    *(a + off++) = *b++;
  *(a + off) = 0;
  return a;
}


x86 assembly (clang, -O0):
<str_extend>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <str_length@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <str_length@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <str_length@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    1179 <str_extend+0x59>
movq   $0x0,-0x8(%rbp)
jmp    11db <str_extend+0xbb>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1030 <grealloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
je     11c7 <str_extend+0xa7>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
mov    (%rax),%dl
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x28(%rbp)
mov    %dl,(%rax,%rcx,1)
jmp    118e <str_extend+0x6e>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_extend>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <str_length@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1040 <str_length@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <str_length@plt>
add    %r13,%r12
js     1189 <str_extend+0x69>
mov    %rax,%rbx
add    $0x1,%r12
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <grealloc@plt>
mov    (%r14),%cl
test   %cl,%cl
je     1183 <str_extend+0x63>
add    $0x1,%r14
nopl   0x0(%rax)
mov    %cl,(%rax,%rbx,1)
add    $0x1,%rbx
movzbl (%r14),%ecx
add    $0x1,%r14
test   %cl,%cl
jne    1170 <str_extend+0x50>
movb   $0x0,(%rax,%rbx,1)
jmp    118b <str_extend+0x6b>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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