#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 *calloc(size_t, int);
int /*<<< orphan*/ puts(char *);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *ls_strdupn(const char *str, size_t len) {
  char *new_str = 0;
  if (!str) {
    puts("ls_strdupn: str undef");
    return 0;
  }
  if (len == 0) {
    puts("ls_strdupn: provided len was 0, recalculating");
    len = strlen(str);
  }
  new_str = calloc(len + 1, sizeof(char));
  if (!new_str) {
    puts("ls_strdupn: call to calloc failed");
    return 0;
  }
  strncpy(new_str, str, len);
  new_str[len] = '\0';
  return new_str;
}


x86 assembly (clang, -O0):
<ls_strdupn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117c <ls_strdupn+0x3c>
lea    0xe96(%rip),%rdi
call   1040 <puts@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ff <ls_strdupn+0xbf>
cmpq   $0x0,-0x18(%rbp)
jne    11a0 <ls_strdupn+0x60>
lea    0xe88(%rip),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11da <ls_strdupn+0x9a>
lea    0xe7c(%rip),%rdi
call   1040 <puts@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ff <ls_strdupn+0xbf>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls_strdupn>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     1197 <ls_strdupn+0x57>
mov    %rsi,%r15
mov    %rdi,%r14
test   %rsi,%rsi
jne    116c <ls_strdupn+0x2c>
lea    0xeba(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rax,%r15
lea    0x1(%r15),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     11a0 <ls_strdupn+0x60>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
movb   $0x0,(%rbx,%r15,1)
jmp    11ae <ls_strdupn+0x6e>
lea    0xe62(%rip),%rdi
jmp    11a7 <ls_strdupn+0x67>
lea    0xe9d(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [22 x i8] c"ls_strdupn: str undef\00"
@.str.1 = [46 x i8] c"ls_strdupn: provided len was 0, recalculating\00"
@.str.2 = [34 x i8] c"ls_strdupn: call to calloc failed\00"
define i8* @ls_strdupn(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* null, i8** %6
%7 = load i8*, i8** %4
%8 = icmp ne i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %35
11: ; preds = %2
%12 = load i64, i64* %5
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = call i32 @puts(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%16 = load i8*, i8** %4
%17 = call i64 @strlen(i8* %16)
store i64 %17, i64* %5
br label %18
18: ; preds = %14, %11
%19 = load i64, i64* %5
%20 = add i64 %19, 1
%21 = call i8* @calloc(i64 %20, i32 1)
store i8* %21, i8** %6
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %26, label %24
24: ; preds = %18
%25 = call i32 @puts(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %3
br label %35
26: ; preds = %18
%27 = load i8*, i8** %6
%28 = load i8*, i8** %4
%29 = load i64, i64* %5
%30 = call i32 @strncpy(i8* %27, i8* %28, i64 %29)
%31 = load i8*, i8** %6
%32 = load i64, i64* %5
%33 = getelementptr inbounds i8, i8* %31, i64 %32
store i8 0, i8* %33
%34 = load i8*, i8** %6
store i8* %34, i8** %3
br label %35
35: ; preds = %26, %24, %9
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
@.str = [22 x i8] c"ls_strdupn: str undef\00"
@.str.1 = [46 x i8] c"ls_strdupn: provided len was 0, recalculating\00"
@.str.2 = [34 x i8] c"ls_strdupn: call to calloc failed\00"
define i8* @ls_strdupn(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %21
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %8, label %11
8: ; preds = %6
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i64 @strlen(i8* %0)
br label %11
11: ; preds = %8, %6
%12 = phi i64 [ %10, %8 ], [ %1, %6 ]
%13 = add i64 %12, 1
%14 = tail call i8* @calloc(i64 %13, i32 1)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %21
18: ; preds = %11
%19 = tail call i32 @strncpy(i8* %14, i8* %0, i64 %12)
%20 = getelementptr inbounds i8, i8* %14, i64 %12
store i8 0, i8* %20
br label %21
21: ; preds = %18, %16, %4
%22 = phi i8* [ %14, %18 ], [ null, %16 ], [ null, %4 ]
ret i8* %22
}