#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 */
typedef int /*<<< orphan*/ UChar;

/* Variables and functions */
int /*<<< orphan*/ *calloc(int, int);
int u_strlen(int /*<<< orphan*/ *);
UChar *u_strdup(UChar *ustr) {
  int i, len = u_strlen(ustr);
  UChar *dup = calloc(len + 1, sizeof(UChar));
  if (dup != NULL)
    for (i = 0; i < len; i++)
      dup[i] = ustr[i];
  return dup;
}


x86 assembly (clang, -O0):
<u_strdup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <u_strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
mov    $0x4,%esi
call   1030 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1193 <u_strdup+0x73>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    118e <u_strdup+0x6e>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
mov    -0x18(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    115e <u_strdup+0x3e>
jmp    1193 <u_strdup+0x73>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u_strdup>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1050 <u_strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
mov    $0x4,%esi
call   1030 <calloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     116d <u_strdup+0x3d>
test   %r15d,%r15d
jle    116d <u_strdup+0x3d>
mov    %r15d,%edx
shl    $0x2,%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <memcpy@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32* @u_strdup(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32* %0, i32** %2
%6 = load i32*, i32** %2
%7 = call i32 @u_strlen(i32* %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = add nsw i32 %8, 1
%10 = call i32* @calloc(i32 %9, i32 4)
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %32
13: ; preds = %1
store i32 0, i32* %3
br label %14
14: ; preds = %28, %13
%15 = load i32, i32* %3
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %31
18: ; preds = %14
%19 = load i32*, i32** %2
%20 = load i32, i32* %3
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i32, i32* %19, i64 %21
%23 = load i32, i32* %22
%24 = load i32*, i32** %5
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32, i32* %24, i64 %26
store i32 %23, i32* %27
br label %28
28: ; preds = %18
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %14
31: ; preds = %14
br label %32
32: ; preds = %31, %1
%33 = load i32*, i32** %5
ret i32* %33
}

LLVM IR (-O2):
define noalias i32* @u_strdup(i32* %0) {
%2 = tail call i32 @u_strlen(i32* %0)
%3 = add nsw i32 %2, 1
%4 = tail call i32* @calloc(i32 %3, i32 4)
%5 = icmp ne i32* %4, null
%6 = icmp sgt i32 %2, 0
%7 = select i1 %5, i1 %6, i1 false
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %0 to i8*
%11 = zext i32 %2 to i64
%12 = shl nuw nsw i64 %11, 2
br label %13
13: ; preds = %8, %1
ret i32* %4
}