#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 */
int /*<<< orphan*/ StrCopyMax(char *, size_t, char const *);
int /*<<< orphan*/ assert(int);
scalar_t__ malloc(size_t);
int strlen(char const *);
char *StrDuplicateMax(const char *source, size_t max) {
  char *target;
  size_t len;
  assert((0 != (source)));
  assert(max > 0);
  len = strlen((source)) + 1;
  if (len > max) {
    len = max;
  }
  target = (char *)malloc(len);
  if (target) {
    StrCopyMax(target, len, source);
  }
  return target;
}


x86 assembly (clang, -O0):
<StrDuplicateMax>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    119d <StrDuplicateMax+0x5d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c6 <StrDuplicateMax+0x86>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1060 <StrCopyMax@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StrDuplicateMax>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1040 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1040 <assert@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
cmp    %r15,%rbx
cmova  %r15,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     119a <StrDuplicateMax+0x5a>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <StrCopyMax@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @StrDuplicateMax(i8* %0, i64 %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = icmp ne i64 %1, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i32 @strlen(i8* %0)
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = icmp ugt i64 %11, %1
%13 = select i1 %12, i64 %1, i64 %11
%14 = tail call i64 @malloc(i64 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %19, label %17
17: ; preds = %2
%18 = tail call i32 @StrCopyMax(i8* %15, i64 %13, i8* %0)
br label %19
19: ; preds = %17, %2
ret i8* %15
}