#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 *calloc(scalar_t__, int);
char *realloc(char *, scalar_t__);
int /*<<< orphan*/ strcat(char *, char const *);
scalar_t__ strlen(char const *);
char *catstring(char *buff, const char *s) {
  if (s) {
    if (buff)
      buff = realloc(buff, strlen(buff) + strlen(s) + 1);
    else
      buff = calloc(strlen(s) + 1, 1);
    strcat(buff, s);
  }
  return (buff);
}


x86 assembly (clang, -O0):
<catstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11cc <catstring+0x8c>
cmpq   $0x0,-0x8(%rbp)
je     11a1 <catstring+0x61>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,-0x8(%rbp)
jmp    11bf <catstring+0x7f>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<catstring>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %rsi,%rsi
je     119e <catstring+0x5e>
mov    %rsi,%r14
test   %rbx,%rbx
je     117a <catstring+0x3a>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1050 <realloc@plt>
jmp    1190 <catstring+0x50>
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%rdi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @catstring(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%5 = load i8*, i8** %4
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %28
7: ; preds = %2
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %19
10: ; preds = %7
%11 = load i8*, i8** %3
%12 = load i8*, i8** %3
%13 = call i64 @strlen(i8* %12)
%14 = load i8*, i8** %4
%15 = call i64 @strlen(i8* %14)
%16 = add nsw i64 %13, %15
%17 = add nsw i64 %16, 1
%18 = call i8* @realloc(i8* %11, i64 %17)
store i8* %18, i8** %3
br label %24
19: ; preds = %7
%20 = load i8*, i8** %4
%21 = call i64 @strlen(i8* %20)
%22 = add nsw i64 %21, 1
%23 = call i8* @calloc(i64 %22, i32 1)
store i8* %23, i8** %3
br label %24
24: ; preds = %19, %10
%25 = load i8*, i8** %3
%26 = load i8*, i8** %4
%27 = call i32 @strcat(i8* %25, i8* %26)
br label %28
28: ; preds = %24, %2
%29 = load i8*, i8** %3
ret i8* %29
}

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