#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(size_t);
char *strndup(const char *s, size_t n) {
  char *d;
  size_t i;
  if (!n)
    return NULL;
  d = malloc(n + 1);
  for (i = 0; i < n; i++)
    d[i] = s[i];
  d[i] = '\0';
  return d;
}


x86 assembly (clang, -O0):
<strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1138 <strndup+0x28>
movq   $0x0,-0x8(%rbp)
jmp    119a <strndup+0x8a>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1030 <malloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1186 <strndup+0x76>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1151 <strndup+0x41>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strndup>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     1151 <strndup+0x31>
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x1(%rsi),%rdi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
movb   $0x0,(%r14,%rbx,1)
jmp    1154 <strndup+0x34>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define noalias i8* @strndup(i8* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %8, label %4
4: ; preds = %2
%5 = add i64 %1, 1
%6 = tail call i8* @malloc(i64 %5)
%7 = getelementptr inbounds i8, i8* %6, i64 %1
store i8 0, i8* %7
br label %8
8: ; preds = %2, %4
%9 = phi i8* [ %6, %4 ], [ null, %2 ]
ret i8* %9
}