#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(unsigned int);
int /*<<< orphan*/ stderr;
unsigned 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);
  }
  unsigned i;
  for (i = 0; i < strlen(s); i++)
    result[i] = s[i];
  for (unsigned 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
jae    11ed <catstr+0xad>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
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
jae    123c <catstr+0xfc>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11f4 <catstr+0xb4>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
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,%r14
mov    %rdi,%r12
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%r15,%rax,1),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11f8 <catstr+0xb8>
mov    %rax,%r15
mov    %r12,%rdi
call   1030 <strlen@plt>
xor    %r13d,%r13d
test   %eax,%eax
je     11ac <catstr+0x6c>
cs nopw 0x0(%rax,%rax,1)
movzbl (%r12,%r13,1),%eax
mov    %al,(%r15,%r13,1)
add    $0x1,%r13
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%r13
jb     1190 <catstr+0x50>
mov    %r14,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
je     11e3 <catstr+0xa3>
mov    %r13d,%r12d
xor    %ebx,%ebx
nopl   (%rax)
lea    (%r12,%rbx,1),%eax
movzbl (%r14,%rbx,1),%ecx
mov    %cl,(%r15,%rax,1)
add    $0x1,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbx
jb     11c0 <catstr+0x80>
add    %ebx,%r13d
mov    %r13d,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
mov    %r12,%rdx
mov    %r14,%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 i32 %9, %11
%13 = add 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 ult i32 %25, %27
br i1 %28, label %29, label %42
29: ; preds = %24
%30 = load i8*, i8** %3
%31 = load i32, i32* %6
%32 = zext 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 = zext 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 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 ult i32 %44, %46
br i1 %47, label %48, label %62
48: ; preds = %43
%49 = load i8*, i8** %4
%50 = load i32, i32* %7
%51 = zext 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 i32 %55, 1
store i32 %56, i32* %6
%57 = zext 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 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 = zext 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 eq i32 %10, 0
br i1 %11, label %18, label %22
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 = %22
%17 = trunc i64 %27 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 eq i32 %20, 0
br i1 %21, label %31, label %35
22: ; preds = %9, %22
%23 = phi i64 [ %27, %22 ], [ 0, %9 ]
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
%26 = getelementptr inbounds i8, i8* %7, i64 %23
store i8 %25, i8* %26
%27 = add nuw nsw i64 %23, 1
%28 = tail call i32 @strlen(i8* %0)
%29 = zext i32 %28 to i64
%30 = icmp ult i64 %27, %29
br i1 %30, label %22, label %16
31: ; preds = %35, %18
%32 = phi i32 [ %19, %18 ], [ %40, %35 ]
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %7, i64 %33
store i8 0, i8* %34
ret i8* %7
35: ; preds = %18, %35
%36 = phi i64 [ %43, %35 ], [ 0, %18 ]
%37 = phi i32 [ %40, %35 ], [ %19, %18 ]
%38 = getelementptr inbounds i8, i8* %1, i64 %36
%39 = load i8, i8* %38
%40 = add i32 %37, 1
%41 = zext i32 %37 to i64
%42 = getelementptr inbounds i8, i8* %7, i64 %41
store i8 %39, i8* %42
%43 = add nuw nsw i64 %36, 1
%44 = tail call i32 @strlen(i8* %1)
%45 = zext i32 %44 to i64
%46 = icmp ult i64 %43, %45
br i1 %46, label %35, label %31
}