#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 *dumb_malloc(size_t);
int /*<<< orphan*/ ehnlc_strncpy(char *, char const *, size_t);
size_t ehnlc_strnlen(char const *, size_t);
char *ehnlc_strndup(const char *s, size_t n) {
  size_t len;
  char *dup;
  len = s ? ehnlc_strnlen(s, n) : 0;
  dup = dumb_malloc(len + 1);
  if (!dup) {
    return NULL;
  }
  dup[len] = '\0';
  ehnlc_strncpy(dup, s, n);
  return dup;
}


x86 assembly (clang, -O0):
<ehnlc_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1161 <ehnlc_strndup+0x31>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <ehnlc_strnlen@plt>
mov    %rax,-0x30(%rbp)
jmp    116c <ehnlc_strndup+0x3c>
xor    %eax,%eax
mov    %rax,-0x30(%rbp)
jmp    116c <ehnlc_strndup+0x3c>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <dumb_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119d <ehnlc_strndup+0x6d>
movq   $0x0,-0x8(%rbp)
jmp    11c2 <ehnlc_strndup+0x92>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <ehnlc_strncpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ehnlc_strndup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdi,%rdi
je     1153 <ehnlc_strndup+0x23>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <ehnlc_strnlen@plt>
mov    %rax,%r12
jmp    1156 <ehnlc_strndup+0x26>
xor    %r12d,%r12d
lea    0x1(%r12),%rdi
call   1050 <dumb_malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     117b <ehnlc_strndup+0x4b>
movb   $0x0,(%rbx,%r12,1)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <ehnlc_strncpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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