#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 */
char *__cxa_demangle(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/,
                     int *);
scalar_t__ strncmp(char *, char const *, int);
int /*<<< orphan*/ warn(char *, char const *, int);
char *sr_demangle_symbol(const char *sym) {
  if (0 != strncmp("_Z", sym, 2))
    return NULL;
  int status;
  char *demangled = __cxa_demangle(sym, NULL, 0, &status);
  if (status != 0 && status != -2)
    warn("__cxa_demangle failed on symbol '%s' with status %d", sym, status);
  return demangled;
}


x86 assembly (clang, -O0):
<sr_demangle_symbol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xeb9(%rip),%rdi
mov    $0x2,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     116c <sr_demangle_symbol+0x3c>
movq   $0x0,-0x8(%rbp)
jmp    11b2 <sr_demangle_symbol+0x82>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
lea    -0x14(%rbp),%rcx
call   1050 <__cxa_demangle@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11aa <sr_demangle_symbol+0x7a>
cmpl   $0xfffffffe,-0x14(%rbp)
je     11aa <sr_demangle_symbol+0x7a>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe5e(%rip),%rdi
call   1040 <warn@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sr_demangle_symbol>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xec2(%rip),%rdi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     115e <sr_demangle_symbol+0x2e>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0x4(%rsp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <__cxa_demangle@plt>
mov    %rax,%r14
mov    0x4(%rsp),%edx
cmp    $0xfffffffe,%edx
je     1153 <sr_demangle_symbol+0x23>
test   %edx,%edx
je     1153 <sr_demangle_symbol+0x23>
lea    0xe7d(%rip),%rdi
mov    %rbx,%rsi
call   1040 <warn@plt>
jmp    1153 <sr_demangle_symbol+0x23>

LLVM IR (-O0):
@.str = [3 x i8] c"_Z\00"
@.str.1 = [52 x i8] c"__cxa_demangle failed on symbol '%s' with status %d\00"
define i8* @sr_demangle_symbol(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i64 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %6, i32 2)
%8 = icmp ne i64 0, %7
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %24
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i8* @__cxa_demangle(i8* %11, i32* null, i32 0, i32* %4)
store i8* %12, i8** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %10
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, -2
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i8*, i8** %3
%20 = load i32, i32* %4
%21 = call i32 @warn(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %20)
br label %22
22: ; preds = %18, %15, %10
%23 = load i8*, i8** %5
store i8* %23, i8** %2
br label %24
24: ; preds = %22, %9
%25 = load i8*, i8** %2
ret i8* %25
}

LLVM IR (-O2):
@.str = [3 x i8] c"_Z\00"
@.str.1 = [52 x i8] c"__cxa_demangle failed on symbol '%s' with status %d\00"
define i8* @sr_demangle_symbol(i8* %0) {
%2 = alloca i32
%3 = tail call i64 @strncmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0, i32 2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %12
5: ; preds = %1
%6 = bitcast i32* %2 to i8*
%7 = call i8* @__cxa_demangle(i8* %0, i32* null, i32 0, i32* %2)
%8 = load i32, i32* %2
switch i32 %8, label %9 [
i32 -2, label %11
i32 0, label %11
]
9: ; preds = %5
%10 = call i32 @warn(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %8)
br label %11
11: ; preds = %5, %5, %9
br label %12
12: ; preds = %1, %11
%13 = phi i8* [ %7, %11 ], [ null, %1 ]
ret i8* %13
}