#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*/ strndup_append_at(char **, int /*<<< orphan*/, char const *,
                                     size_t);
int /*<<< orphan*/ ta_free(char *);
int /*<<< orphan*/ ta_set_parent(char *, void *);
char *ta_strndup(void *ta_parent, const char *str, size_t n) {
  if (!str)
    return NULL;
  char *new = NULL;
  strndup_append_at(&new, 0, str, n);
  if (!ta_set_parent(new, ta_parent)) {
    ta_free(new);
    new = NULL;
  }
  return new;
}


x86 assembly (clang, -O0):
<ta_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115c <ta_strndup+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    11a6 <ta_strndup+0x76>
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
call   1040 <strndup_append_at@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <ta_set_parent@plt>
cmp    $0x0,%eax
jne    119e <ta_strndup+0x6e>
mov    -0x28(%rbp),%rdi
call   1030 <ta_free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ta_strndup>:
test   %rsi,%rsi
je     1180 <ta_strndup+0x50>
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%rcx
mov    %rsi,%rdx
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1040 <strndup_append_at@plt>
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1050 <ta_set_parent@plt>
mov    %eax,%ecx
mov    0x8(%rsp),%rax
test   %ecx,%ecx
jne    117a <ta_strndup+0x4a>
mov    %rax,%rdi
call   1030 <ta_free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
ret

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

LLVM IR (-O2):
define i8* @ta_strndup(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = icmp eq i8* %1, null
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%8 = call i32 @strndup_append_at(i8** %4, i32 0, i8* %1, i64 %2)
%9 = load i8*, i8** %4
%10 = call i32 @ta_set_parent(i8* %9, i8* %0)
%11 = icmp eq i32 %10, 0
%12 = load i8*, i8** %4
br i1 %11, label %13, label %15
13: ; preds = %6
%14 = call i32 @ta_free(i8* %12)
br label %15
15: ; preds = %6, %13
%16 = phi i8* [ null, %13 ], [ %12, %6 ]
br label %17
17: ; preds = %3, %15
%18 = phi i8* [ %16, %15 ], [ null, %3 ]
ret i8* %18
}