#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*/ assert(int /*<<< orphan*/);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *_grat_trie_strndup(const char *str, size_t max) {
  char *copy;
  size_t len;
  assert(str != NULL);
  for (len = 0; len < max; len++) {
    continue;
  }
  if (!(copy = (char *)malloc(len + 1))) {
    return NULL;
  }
  memcpy(copy, str, len);
  copy[len] = '\0';
  return copy;
}


x86 assembly (clang, -O0):
<_grat_trie_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    117e <_grat_trie_strndup+0x4e>
jmp    116d <_grat_trie_strndup+0x3d>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    115a <_grat_trie_strndup+0x2a>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11a6 <_grat_trie_strndup+0x76>
movq   $0x0,-0x8(%rbp)
jmp    11cb <_grat_trie_strndup+0x9b>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_grat_trie_strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1030 <assert@plt>
lea    0x1(%r15),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     116f <_grat_trie_strndup+0x3f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
jmp    1171 <_grat_trie_strndup+0x41>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i8* @_grat_trie_strndup(i8* %0, i64 %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = add i64 %1, 1
%7 = tail call i64 @malloc(i64 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i32 @memcpy(i8* %8, i8* %0, i64 %1)
%12 = getelementptr inbounds i8, i8* %8, i64 %1
store i8 0, i8* %12
br label %13
13: ; preds = %2, %10
%14 = phi i8* [ %8, %10 ], [ null, %2 ]
ret i8* %14
}