#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*/ eiki_close(int);
int eiki_execv_fd(char *, char **);
int /*<<< orphan*/ eiki_free(char *);
char *eiki_malloc(scalar_t__);
char *eiki_readln(int);
int /*<<< orphan*/ eiki_strcpy(char *, char const *);
scalar_t__ eiki_strlen(char const *);
char *eiki_cxx_demangle(const char *mangled) {
  char argv_0[] = "c++filt";
  char *argv[] = {argv_0, NULL, NULL};
  char *buf = NULL;
  int fd = -1;
  if (!mangled) {
    return NULL;
  }
  argv[1] = eiki_malloc(eiki_strlen(mangled) + 1);
  if (!argv[1]) {
    goto end;
  }
  eiki_strcpy(argv[1], mangled);
  fd = eiki_execv_fd("/usr/bin/"
                     "c++filt",
                     argv);
  if (fd >= 0) {
    buf = eiki_readln(fd);
    eiki_close(fd);
  }
end:
  eiki_free(argv[1]);
  return buf;
}


x86 assembly (clang, -O0):
<eiki_cxx_demangle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xe86(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c6 <eiki_cxx_demangle+0x56>
movq   $0x0,-0x8(%rbp)
jmp    1243 <eiki_cxx_demangle+0xd3>
mov    -0x10(%rbp),%rdi
call   1030 <eiki_strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1040 <eiki_malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ef <eiki_cxx_demangle+0x7f>
jmp    1232 <eiki_cxx_demangle+0xc2>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <eiki_strcpy@plt>
lea    -0x30(%rbp),%rsi
lea    0xdf9(%rip),%rdi
call   1080 <eiki_execv_fd@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jl     122d <eiki_cxx_demangle+0xbd>
mov    -0x3c(%rbp),%edi
call   1090 <eiki_readln@plt>
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%edi
call   1050 <eiki_close@plt>
jmp    1232 <eiki_cxx_demangle+0xc2>
mov    -0x28(%rbp),%rdi
call   1070 <eiki_free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eiki_cxx_demangle>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
movabs $0x746c69662b2b63,%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x10(%rsp)
movq   $0x0,0x20(%rsp)
test   %rdi,%rdi
je     11ed <eiki_cxx_demangle+0x7d>
mov    %rdi,%rbx
call   1030 <eiki_strlen@plt>
lea    0x1(%rax),%rdi
call   1040 <eiki_malloc@plt>
mov    %rax,0x18(%rsp)
test   %rax,%rax
je     11f1 <eiki_cxx_demangle+0x81>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1060 <eiki_strcpy@plt>
lea    0xe36(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1080 <eiki_execv_fd@plt>
test   %eax,%eax
js     11f1 <eiki_cxx_demangle+0x81>
mov    %eax,%ebp
mov    %eax,%edi
call   1090 <eiki_readln@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <eiki_close@plt>
jmp    11f3 <eiki_cxx_demangle+0x83>
xor    %ebx,%ebx
jmp    11fd <eiki_cxx_demangle+0x8d>
xor    %ebx,%ebx
mov    0x18(%rsp),%rdi
call   1070 <eiki_free@plt>
mov    %rbx,%rax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.eiki_cxx_demangle.argv_0 = [8 x i8] c"c++filt\00"
@.str = [17 x i8] c"/usr/bin/c++filt\00"
define i8* @eiki_cxx_demangle(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [8 x i8]
%5 = alloca [3 x i8*]
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = bitcast [8 x i8]* %4 to i8*
%9 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%10 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
store i8* %10, i8** %9
%11 = getelementptr inbounds i8*, i8** %9, i64 1
store i8* null, i8** %11
%12 = getelementptr inbounds i8*, i8** %11, i64 1
store i8* null, i8** %12
store i8* null, i8** %6
store i32 -1, i32* %7
%13 = load i8*, i8** %3
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %1
store i8* null, i8** %2
br label %46
16: ; preds = %1
%17 = load i8*, i8** %3
%18 = call i64 @eiki_strlen(i8* %17)
%19 = add nsw i64 %18, 1
%20 = call i8* @eiki_malloc(i64 %19)
%21 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 1
store i8* %20, i8** %21
%22 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 1
%23 = load i8*, i8** %22
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %16
br label %41
26: ; preds = %16
%27 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 1
%28 = load i8*, i8** %27
%29 = load i8*, i8** %3
%30 = call i32 @eiki_strcpy(i8* %28, i8* %29)
%31 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 0
%32 = call i32 @eiki_execv_fd(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp sge i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %26
%36 = load i32, i32* %7
%37 = call i8* @eiki_readln(i32 %36)
store i8* %37, i8** %6
%38 = load i32, i32* %7
%39 = call i32 @eiki_close(i32 %38)
br label %40
40: ; preds = %35, %26
br label %41
41: ; preds = %40, %25
%42 = getelementptr inbounds [3 x i8*], [3 x i8*]* %5, i64 0, i64 1
%43 = load i8*, i8** %42
%44 = call i32 @eiki_free(i8* %43)
%45 = load i8*, i8** %6
store i8* %45, i8** %2
br label %46
46: ; preds = %41, %15
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@.str = [17 x i8] c"/usr/bin/c++filt\00"
define i8* @eiki_cxx_demangle(i8* %0) {
%2 = alloca i64
%3 = alloca [3 x i8*]
%4 = bitcast i64* %2 to i8*
store i64 32770297239907171, i64* %2
%5 = bitcast [3 x i8*]* %3 to i8*
%6 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%7 = bitcast [3 x i8*]* %3 to i64**
store i64* %2, i64** %7
%8 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 1
%9 = icmp eq i8* %0, null
%10 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 2
%11 = bitcast i8** %10 to i64*
store i64 0, i64* %11
br i1 %9, label %28, label %12
12: ; preds = %1
%13 = call i64 @eiki_strlen(i8* %0)
%14 = add nsw i64 %13, 1
%15 = call i8* @eiki_malloc(i64 %14)
store i8* %15, i8** %8
%16 = icmp eq i8* %15, null
br i1 %16, label %24, label %17
17: ; preds = %12
%18 = call i32 @eiki_strcpy(i8* %15, i8* %0)
%19 = call i32 @eiki_execv_fd(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %6)
%20 = icmp sgt i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i8* @eiki_readln(i32 %19)
%23 = call i32 @eiki_close(i32 %19)
br label %24
24: ; preds = %17, %21, %12
%25 = phi i8* [ %22, %21 ], [ null, %17 ], [ null, %12 ]
%26 = load i8*, i8** %8
%27 = call i32 @eiki_free(i8* %26)
br label %28
28: ; preds = %1, %24
%29 = phi i8* [ %25, %24 ], [ null, %1 ]
ret i8* %29
}