#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*/ strcpy(char *, char const *);
int strlen(char const *);
char *xp_asprintf_start(const char *format) {
  char *ret;
  char *p;
  ret = (char *)malloc(strlen(format) + 1 + ((sizeof(size_t) * 2)));
  if (ret == NULL)
    return (NULL);
  strcpy(ret + sizeof(size_t) * 2, format);
  *(size_t *)(ret + sizeof(size_t)) = strlen(format);
  for (p = ret + sizeof(size_t) * 2; *p; p++) {
    if (*p == '%') {
      if (*(p + 1) == '%')
        p++;
      else
        break;
    }
  }
  if (!*p)
    *(size_t *)ret = 0;
  else
    *(size_t *)ret = p - ret;
  return (ret);
}


x86 assembly (clang, -O0):
<xp_asprintf_start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
add    $0x10,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <xp_asprintf_start+0x41>
movq   $0x0,-0x8(%rbp)
jmp    1238 <xp_asprintf_start+0x108>
mov    -0x18(%rbp),%rdi
add    $0x10,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1201 <xp_asprintf_start+0xd1>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    11eb <xp_asprintf_start+0xbb>
mov    -0x20(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x25,%eax
jne    11e1 <xp_asprintf_start+0xb1>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11e6 <xp_asprintf_start+0xb6>
jmp    1201 <xp_asprintf_start+0xd1>
jmp    11eb <xp_asprintf_start+0xbb>
jmp    11f0 <xp_asprintf_start+0xc0>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11a2 <xp_asprintf_start+0x72>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
jne    121e <xp_asprintf_start+0xee>
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
jmp    1230 <xp_asprintf_start+0x100>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xp_asprintf_start>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <strlen@plt>
lea    0x11(%rax),%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     118b <xp_asprintf_start+0x5b>
mov    %rax,%r14
lea    0x10(%rax),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,0x8(%r14)
jmp    1178 <xp_asprintf_start+0x48>
nopl   0x0(%rax)
test   %al,%al
je     1190 <xp_asprintf_start+0x60>
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x25,%al
jne    1170 <xp_asprintf_start+0x40>
cmpb   $0x25,0x1(%rbx)
jne    1194 <xp_asprintf_start+0x64>
add    $0x1,%rbx
jmp    1174 <xp_asprintf_start+0x44>
xor    %r14d,%r14d
jmp    119a <xp_asprintf_start+0x6a>
xor    %ebx,%ebx
jmp    1197 <xp_asprintf_start+0x67>
sub    %r14,%rbx
mov    %rbx,(%r14)
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @xp_asprintf_start(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = add nsw i32 %7, 1
%9 = sext i32 %8 to i64
%10 = add i64 %9, 16
%11 = trunc i64 %10 to i32
%12 = call i64 @malloc(i32 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %1
store i8* null, i8** %2
br label %71
17: ; preds = %1
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 16
%20 = load i8*, i8** %3
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = load i8*, i8** %3
%23 = call i32 @strlen(i8* %22)
%24 = sext i32 %23 to i64
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i64 8
%27 = bitcast i8* %26 to i64*
store i64 %24, i64* %27
%28 = load i8*, i8** %4
%29 = getelementptr inbounds i8, i8* %28, i64 16
store i8* %29, i8** %5
br label %30
30: ; preds = %51, %17
%31 = load i8*, i8** %5
%32 = load i8, i8* %31
%33 = icmp ne i8 %32, 0
br i1 %33, label %34, label %54
34: ; preds = %30
%35 = load i8*, i8** %5
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 37
br i1 %38, label %39, label %50
39: ; preds = %34
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i64 1
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 37
br i1 %44, label %45, label %48
45: ; preds = %39
%46 = load i8*, i8** %5
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %5
br label %49
48: ; preds = %39
br label %54
49: ; preds = %45
br label %50
50: ; preds = %49, %34
br label %51
51: ; preds = %50
%52 = load i8*, i8** %5
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %5
br label %30
54: ; preds = %48, %30
%55 = load i8*, i8** %5
%56 = load i8, i8* %55
%57 = icmp ne i8 %56, 0
br i1 %57, label %61, label %58
58: ; preds = %54
%59 = load i8*, i8** %4
%60 = bitcast i8* %59 to i64*
store i64 0, i64* %60
br label %69
61: ; preds = %54
%62 = load i8*, i8** %5
%63 = load i8*, i8** %4
%64 = ptrtoint i8* %62 to i64
%65 = ptrtoint i8* %63 to i64
%66 = sub i64 %64, %65
%67 = load i8*, i8** %4
%68 = bitcast i8* %67 to i64*
store i64 %66, i64* %68
br label %69
69: ; preds = %61, %58
%70 = load i8*, i8** %4
store i8* %70, i8** %2
br label %71
71: ; preds = %69, %16
%72 = load i8*, i8** %2
ret i8* %72
}

LLVM IR (-O2):
define i8* @xp_asprintf_start(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add i32 %2, 17
%4 = tail call i64 @malloc(i32 %3)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %30, label %7
7: ; preds = %1
%8 = getelementptr inbounds i8, i8* %5, i64 16
%9 = tail call i32 @strcpy(i8* %8, i8* %0)
%10 = tail call i32 @strlen(i8* %0)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %5, i64 8
%13 = bitcast i8* %12 to i64*
store i64 %11, i64* %13
br label %14
14: ; preds = %21, %7
%15 = phi i8* [ %8, %7 ], [ %23, %21 ]
%16 = load i8, i8* %15
switch i8 %16, label %21 [
i8 0, label %27
i8 37, label %17
]
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %15, i64 1
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 37
br i1 %20, label %21, label %24
21: ; preds = %17, %14
%22 = phi i8* [ %15, %14 ], [ %18, %17 ]
%23 = getelementptr inbounds i8, i8* %22, i64 1
br label %14
24: ; preds = %17
%25 = ptrtoint i8* %15 to i64
%26 = sub i64 %25, %4
br label %27
27: ; preds = %14, %24
%28 = phi i64 [ %26, %24 ], [ 0, %14 ]
%29 = inttoptr i64 %4 to i64*
store i64 %28, i64* %29
br label %30
30: ; preds = %27, %1
%31 = phi i8* [ null, %1 ], [ %5, %27 ]
ret i8* %31
}