#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 *malloc(int);
int /*<<< orphan*/ nasm_error(int, char *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *nasm_strndup(const char *s, size_t len) {
  char *p;
  int size = len + 1;
  p = malloc(size);
  if (!p)
    nasm_error(0x00000002 | 0x00000010, "out of memory");
  strncpy(p, s, len);
  p[len] = '\0';
  return p;
}


x86 assembly (clang, -O0):
<nasm_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1173 <nasm_strndup+0x43>
mov    $0x12,%edi
lea    0xe92(%rip),%rsi
call   1050 <nasm_error@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nasm_strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x1(%rsi),%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    115c <nasm_strndup+0x2c>
lea    0xeae(%rip),%rsi
mov    $0x12,%edi
call   1050 <nasm_error@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"out of memory\00"
define i8* @nasm_strndup(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = add i64 %7, 1
%9 = trunc i64 %8 to i32
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = call i8* @malloc(i32 %10)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = call i32 @nasm_error(i32 18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i8*, i8** %5
%18 = load i8*, i8** %3
%19 = load i64, i64* %4
%20 = call i32 @strncpy(i8* %17, i8* %18, i64 %19)
%21 = load i8*, i8** %5
%22 = load i64, i64* %4
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8 0, i8* %23
%24 = load i8*, i8** %5
ret i8* %24
}

LLVM IR (-O2):
@.str = [14 x i8] c"out of memory\00"
define i8* @nasm_strndup(i8* %0, i64 %1) {
%3 = trunc i64 %1 to i32
%4 = add i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @nasm_error(i32 18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = tail call i32 @strncpy(i8* %5, i8* %0, i64 %1)
%11 = getelementptr inbounds i8, i8* %5, i64 %1
store i8 0, i8* %11
ret i8* %5
}