#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 *fcitx_utils_malloc(size_t);
char *fcitx_utils_realloc(char *, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
__attribute__((visibility("default"))) char *
fcitx_utils_set_str_with_len(char *res, const char *str, size_t len) {
  if (res) {
    res = fcitx_utils_realloc(res, len + 1);
  } else {
    res = fcitx_utils_malloc(len + 1);
  }
  memcpy(res, str, len);
  res[len] = '\0';
  return res;
}


x86 assembly (clang, -O0):
<fcitx_utils_set_str_with_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1169 <fcitx_utils_set_str_with_len+0x39>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
call   1050 <fcitx_utils_realloc@plt>
mov    %rax,-0x8(%rbp)
jmp    117a <fcitx_utils_set_str_with_len+0x4a>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1040 <fcitx_utils_malloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fcitx_utils_set_str_with_len>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
lea    0x1(%rdx),%rsi
test   %rdi,%rdi
je     114b <fcitx_utils_set_str_with_len+0x1b>
call   1050 <fcitx_utils_realloc@plt>
jmp    1153 <fcitx_utils_set_str_with_len+0x23>
mov    %rsi,%rdi
call   1040 <fcitx_utils_malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1030 <memcpy@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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