#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *,
                           char const *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int strlen(char const *);
char *catstr(const char *s, const char *t) {
  char *const result = malloc(strlen(s) + strlen(t) + 1);
  if (result == NULL) {
    fprintf(stderr, "Cannot allocate memory for catstr of %s and %s\n", s, t);
    exit(1);
  }
  int i;
  for (i = 0; i < strlen(s); i++)
    result[i] = s[i];
  for (int j = 0; j < strlen(t); j++)
    result[i++] = t[j];
  result[i] = '\0';
  return result;
}


x86 assembly (clang, -O0):
<catstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x24(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a8 <catstr+0x68>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe67(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
cmp    %ecx,%eax
jge    11ef <catstr+0xaf>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11af <catstr+0x6f>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
jge    1240 <catstr+0x100>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11f6 <catstr+0xb6>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<catstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %eax,%r14d
mov    %r15,%rdi
call   1030 <strlen@plt>
lea    (%r14,%rax,1),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1204 <catstr+0xc4>
mov    %rax,%r14
mov    %r12,%rdi
call   1030 <strlen@plt>
xor    %r13d,%r13d
test   %eax,%eax
jle    11ac <catstr+0x6c>
cs nopw 0x0(%rax,%rax,1)
movzbl (%r12,%r13,1),%eax
mov    %al,(%r14,%r13,1)
add    $0x1,%r13
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%r13
jl     1190 <catstr+0x50>
mov    %r15,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11ef <catstr+0xaf>
mov    %r13d,%r13d
mov    %r14,%r12
add    %r13,%r12
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl (%r15,%rbx,1),%eax
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     11d0 <catstr+0x90>
add    %ebx,%r13d
mov    %r13d,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    %r12,%rdx
mov    %r15,%rcx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [48 x i8] c"Cannot allocate memory for catstr of %s and %s\0A\00"
define i8* @catstr(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %9, %11
%13 = add nsw i32 %12, 1
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %3
%20 = load i8*, i8** %4
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
store i32 0, i32* %6
br label %24
24: ; preds = %39, %23
%25 = load i32, i32* %6
%26 = load i8*, i8** %3
%27 = call i32 @strlen(i8* %26)
%28 = icmp slt i32 %25, %27
br i1 %28, label %29, label %42
29: ; preds = %24
%30 = load i8*, i8** %3
%31 = load i32, i32* %6
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = load i8*, i8** %5
%36 = load i32, i32* %6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 %34, i8* %38
br label %39
39: ; preds = %29
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %24
42: ; preds = %24
store i32 0, i32* %7
br label %43
43: ; preds = %59, %42
%44 = load i32, i32* %7
%45 = load i8*, i8** %4
%46 = call i32 @strlen(i8* %45)
%47 = icmp slt i32 %44, %46
br i1 %47, label %48, label %62
48: ; preds = %43
%49 = load i8*, i8** %4
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i8, i8* %52
%54 = load i8*, i8** %5
%55 = load i32, i32* %6
%56 = add nsw i32 %55, 1
store i32 %56, i32* %6
%57 = sext i32 %55 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
store i8 %53, i8* %58
br label %59
59: ; preds = %48
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
br label %43
62: ; preds = %43
%63 = load i8*, i8** %5
%64 = load i32, i32* %6
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
store i8 0, i8* %66
%67 = load i8*, i8** %5
ret i8* %67
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [48 x i8] c"Cannot allocate memory for catstr of %s and %s\0A\00"
define noalias i8* @catstr(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = add i32 %3, 1
%6 = add i32 %5, %4
%7 = tail call i8* @malloc(i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = tail call i32 @strlen(i8* %0)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %24, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %24
%17 = trunc i64 %29 to i32
br label %18
18: ; preds = %16, %9
%19 = phi i32 [ 0, %9 ], [ %17, %16 ]
%20 = tail call i32 @strlen(i8* %1)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %22, label %35
22: ; preds = %18
%23 = zext i32 %19 to i64
br label %39
24: ; preds = %9, %24
%25 = phi i64 [ %29, %24 ], [ 0, %9 ]
%26 = getelementptr inbounds i8, i8* %0, i64 %25
%27 = load i8, i8* %26
%28 = getelementptr inbounds i8, i8* %7, i64 %25
store i8 %27, i8* %28
%29 = add nuw nsw i64 %25, 1
%30 = tail call i32 @strlen(i8* %0)
%31 = sext i32 %30 to i64
%32 = icmp slt i64 %29, %31
br i1 %32, label %24, label %16
33: ; preds = %39
%34 = trunc i64 %44 to i32
br label %35
35: ; preds = %33, %18
%36 = phi i32 [ %19, %18 ], [ %34, %33 ]
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %7, i64 %37
store i8 0, i8* %38
ret i8* %7
39: ; preds = %22, %39
%40 = phi i64 [ %23, %22 ], [ %44, %39 ]
%41 = phi i64 [ 0, %22 ], [ %46, %39 ]
%42 = getelementptr inbounds i8, i8* %1, i64 %41
%43 = load i8, i8* %42
%44 = add nuw nsw i64 %40, 1
%45 = getelementptr inbounds i8, i8* %7, i64 %40
store i8 %43, i8* %45
%46 = add nuw nsw i64 %41, 1
%47 = tail call i32 @strlen(i8* %1)
%48 = sext i32 %47 to i64
%49 = icmp slt i64 %46, %48
br i1 %49, label %39, label %33
}