#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*/ _DIAGASSERT(int /*<<< orphan*/);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *strndup(const char *str, size_t n) {
  size_t len;
  char *copy;
  _DIAGASSERT(str != NULL);
  for (len = 0; len < n && str[len]; len++)
    continue;
  if (!(copy = malloc(len + 1)))
    return (NULL);
  memcpy(copy, str, len);
  copy[len] = '\0';
  return (copy);
}


x86 assembly (clang, -O0):
<strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <_DIAGASSERT@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
cmp    -0x18(%rbp),%rcx
mov    %al,-0x29(%rbp)
jae    1182 <strndup+0x52>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1192 <strndup+0x62>
jmp    11a8 <strndup+0x78>
jmp    1197 <strndup+0x67>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    115a <strndup+0x2a>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11d0 <strndup+0xa0>
movq   $0x0,-0x8(%rbp)
jmp    11f5 <strndup+0xc5>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%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
mov    %rsi,%r15
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1040 <_DIAGASSERT@plt>
xor    %ebx,%ebx
test   %r15,%r15
je     1163 <strndup+0x33>
cmpb   $0x0,(%r14,%rbx,1)
je     1163 <strndup+0x33>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <strndup+0x20>
mov    %r15,%rbx
lea    0x1(%rbx),%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1187 <strndup+0x57>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
mov    %r15,%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 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
%10 = zext i1 %9 to i32
%11 = call i32 @_DIAGASSERT(i32 %10)
store i64 0, i64* %6
br label %12
12: ; preds = %26, %2
%13 = load i64, i64* %6
%14 = load i64, i64* %5
%15 = icmp ult i64 %13, %14
br i1 %15, label %16, label %23
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = load i64, i64* %6
%19 = getelementptr inbounds i8, i8* %17, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br label %23
23: ; preds = %16, %12
%24 = phi i1 [ false, %12 ], [ %22, %16 ]
br i1 %24, label %25, label %29
25: ; preds = %23
br label %26
26: ; preds = %25
%27 = load i64, i64* %6
%28 = add i64 %27, 1
store i64 %28, i64* %6
br label %12
29: ; preds = %23
%30 = load i64, i64* %6
%31 = add i64 %30, 1
%32 = call i8* @malloc(i64 %31)
store i8* %32, i8** %7
%33 = icmp ne i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %29
store i8* null, i8** %3
br label %44
35: ; preds = %29
%36 = load i8*, i8** %7
%37 = load i8*, i8** %4
%38 = load i64, i64* %6
%39 = call i32 @memcpy(i8* %36, i8* %37, i64 %38)
%40 = load i8*, i8** %7
%41 = load i64, i64* %6
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8 0, i8* %42
%43 = load i8*, i8** %7
store i8* %43, i8** %3
br label %44
44: ; preds = %35, %34
%45 = load i8*, i8** %3
ret i8* %45
}

LLVM IR (-O2):
define i8* @strndup(i8* %0, i64 %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @_DIAGASSERT(i32 %4)
%6 = icmp eq i64 %1, 0
br i1 %6, label %15, label %7
7: ; preds = %2, %12
%8 = phi i64 [ %13, %12 ], [ 0, %2 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = add nuw i64 %8, 1
%14 = icmp eq i64 %13, %1
br i1 %14, label %15, label %7
15: ; preds = %7, %12, %2
%16 = phi i64 [ 0, %2 ], [ %1, %12 ], [ %8, %7 ]
%17 = add i64 %16, 1
%18 = tail call i8* @malloc(i64 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %15
%21 = tail call i32 @memcpy(i8* %18, i8* %0, i64 %16)
%22 = getelementptr inbounds i8, i8* %18, i64 %16
store i8 0, i8* %22
br label %23
23: ; preds = %15, %20
ret i8* %18
}