#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 *calloc_or_die(char *, int, int);
int jutf8_strlen(unsigned short, unsigned char *);
char *jutf8_strncpy(char *, unsigned short, unsigned char *, int);
char *jutf8_strdup(unsigned short length, unsigned char *bytes) {
  int cslen, size, ridx = 0;
  char *ret;
  cslen = jutf8_strlen(length, bytes);
  if (cslen < 0) {
    return (char *)NULL;
  }
  ret = calloc_or_die("jutf8_strdup", cslen + 1, sizeof(char));
  return jutf8_strncpy(ret, length, bytes, cslen + 1);
}


x86 assembly (clang, -O0):
<jutf8_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %di,%ax
mov    %ax,-0xa(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0xa(%rbp),%ax
mov    -0x18(%rbp),%rsi
movzwl %ax,%edi
call   1030 <jutf8_strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1174 <jutf8_strdup+0x44>
movq   $0x0,-0x8(%rbp)
jmp    11ad <jutf8_strdup+0x7d>
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
lea    0xe7f(%rip),%rdi
mov    $0x1,%edx
call   1040 <calloc_or_die@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0xa(%rbp),%ax
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
movzwl %ax,%esi
call   1050 <jutf8_strncpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jutf8_strdup>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
call   1030 <jutf8_strlen@plt>
test   %eax,%eax
js     116e <jutf8_strdup+0x3e>
mov    %eax,%ebx
add    $0x1,%ebx
lea    0xeb2(%rip),%rdi
mov    %ebx,%esi
mov    $0x1,%edx
call   1040 <calloc_or_die@plt>
movzwl %bp,%esi
mov    %rax,%rdi
mov    %r14,%rdx
mov    %ebx,%ecx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <jutf8_strncpy@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"jutf8_strdup\00"
define i8* @jutf8_strdup(i16 zeroext %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i16
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i16 %0, i16* %4
store i8* %1, i8** %5
store i32 0, i32* %8
%10 = load i16, i16* %4
%11 = load i8*, i8** %5
%12 = call i32 @jutf8_strlen(i16 zeroext %10, i8* %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i8* null, i8** %3
br label %26
16: ; preds = %2
%17 = load i32, i32* %6
%18 = add nsw i32 %17, 1
%19 = call i8* @calloc_or_die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %18, i32 1)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = load i16, i16* %4
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = add nsw i32 %23, 1
%25 = call i8* @jutf8_strncpy(i8* %20, i16 zeroext %21, i8* %22, i32 %24)
store i8* %25, i8** %3
br label %26
26: ; preds = %16, %15
%27 = load i8*, i8** %3
ret i8* %27
}

LLVM IR (-O2):
@.str = [13 x i8] c"jutf8_strdup\00"
define i8* @jutf8_strdup(i16 zeroext %0, i8* %1) {
%3 = tail call i32 @jutf8_strlen(i16 zeroext %0, i8* %1)
%4 = icmp slt i32 %3, 0
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = add nuw nsw i32 %3, 1
%7 = tail call i8* @calloc_or_die(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6, i32 1)
%8 = tail call i8* @jutf8_strncpy(i8* %7, i16 zeroext %0, i8* %1, i32 %6)
br label %9
9: ; preds = %2, %5
%10 = phi i8* [ %8, %5 ], [ null, %2 ]
ret i8* %10
}