#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 *md_malloc(size_t, char const *, int);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strnlen(char const *, size_t);
char __attribute__((visibility("default"))) *
    md_strndup(const char *ptr, size_t n, const char *fname, int line) {
  size_t len = strnlen(ptr, n);
  char *str = md_malloc(len + 1, fname, line);
  if (str) {
    memcpy(str, ptr, len);
    str[len] = '\0';
  }
  return str;
}


x86 assembly (clang, -O0):
<md_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strnlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <md_malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1198 <md_strndup+0x68>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<md_strndup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %rdi,%r14
call   1030 <strnlen@plt>
mov    %rax,%r15
lea    0x1(%rax),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <md_malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1170 <md_strndup+0x40>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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