#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 */
struct vink_arena {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
char *vink_arena_alloc(struct vink_arena *, size_t);
char *vink_arena_strndup(struct vink_arena *arena, const char *string,
                         size_t length) {
  char *result;
  result = vink_arena_alloc(arena, length + 1);
  if (!result)
    return 0;
  memcpy(result, string, length);
  result[length] = 0;
  return result;
}


x86 assembly (clang, -O0):
<vink_arena_strndup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
call   1030 <vink_arena_alloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1161 <vink_arena_strndup+0x41>
movq   $0x0,-0x8(%rbp)
jmp    1186 <vink_arena_strndup+0x66>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vink_arena_strndup>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
lea    0x1(%rdx),%rsi
call   1030 <vink_arena_alloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     114f <vink_arena_strndup+0x2f>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.vink_arena = type { i32 }
define i8* @vink_arena_strndup(%struct.vink_arena* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca %struct.vink_arena*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store %struct.vink_arena* %0, %struct.vink_arena** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load %struct.vink_arena*, %struct.vink_arena** %5
%10 = load i64, i64* %7
%11 = add i64 %10, 1
%12 = call i8* @vink_arena_alloc(%struct.vink_arena* %9, i64 %11)
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %3
store i8* null, i8** %4
br label %25
16: ; preds = %3
%17 = load i8*, i8** %8
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = call i32 @memcpy(i8* %17, i8* %18, i64 %19)
%21 = load i8*, i8** %8
%22 = load i64, i64* %7
%23 = getelementptr inbounds i8, i8* %21, i64 %22
store i8 0, i8* %23
%24 = load i8*, i8** %8
store i8* %24, i8** %4
br label %25
25: ; preds = %16, %15
%26 = load i8*, i8** %4
ret i8* %26
}

LLVM IR (-O2):
%struct.vink_arena = type { i32 }
define i8* @vink_arena_strndup(%struct.vink_arena* %0, i8* %1, i64 %2) {
%4 = add i64 %2, 1
%5 = tail call i8* @vink_arena_alloc(%struct.vink_arena* %0, i64 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = tail call i32 @memcpy(i8* %5, i8* %1, i64 %2)
%9 = getelementptr inbounds i8, i8* %5, i64 %2
store i8 0, i8* %9
br label %10
10: ; preds = %3, %7
ret i8* %5
}