#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(int);
int /*<<< orphan*/ strlcpy(char *, char const *, int);
int strlen(char const *);
char *strlsave(const char *s, size_t n) {
  int l;
  char *save;
  if (s == NULL)
    return NULL;
  l = strlen(s);
  l = (((n) < (l)) ? (n) : (l));
  save = malloc(l + 1);
  strlcpy(save, s, l + 1);
  return save;
}


x86 assembly (clang, -O0):
<strlsave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1158 <strlsave+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11bb <strlsave+0x8b>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jae    1182 <strlsave+0x52>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    118a <strlsave+0x5a>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1050 <strlcpy@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlsave>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     116b <strlsave+0x3b>
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
movslq %eax,%rbx
cmp    %r14,%rbx
cmova  %r14d,%ebx
add    $0x1,%ebx
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1050 <strlcpy@plt>
jmp    116e <strlsave+0x3e>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @strlsave(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %13, label %4
4: ; preds = %2
%5 = tail call i32 @strlen(i8* %0)
%6 = sext i32 %5 to i64
%7 = icmp ugt i64 %6, %1
%8 = select i1 %7, i64 %1, i64 %6
%9 = trunc i64 %8 to i32
%10 = add nsw i32 %9, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = tail call i32 @strlcpy(i8* %11, i8* %0, i32 %10)
br label %13
13: ; preds = %2, %4
%14 = phi i8* [ %11, %4 ], [ null, %2 ]
ret i8* %14
}