#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int NS_MAXCDNAME;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *);
size_t strlen(char *);
size_t read_formated_dname(unsigned char *buffer, char *dname) {
  char *d = (char *)malloc(sizeof(char) * NS_MAXCDNAME + 1);
  if (d == NULL)
    do {
      perror("malloc failed");
      exit(EXIT_FAILURE);
    } while (0);
  size_t i;
  for (i = 0; i < NS_MAXCDNAME && buffer[i] != '\0'; ++i)
    d[i] = (char)buffer[i];
  snprintf(dname, strlen(d) + 1, "%s", d);
  free(d);
  return strlen(dname) + 1;
}


x86 assembly (clang, -O0):
<read_formated_dname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e69(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b7 <read_formated_dname+0x57>
jmp    119d <read_formated_dname+0x3d>
lea    0xe5c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rcx
mov    0x2e16(%rip),%rax
movslq (%rax),%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x21(%rbp)
jae    11f0 <read_formated_dname+0x90>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1200 <read_formated_dname+0xa0>
jmp    1227 <read_formated_dname+0xc7>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11bf <read_formated_dname+0x5f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
mov    -0x18(%rbp),%rcx
lea    0xdc0(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_formated_dname>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e6b(%rip),%r12
mov    (%r12),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11f7 <read_formated_dname+0x97>
mov    %rax,%rbx
cmpl   $0x0,(%r12)
je     11b9 <read_formated_dname+0x59>
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%r15,%rax,1),%ecx
test   %cl,%cl
je     11b9 <read_formated_dname+0x59>
mov    %cl,(%rbx,%rax,1)
add    $0x1,%rax
movslq (%r12),%rcx
cmp    %rcx,%rax
jb     11a0 <read_formated_dname+0x40>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rsi
lea    0xe42(%rip),%rdx
mov    %r14,%rdi
mov    %rbx,%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
add    $0x1,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe02(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@NS_MAXCDNAME = global i32 0
@.str = [14 x i8] c"malloc failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i64 @read_formated_dname(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i32, i32* @NS_MAXCDNAME
%8 = sext i32 %7 to i64
%9 = mul i64 1, %8
%10 = add i64 %9, 1
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %22
16: ; preds = %2
br label %17
17: ; preds = %16
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; No predecessors!
br label %22
22: ; preds = %21, %2
store i64 0, i64* %6
br label %23
23: ; preds = %45, %22
%24 = load i64, i64* %6
%25 = load i32, i32* @NS_MAXCDNAME
%26 = sext i32 %25 to i64
%27 = icmp ult i64 %24, %26
br i1 %27, label %28, label %35
28: ; preds = %23
%29 = load i8*, i8** %3
%30 = load i64, i64* %6
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = icmp ne i32 %33, 0
br label %35
35: ; preds = %28, %23
%36 = phi i1 [ false, %23 ], [ %34, %28 ]
br i1 %36, label %37, label %48
37: ; preds = %35
%38 = load i8*, i8** %3
%39 = load i64, i64* %6
%40 = getelementptr inbounds i8, i8* %38, i64 %39
%41 = load i8, i8* %40
%42 = load i8*, i8** %5
%43 = load i64, i64* %6
%44 = getelementptr inbounds i8, i8* %42, i64 %43
store i8 %41, i8* %44
br label %45
45: ; preds = %37
%46 = load i64, i64* %6
%47 = add i64 %46, 1
store i64 %47, i64* %6
br label %23
48: ; preds = %35
%49 = load i8*, i8** %4
%50 = load i8*, i8** %5
%51 = call i64 @strlen(i8* %50)
%52 = add i64 %51, 1
%53 = load i8*, i8** %5
%54 = call i32 @snprintf(i8* %49, i64 %52, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %53)
%55 = load i8*, i8** %5
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %4
%58 = call i64 @strlen(i8* %57)
%59 = add i64 %58, 1
ret i64 %59
}

LLVM IR (-O2):
@NS_MAXCDNAME = global i32 0
@.str = [14 x i8] c"malloc failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i64 @read_formated_dname(i8* nocapture %0, i8* %1) {
%3 = load i32, i32* @NS_MAXCDNAME
%4 = add i32 %3, 1
%5 = tail call i64 @malloc(i32 %4)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @NS_MAXCDNAME
%10 = icmp eq i32 %9, 0
br i1 %10, label %26, label %15
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %8, %20
%16 = phi i64 [ %22, %20 ], [ 0, %8 ]
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %15
%21 = getelementptr inbounds i8, i8* %6, i64 %16
store i8 %18, i8* %21
%22 = add nuw i64 %16, 1
%23 = load i32, i32* @NS_MAXCDNAME
%24 = sext i32 %23 to i64
%25 = icmp ult i64 %22, %24
br i1 %25, label %15, label %26
26: ; preds = %15, %20, %8
%27 = tail call i64 @strlen(i8* %6)
%28 = add i64 %27, 1
%29 = tail call i32 @snprintf(i8* %1, i64 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%30 = tail call i32 @free(i8* %6)
%31 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%32 = add i64 %31, 1
ret i64 %32
}