#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*/ die_out_of_memory();
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *xmalloc(size_t);
char *xstrndup(const char *str, size_t n) {
  char *p;
  if (!str)
    return NULL;
  p = xmalloc(n + 1);
  if (!p)
    die_out_of_memory();
  strncpy(p, str, n);
  p[n] = '\0';
  return p;
}


x86 assembly (clang, -O0):
<xstrndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1158 <xstrndup+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11a0 <xstrndup+0x70>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1040 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117b <xstrndup+0x4b>
mov    $0x0,%al
call   1050 <die_out_of_memory@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xstrndup>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     116d <xstrndup+0x3d>
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x1(%rsi),%rdi
call   1040 <xmalloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1158 <xstrndup+0x28>
xor    %eax,%eax
call   1050 <die_out_of_memory@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r14,1)
jmp    116f <xstrndup+0x3f>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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