#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*/ memcpy(char *, char const *, size_t);
scalar_t__ ralloc_array_size(void const *, int, size_t);
size_t strlen(char const *);
char *ralloc_strndup(const void *ctx, const char *str, size_t max) {
  size_t n;
  char *ptr;
  if ((str == NULL))
    return NULL;
  n = strlen(str);
  if (n > max)
    n = max;
  ptr = ((char *)ralloc_array_size(ctx, sizeof(char), n + 1));
  memcpy(ptr, str, n);
  ptr[n] = '\0';
  return ptr;
}


x86 assembly (clang, -O0):
<ralloc_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115c <ralloc_strndup+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    11be <ralloc_strndup+0x8e>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    117f <ralloc_strndup+0x4f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
mov    $0x1,%esi
call   1030 <ralloc_array_size@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ralloc_strndup>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rsi,%rsi
je     1181 <ralloc_strndup+0x51>
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
cmp    %r12,%rax
cmova  %r12,%rbx
lea    0x1(%rbx),%rdx
mov    %r15,%rdi
mov    $0x1,%esi
call   1030 <ralloc_array_size@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
jmp    1184 <ralloc_strndup+0x54>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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