#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__ isalnum(char const);
char *realloc(char *, scalar_t__);
scalar_t__ strlen(char const *);
char *safe(const char *name) {
  static char *safe = NULL;
  int i;
  safe = realloc(safe, strlen(name) + 1);
  for (i = 0; name[i]; i++)
    if (isalnum(name[i]))
      safe[i] = name[i];
    else
      safe[i] = '_';
  safe[i] = 0;
  return (safe);
}


x86 assembly (clang, -O0):
<safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2efd(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x18(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
call   1050 <realloc@plt>
mov    %rax,0x2ed9(%rip)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11db <safe+0xab>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%rax
je     11b9 <safe+0x89>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    0x2e93(%rip),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11c8 <safe+0x98>
mov    0x2e80(%rip),%rax
movslq -0xc(%rbp),%rcx
movb   $0x5f,(%rax,%rcx,1)
jmp    11cd <safe+0x9d>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116e <safe+0x3e>
mov    0x2e5e(%rip),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e4f(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
mov    0x2f01(%rip),%r14
call   1030 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %r14,%rdi
call   1050 <realloc@plt>
mov    %rax,0x2ee9(%rip)
mov    (%r15),%cl
test   %cl,%cl
je     11a2 <safe+0x72>
xor    %ebx,%ebx
jmp    118d <safe+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%r15,%rbx,1),%ecx
mov    0x2ec4(%rip),%rax
mov    %cl,(%rax,%rbx,1)
movzbl 0x1(%r15,%rbx,1),%ecx
add    $0x1,%rbx
test   %cl,%cl
je     119e <safe+0x6e>
movsbl %cl,%edi
call   1040 <isalnum@plt>
test   %rax,%rax
jne    1170 <safe+0x40>
mov    $0x5f,%cl
jmp    1175 <safe+0x45>
mov    %ebx,%ecx
jmp    11a4 <safe+0x74>
xor    %ecx,%ecx
movb   $0x0,(%rax,%rcx,1)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@safe.safe = internal global i8* null
define i8* @safe(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** @safe.safe
%5 = load i8*, i8** %2
%6 = call i64 @strlen(i8* %5)
%7 = add nsw i64 %6, 1
%8 = call i8* @realloc(i8* %4, i64 %7)
store i8* %8, i8** @safe.safe
store i32 0, i32* %3
br label %9
9: ; preds = %40, %1
%10 = load i8*, i8** %2
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %16, label %43
16: ; preds = %9
%17 = load i8*, i8** %2
%18 = load i32, i32* %3
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = call i64 @isalnum(i8 signext %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %16
%25 = load i8*, i8** %2
%26 = load i32, i32* %3
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = load i8*, i8** @safe.safe
%31 = load i32, i32* %3
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
store i8 %29, i8* %33
br label %39
34: ; preds = %16
%35 = load i8*, i8** @safe.safe
%36 = load i32, i32* %3
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 95, i8* %38
br label %39
39: ; preds = %34, %24
br label %40
40: ; preds = %39
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 1
store i32 %42, i32* %3
br label %9
43: ; preds = %9
%44 = load i8*, i8** @safe.safe
%45 = load i32, i32* %3
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i8*, i8** @safe.safe
ret i8* %48
}

LLVM IR (-O2):
@safe.safe = internal unnamed_addr global i8* null
define i8* @safe(i8* nocapture %0) {
%2 = load i8*, i8** @safe.safe
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = add nsw i64 %3, 1
%5 = tail call i8* @realloc(i8* %2, i64 %4)
store i8* %5, i8** @safe.safe
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %1, %16
%9 = phi i64 [ %20, %16 ], [ 0, %1 ]
%10 = phi i8 [ %22, %16 ], [ %6, %1 ]
%11 = phi i8* [ %21, %16 ], [ %0, %1 ]
%12 = tail call i64 @isalnum(i8 signext %10)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %8
%15 = load i8, i8* %11
br label %16
16: ; preds = %8, %14
%17 = phi i8 [ %15, %14 ], [ 95, %8 ]
%18 = load i8*, i8** @safe.safe
%19 = getelementptr inbounds i8, i8* %18, i64 %9
store i8 %17, i8* %19
%20 = add nuw i64 %9, 1
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %8
24: ; preds = %16
%25 = and i64 %20, 4294967295
br label %26
26: ; preds = %24, %1
%27 = phi i8* [ %5, %1 ], [ %18, %24 ]
%28 = phi i64 [ 0, %1 ], [ %25, %24 ]
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8 0, i8* %29
ret i8* %27
}