#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 *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strnlen(char const *, size_t);
char *im_strndup(const char *src, size_t maxlen) {
  size_t len;
  char *dup;
  len = strnlen(src, maxlen);
  if (!len)
    return NULL;
  len++;
  dup = malloc(len);
  if (!dup)
    return NULL;
  memcpy(dup, src, len);
  return dup;
}


x86 assembly (clang, -O0):
<im_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strnlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1169 <im_strndup+0x39>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <im_strndup+0x83>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119a <im_strndup+0x6a>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <im_strndup+0x83>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<im_strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <strnlen@plt>
test   %rax,%rax
je     1169 <im_strndup+0x39>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1169 <im_strndup+0x39>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
jmp    116c <im_strndup+0x3c>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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