#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *expand_charstring(char **cpp, char *msg) {
  int i;
  char *ptr;
  i = **cpp;
  (*cpp)++;
  if ((ptr = (char *)malloc(i + 1)) == NULL) {
    (*cpp)--;
    return (NULL);
  }
  strncpy(ptr, *cpp, i);
  ptr[i] = '\0';
  *cpp += i;
  return (ptr);
}


x86 assembly (clang, -O0):
<expand_charstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    117f <expand_charstring+0x5f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11b9 <expand_charstring+0x99>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
movslq %edx,%rdx
add    %rdx,%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_charstring>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    (%rdi),%rax
movsbl (%rax),%r15d
add    $0x1,%rax
mov    %rax,(%rdi)
lea    0x1(%r15),%edi
call   1040 <malloc@plt>
mov    (%r14),%rsi
test   %rax,%rax
je     1161 <expand_charstring+0x41>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15d,%edx
call   1030 <strncpy@plt>
movslq %r15d,%rsi
movb   $0x0,(%rbx,%rsi,1)
add    (%r14),%rsi
jmp    1167 <expand_charstring+0x47>
add    $0xffffffffffffffff,%rsi
xor    %ebx,%ebx
mov    %rsi,(%r14)
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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