#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*/ uint8_t;

/* Variables and functions */
int MAXDNAME;
int dn_expand(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
              char *, int);
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
char *expand_cdname(char **cpp, char *msg) {
  char name[MAXDNAME];
  int i;
  char *ptr;
  if ((i = dn_expand((uint8_t *)msg, (uint8_t *)msg + 512, (uint8_t *)*cpp,
                     name, MAXDNAME)) < 0)
    return (NULL);
  if (name[0] == '\0') {
    name[0] = '.';
    name[1] = '\0';
  }
  *cpp += i;
  if ((ptr = (char *)malloc(strlen(name) + 1)) == NULL) {
    *cpp -= i;
    return (NULL);
  }
  strcpy(ptr, name);
  return (ptr);
}


x86 assembly (clang, -O0):
<expand_cdname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x800,%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    0x2e47(%rip),%rax
mov    (%rax),%r8d
call   1060 <dn_expand@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11c1 <expand_cdname+0x81>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    125a <expand_cdname+0x11a>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11dc <expand_cdname+0x9c>
mov    -0x48(%rbp),%rax
movb   $0x2e,(%rax)
movb   $0x0,0x1(%rax)
mov    -0x48(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
movslq %edx,%rdx
add    %rdx,%rcx
mov    %rcx,(%rax)
call   1040 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    123e <expand_cdname+0xfe>
mov    -0x2c(%rbp),%edx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
movslq %edx,%rsi
xor    %edx,%edx
sub    %rsi,%rdx
add    %rdx,%rcx
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    125a <expand_cdname+0x11a>
mov    -0x48(%rbp),%rsi
mov    -0x38(%rbp),%rdi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_cdname>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rax
mov    %rdi,%r14
mov    0x2e88(%rip),%rcx
mov    (%rcx),%r8d
mov    %rsp,%rbx
mov    %r8,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
add    $0x800,%rsi
mov    (%rdi),%rdx
mov    %rax,%rdi
mov    %rbx,%rcx
call   1060 <dn_expand@plt>
test   %eax,%eax
js     11c1 <expand_cdname+0x81>
cmpb   $0x0,(%rbx)
jne    1192 <expand_cdname+0x52>
movw   $0x2e,(%rbx)
mov    %eax,%r12d
add    %r12,(%r14)
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11be <expand_cdname+0x7e>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    11c4 <expand_cdname+0x84>
sub    %r12,(%r14)
xor    %r15d,%r15d
mov    %r15,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MAXDNAME = global i32 0
define i8* @expand_cdname(i8** %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i8** %0, i8*** %4
store i8* %1, i8** %5
%11 = load i32, i32* @MAXDNAME
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
%15 = load i8*, i8** %5
%16 = bitcast i8* %15 to i32*
%17 = load i8*, i8** %5
%18 = bitcast i8* %17 to i32*
%19 = getelementptr inbounds i32, i32* %18, i64 512
%20 = load i8**, i8*** %4
%21 = load i8*, i8** %20
%22 = bitcast i8* %21 to i32*
%23 = load i32, i32* @MAXDNAME
%24 = call i32 @dn_expand(i32* %16, i32* %19, i32* %22, i8* %14, i32 %23)
store i32 %24, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %2
store i8* null, i8** %3
store i32 1, i32* %10
br label %57
27: ; preds = %2
%28 = getelementptr inbounds i8, i8* %14, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = getelementptr inbounds i8, i8* %14, i64 0
store i8 46, i8* %33
%34 = getelementptr inbounds i8, i8* %14, i64 1
store i8 0, i8* %34
br label %35
35: ; preds = %32, %27
%36 = load i32, i32* %8
%37 = load i8**, i8*** %4
%38 = load i8*, i8** %37
%39 = sext i32 %36 to i64
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8* %40, i8** %37
%41 = call i64 @strlen(i8* %14)
%42 = add nsw i64 %41, 1
%43 = call i64 @malloc(i64 %42)
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %9
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %53
46: ; preds = %35
%47 = load i32, i32* %8
%48 = load i8**, i8*** %4
%49 = load i8*, i8** %48
%50 = sext i32 %47 to i64
%51 = sub i64 0, %50
%52 = getelementptr inbounds i8, i8* %49, i64 %51
store i8* %52, i8** %48
store i8* null, i8** %3
store i32 1, i32* %10
br label %57
53: ; preds = %35
%54 = load i8*, i8** %9
%55 = call i32 @strcpy(i8* %54, i8* %14)
%56 = load i8*, i8** %9
store i8* %56, i8** %3
store i32 1, i32* %10
br label %57
57: ; preds = %53, %46, %26
%58 = load i8*, i8** %6
%59 = load i8*, i8** %3
ret i8* %59
}

LLVM IR (-O2):
@MAXDNAME = global i32 0
define i8* @expand_cdname(i8** nocapture %0, i8* %1) {
%3 = load i32, i32* @MAXDNAME
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = bitcast i8* %1 to i32*
%7 = getelementptr inbounds i8, i8* %1, i64 2048
%8 = bitcast i8* %7 to i32*
%9 = bitcast i8** %0 to i32**
%10 = load i32*, i32** %9
%11 = call i32 @dn_expand(i32* %6, i32* %8, i32* %10, i8* %5, i32 %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %33, label %13
13: ; preds = %2
%14 = load i8, i8* %5
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
store i8 46, i8* %5
%17 = getelementptr inbounds i8, i8* %5, i64 1
store i8 0, i8* %17
br label %18
18: ; preds = %16, %13
%19 = load i8*, i8** %0
%20 = zext i32 %11 to i64
%21 = getelementptr inbounds i8, i8* %19, i64 %20
store i8* %21, i8** %0
%22 = call i64 @strlen(i8* %5)
%23 = add nsw i64 %22, 1
%24 = call i64 @malloc(i64 %23)
%25 = inttoptr i64 %24 to i8*
%26 = icmp eq i64 %24, 0
br i1 %26, label %27, label %31
27: ; preds = %18
%28 = load i8*, i8** %0
%29 = sub nsw i64 0, %20
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8* %30, i8** %0
br label %33
31: ; preds = %18
%32 = call i32 @strcpy(i8* %25, i8* %5)
br label %33
33: ; preds = %2, %31, %27
%34 = phi i8* [ null, %27 ], [ %25, %31 ], [ null, %2 ]
ret i8* %34
}