#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 */
unsigned char *malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, char const *, int);
int strlen(char const *);
unsigned char *duplicate_string_from_char_sequence(const char *character,
                                                   int *_length) {
  if (character == NULL || _length == NULL)
    return NULL;
  int length = *_length;
  if (length <= 0) {
    length = strlen(character);
    if (length == 0)
      return NULL;
  }
  unsigned char *new_copy = malloc(length + 1);
  if (new_copy == 0)
    return NULL;
  memcpy(new_copy, character, length);
  new_copy[length] = 0;
  *_length = length;
  return new_copy;
}


x86 assembly (clang, -O0):
<duplicate_string_from_char_sequence>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1156 <duplicate_string_from_char_sequence+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <duplicate_string_from_char_sequence+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11f2 <duplicate_string_from_char_sequence+0xc2>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     119e <duplicate_string_from_char_sequence+0x6e>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1199 <duplicate_string_from_char_sequence+0x69>
movq   $0x0,-0x8(%rbp)
jmp    11f2 <duplicate_string_from_char_sequence+0xc2>
jmp    119e <duplicate_string_from_char_sequence+0x6e>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c5 <duplicate_string_from_char_sequence+0x95>
movq   $0x0,-0x8(%rbp)
jmp    11f2 <duplicate_string_from_char_sequence+0xc2>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<duplicate_string_from_char_sequence>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %ebx,%ebx
test   %rdi,%rdi
je     118f <duplicate_string_from_char_sequence+0x5f>
mov    %rsi,%r14
test   %rsi,%rsi
je     118f <duplicate_string_from_char_sequence+0x5f>
mov    %rdi,%r15
mov    (%r14),%r12d
test   %r12d,%r12d
jg     1161 <duplicate_string_from_char_sequence+0x31>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
test   %eax,%eax
je     118d <duplicate_string_from_char_sequence+0x5d>
lea    0x1(%r12),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     118d <duplicate_string_from_char_sequence+0x5d>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1040 <memcpy@plt>
movslq %r12d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %r12d,(%r14)
jmp    118f <duplicate_string_from_char_sequence+0x5f>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @duplicate_string_from_char_sequence(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i32*, i32** %5
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %10, %2
store i8* null, i8** %3
br label %45
14: ; preds = %10
%15 = load i32*, i32** %5
%16 = load i32, i32* %15
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp sle i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = call i32 @strlen(i8* %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
store i8* null, i8** %3
br label %45
25: ; preds = %19
br label %26
26: ; preds = %25, %14
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %7
%30 = load i8*, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %26
store i8* null, i8** %3
br label %45
33: ; preds = %26
%34 = load i8*, i8** %7
%35 = load i8*, i8** %4
%36 = load i32, i32* %6
%37 = call i32 @memcpy(i8* %34, i8* %35, i32 %36)
%38 = load i8*, i8** %7
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
store i8 0, i8* %41
%42 = load i32, i32* %6
%43 = load i32*, i32** %5
store i32 %42, i32* %43
%44 = load i8*, i8** %7
store i8* %44, i8** %3
br label %45
45: ; preds = %33, %32, %24, %13
%46 = load i8*, i8** %3
ret i8* %46
}

LLVM IR (-O2):
define i8* @duplicate_string_from_char_sequence(i8* %0, i32* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i32* %1, null
%5 = or i1 %3, %4
br i1 %5, label %21, label %6
6: ; preds = %2
%7 = load i32, i32* %1
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 @strlen(i8* %0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %9, %6
%13 = phi i32 [ %10, %9 ], [ %7, %6 ]
%14 = add nsw i32 %13, 1
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %12
%18 = tail call i32 @memcpy(i8* %15, i8* %0, i32 %13)
%19 = sext i32 %13 to i64
%20 = getelementptr inbounds i8, i8* %15, i64 %19
store i8 0, i8* %20
store i32 %13, i32* %1
br label %21
21: ; preds = %9, %12, %17, %2
%22 = phi i8* [ null, %2 ], [ null, %9 ], [ %15, %17 ], [ null, %12 ]
ret i8* %22
}