#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(int, unsigned int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, unsigned long);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
char *strnsave(char *s, int n) {
  char *cp;
  if (n < 0) {
    n = (int)strlen(s);
  }
  do {
    if ((cp = calloc(sizeof(char), (unsigned)(n + 1))) == NULL) {
      (void)fprintf(stderr, "%s: can't allocate %lu bytes\n", "strnsave",
                    (unsigned long)(n + 1 * sizeof(char)));
      return NULL;
    }
  } while (0);
  (void)memcpy(cp, s, (size_t)n);
  cp[n] = 0x0;
  return cp;
}


x86 assembly (clang, -O0):
<strnsave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1165 <strnsave+0x25>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x14(%rbp)
jmp    116a <strnsave+0x2a>
mov    -0x14(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b9 <strnsave+0x79>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
movslq -0x14(%rbp),%rcx
add    $0x1,%rcx
lea    0xe60(%rip),%rsi
lea    0xe77(%rip),%rdx
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e3 <strnsave+0xa3>
jmp    11be <strnsave+0x7e>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x14(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%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):
<strnsave>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %rdi,%r14
test   %esi,%esi
jns    1159 <strnsave+0x19>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
lea    0x1(%rbx),%esi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1186 <strnsave+0x46>
movslq %ebx,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
jmp    11a9 <strnsave+0x69>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
movslq %ebx,%rcx
add    $0x1,%rcx
lea    0xe63(%rip),%rsi
lea    0xe7a(%rip),%rdx
call   1050 <fprintf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"%s: can't allocate %lu bytes\0A\00"
@.str.1 = [9 x i8] c"strnsave\00"
define i8* @strnsave(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
%12 = trunc i64 %11 to i32
store i32 %12, i32* %5
br label %13
13: ; preds = %9, %2
br label %14
14: ; preds = %13
%15 = load i32, i32* %5
%16 = add nsw i32 %15, 1
%17 = call i8* @calloc(i32 1, i32 %16)
store i8* %17, i8** %6
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %25
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = add i64 %22, 1
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64 %23)
store i8* null, i8** %3
br label %37
25: ; preds = %14
br label %26
26: ; preds = %25
%27 = load i8*, i8** %6
%28 = load i8*, i8** %4
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = call i32 @memcpy(i8* %27, i8* %28, i64 %30)
%32 = load i8*, i8** %6
%33 = load i32, i32* %5
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %6
store i8* %36, i8** %3
br label %37
37: ; preds = %26, %19
%38 = load i8*, i8** %3
ret i8* %38
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"%s: can't allocate %lu bytes\0A\00"
@.str.1 = [9 x i8] c"strnsave\00"
define i8* @strnsave(i8* %0, i32 %1) {
%3 = icmp slt i32 %1, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = trunc i64 %5 to i32
br label %7
7: ; preds = %2, %4
%8 = phi i32 [ %6, %4 ], [ %1, %2 ]
%9 = add nsw i32 %8, 1
%10 = tail call i8* @calloc(i32 1, i32 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %17
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = sext i32 %8 to i64
%15 = add nsw i64 %14, 1
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64 %15)
br label %21
17: ; preds = %7
%18 = sext i32 %8 to i64
%19 = tail call i32 @memcpy(i8* %10, i8* %0, i64 %18)
%20 = getelementptr inbounds i8, i8* %10, i64 %18
store i8 0, i8* %20
br label %21
21: ; preds = %17, %12
ret i8* %10
}