#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 */
scalar_t__ malloc(size_t);
scalar_t__ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *our_strndup(const char *str, size_t n) {
  char *result = NULL;
  char *result_str = NULL;
  size_t len = strlen(str);
  if (n < len)
    len = n;
  if (NULL == (result = (char *)malloc(len + 1)))
    return NULL;
  result[len] = '\0';
  result_str = (char *)memcpy(result, str, len);
  return result_str;
}


x86 assembly (clang, -O0):
<our_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1173 <our_strndup+0x43>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    119f <our_strndup+0x6f>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <our_strndup+0x98>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <memcpy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<our_strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1030 <strlen@plt>
mov    %rax,%rbx
cmp    %r15,%rax
cmova  %r15,%rbx
lea    0x1(%rbx),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     116c <our_strndup+0x3c>
movb   $0x0,(%rax,%rbx,1)
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
jmp    116e <our_strndup+0x3e>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @our_strndup(i8* %0, i64 %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = icmp ugt i64 %3, %1
%5 = select i1 %4, i64 %1, i64 %3
%6 = add i64 %5, 1
%7 = tail call i64 @malloc(i64 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = getelementptr inbounds i8, i8* %8, i64 %5
store i8 0, i8* %11
%12 = tail call i64 @memcpy(i8* %8, i8* %0, i64 %5)
%13 = inttoptr i64 %12 to i8*
br label %14
14: ; preds = %2, %10
%15 = phi i8* [ %13, %10 ], [ null, %2 ]
ret i8* %15
}