#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 */
char *_tc_malloc(char const *, int, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *_tc_strndup(const char *file, int line, const char *s, size_t n) {
  char *pc = NULL;
  if (s != NULL) {
    pc = _tc_malloc(file, line, n + 1);
    if (pc != NULL) {
      memcpy(pc, s, n);
      pc[n] = '\0';
    }
  }
  return pc;
}


x86 assembly (clang, -O0):
<_tc_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     118f <_tc_strndup+0x6f>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
call   1030 <_tc_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     118a <_tc_strndup+0x6a>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    118f <_tc_strndup+0x6f>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_tc_strndup>:
push   %r15
push   %r14
push   %rbx
test   %rdx,%rdx
je     1156 <_tc_strndup+0x36>
mov    %rcx,%r14
mov    %rdx,%r15
lea    0x1(%rcx),%rdx
call   1030 <_tc_malloc@plt>
test   %rax,%rax
je     1156 <_tc_strndup+0x36>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r14,1)
jmp    1158 <_tc_strndup+0x38>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @_tc_strndup(i8* %0, i32 %1, i8* %2, i64 %3) {
%5 = icmp eq i8* %2, null
br i1 %5, label %13, label %6
6: ; preds = %4
%7 = add i64 %3, 1
%8 = tail call i8* @_tc_malloc(i8* %0, i32 %1, i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = tail call i32 @memcpy(i8* %8, i8* %2, i64 %3)
%12 = getelementptr inbounds i8, i8* %8, i64 %3
store i8 0, i8* %12
br label %13
13: ; preds = %6, %10, %4
%14 = phi i8* [ %8, %10 ], [ null, %6 ], [ null, %4 ]
ret i8* %14
}