#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 */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int strlen(char const *);
char *scat(char *str, const char *str1, bool freestr) {
  if (!str || !str1) {
    return NULL;
  }
  char *buffer =
      (char *)malloc(sizeof(char) * (strlen(str) + strlen(str1) + 1));
  memcpy(buffer, str, strlen(str));
  memcpy(buffer + strlen(str), str1, strlen(str1) + 1);
  if (freestr) {
    free(str);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<scat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1169 <scat+0x29>
cmpq   $0x0,-0x18(%rbp)
jne    1176 <scat+0x36>
movq   $0x0,-0x8(%rbp)
jmp    122d <scat+0xed>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1050 <memcpy@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1225 <scat+0xe5>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r12d,%r12d
test   %rdi,%rdi
je     11c1 <scat+0x81>
mov    %rsi,%r15
test   %rsi,%rsi
je     11c1 <scat+0x81>
mov    %edx,%r14d
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    (%rax,%rbp,1),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbp
add    %r12,%rbp
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1050 <memcpy@plt>
test   %r14d,%r14d
je     11c1 <scat+0x81>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @scat(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %11, %3
store i8* null, i8** %4
br label %49
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = call i32 @strlen(i8* %16)
%18 = load i8*, i8** %6
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %17, %19
%21 = add nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = mul i64 1, %22
%24 = trunc i64 %23 to i32
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = load i8*, i8** %5
%29 = load i8*, i8** %5
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @memcpy(i8* %27, i8* %28, i32 %30)
%32 = load i8*, i8** %8
%33 = load i8*, i8** %5
%34 = call i32 @strlen(i8* %33)
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %32, i64 %35
%37 = load i8*, i8** %6
%38 = load i8*, i8** %6
%39 = call i32 @strlen(i8* %38)
%40 = add nsw i32 %39, 1
%41 = call i32 @memcpy(i8* %36, i8* %37, i32 %40)
%42 = load i32, i32* %7
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %15
%45 = load i8*, i8** %5
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %44, %15
%48 = load i8*, i8** %8
store i8* %48, i8** %4
br label %49
49: ; preds = %47, %14
%50 = load i8*, i8** %4
ret i8* %50
}

LLVM IR (-O2):
define i8* @scat(i8* %0, i8* %1, i32 %2) {
%4 = icmp ne i8* %0, null
%5 = icmp ne i8* %1, null
%6 = and i1 %4, %5
br i1 %6, label %7, label %25
7: ; preds = %3
%8 = tail call i32 @strlen(i8* %0)
%9 = tail call i32 @strlen(i8* %1)
%10 = add i32 %8, 1
%11 = add i32 %10, %9
%12 = tail call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
%14 = tail call i32 @strlen(i8* %0)
%15 = tail call i32 @memcpy(i8* %13, i8* %0, i32 %14)
%16 = tail call i32 @strlen(i8* %0)
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %13, i64 %17
%19 = tail call i32 @strlen(i8* %1)
%20 = add nsw i32 %19, 1
%21 = tail call i32 @memcpy(i8* %18, i8* %1, i32 %20)
%22 = icmp eq i32 %2, 0
br i1 %22, label %25, label %23
23: ; preds = %7
%24 = tail call i32 @free(i8* %0)
br label %25
25: ; preds = %7, %23, %3
%26 = phi i8* [ null, %3 ], [ %13, %23 ], [ %13, %7 ]
ret i8* %26
}