#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 *);
char *strdup_printf(char *, char *, char *);
char *strndup(char const *, int);
char *append_cdata(char *t, const char *n, int length) {
  char *r, *k;
  if (!length)
    return t;
  k = strndup(n, length);
  if (t) {
    r = strdup_printf("%s%s", t, k);
    {
      if (k) {
        free(k);
        k = NULL;
      }
    };
    {
      if (t) {
        free(t);
        t = NULL;
      }
    };
  } else
    r = k;
  return r;
}


x86 assembly (clang, -O0):
<append_cdata>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    115a <append_cdata+0x2a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11da <append_cdata+0xaa>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <strndup@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ca <append_cdata+0x9a>
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xe7c(%rip),%rdi
call   1040 <strdup_printf@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11a9 <append_cdata+0x79>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c5 <append_cdata+0x95>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
jmp    11d2 <append_cdata+0xa2>
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_cdata>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
test   %edx,%edx
je     1180 <append_cdata+0x50>
mov    %rsi,%rdi
mov    %edx,%esi
call   1050 <strndup@plt>
mov    %rax,%r14
test   %rbx,%rbx
je     117d <append_cdata+0x4d>
lea    0xeab(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <strdup_printf@plt>
mov    %rax,%r15
test   %r14,%r14
je     1170 <append_cdata+0x40>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rbx
jmp    1180 <append_cdata+0x50>
mov    %r14,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s%s\00"
define i8* @append_cdata(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %3
%13 = load i8*, i8** %5
store i8* %13, i8** %4
br label %40
14: ; preds = %3
%15 = load i8*, i8** %6
%16 = load i32, i32* %7
%17 = call i8* @strndup(i8* %15, i32 %16)
store i8* %17, i8** %9
%18 = load i8*, i8** %5
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %36
20: ; preds = %14
%21 = load i8*, i8** %5
%22 = load i8*, i8** %9
%23 = call i8* @strdup_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %22)
store i8* %23, i8** %8
%24 = load i8*, i8** %9
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i8*, i8** %9
%28 = call i32 @free(i8* %27)
store i8* null, i8** %9
br label %29
29: ; preds = %26, %20
%30 = load i8*, i8** %5
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8*, i8** %5
%34 = call i32 @free(i8* %33)
store i8* null, i8** %5
br label %35
35: ; preds = %32, %29
br label %38
36: ; preds = %14
%37 = load i8*, i8** %9
store i8* %37, i8** %8
br label %38
38: ; preds = %36, %35
%39 = load i8*, i8** %8
store i8* %39, i8** %4
br label %40
40: ; preds = %38, %12
%41 = load i8*, i8** %4
ret i8* %41
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s%s\00"
define i8* @append_cdata(i8* %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %15, label %5
5: ; preds = %3
%6 = tail call i8* @strndup(i8* %1, i32 %2)
%7 = icmp eq i8* %0, null
br i1 %7, label %15, label %8
8: ; preds = %5
%9 = tail call i8* @strdup_printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %6)
%10 = icmp eq i8* %6, null
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @free(i8* %6)
br label %13
13: ; preds = %8, %11
%14 = tail call i32 @free(i8* %0)
br label %15
15: ; preds = %13, %5, %3
%16 = phi i8* [ %0, %3 ], [ %9, %13 ], [ %6, %5 ]
ret i8* %16
}