#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 *kl_malloc(int);
int /*<<< orphan*/ strlcpy(char *, char const *, int);
char *kl_strndup(char const *src, int max) {
  int len = 0;
  while (src[len] && len < max) {
    len++;
  }
  char *dest = kl_malloc(len + 1);
  strlcpy(dest, src, len + 1);
  return dest;
}


x86 assembly (clang, -O0):
<kl_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
je     115c <kl_strndup+0x3c>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
setl   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    116c <kl_strndup+0x4c>
jmp    117a <kl_strndup+0x5a>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <kl_strndup+0x16>
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1030 <kl_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
add    $0x1,%edx
call   1040 <strlcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kl_strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
movslq %esi,%rax
xor    %ecx,%ecx
nopl   (%rax)
lea    0x1(%rcx),%rbx
cmpb   $0x0,(%r14,%rcx,1)
je     1143 <kl_strndup+0x23>
cmp    %rax,%rcx
mov    %rbx,%rcx
jl     1130 <kl_strndup+0x10>
mov    %ebx,%edi
call   1030 <kl_malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <strlcpy@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @kl_strndup(i8* %0, i32 %1) {
%3 = sext i32 %1 to i64
br label %4
4: ; preds = %4, %2
%5 = phi i64 [ %11, %4 ], [ 0, %2 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
%8 = icmp ne i8 %7, 0
%9 = icmp slt i64 %5, %3
%10 = select i1 %8, i1 %9, i1 false
%11 = add nuw nsw i64 %5, 1
br i1 %10, label %4, label %12
12: ; preds = %4
%13 = trunc i64 %5 to i32
%14 = add nuw nsw i32 %13, 1
%15 = tail call i8* @kl_malloc(i32 %14)
%16 = tail call i32 @strlcpy(i8* %15, i8* %0, i32 %14)
ret i8* %15
}