#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);
int /*<<< orphan*/ isalpha(char const);
int /*<<< orphan*/ sprintf(char *, char *, char const);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strlcpy(char *, char *, int);
int strlen(char const *);
char *sanitize_name(char *outbuf, const char *input, int length) {
  char hex[3];
  char temp[strlen(input) * 2 + 1];
  char *output = temp;
  if (!isalpha(*input) && *input != '_')
    *(output++) = '_';
  while (*input) {
    if (isalnum(*input))
      *(output++) = *input;
    else if (*input == ' ' || *input == '-' || *input == '_') {
      *(output++) = '_';
    } else {
      sprintf(hex, "%.2x", *input);
      strcpy(output, hex);
      output += 2;
    }
    input++;
  }
  *(output) = 0;
  strlcpy(outbuf, temp, length);
  return outbuf;
}


x86 assembly (clang, -O0):
<sanitize_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    %ecx,%eax
lea    0x1(%rax,%rax,1),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x20(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x38(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x28(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1070 <isalpha@plt>
cmp    $0x0,%eax
jne    11e4 <sanitize_name+0x84>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
je     11e4 <sanitize_name+0x84>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x5f,(%rax)
jmp    11e9 <sanitize_name+0x89>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     12b5 <sanitize_name+0x155>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isalnum@plt>
cmp    $0x0,%rax
je     1228 <sanitize_name+0xc8>
mov    -0x10(%rbp),%rax
mov    (%rax),%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
jmp    12a4 <sanitize_name+0x144>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
je     1258 <sanitize_name+0xf8>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     1258 <sanitize_name+0xf8>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5f,%eax
jne    126f <sanitize_name+0x10f>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x5f,(%rax)
jmp    129f <sanitize_name+0x13f>
lea    -0x17(%rbp),%rdi
mov    -0x10(%rbp),%rax
lea    0xd82(%rip),%rsi
movsbl (%rax),%edx
call   1080 <sprintf@plt>
mov    -0x30(%rbp),%rdi
lea    -0x17(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x30(%rbp)
jmp    12a4 <sanitize_name+0x144>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11e9 <sanitize_name+0x89>
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
call   1060 <strlcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sanitize_name>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,-0x2c(%rbp)
mov    %rsi,%rbx
mov    %rdi,-0x40(%rbp)
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rsp,-0x38(%rbp)
add    %eax,%eax
add    $0x1,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
movsbl (%rbx),%edi
call   1070 <isalpha@plt>
mov    (%rbx),%cl
mov    %r12,%r13
test   %eax,%eax
jne    11c2 <sanitize_name+0x62>
mov    $0x5f,%al
mov    %r12,%r13
cmp    $0x5f,%cl
je     11c8 <sanitize_name+0x68>
lea    0x1(%r12),%r13
movb   $0x5f,(%r12)
mov    %ecx,%eax
test   %cl,%cl
je     123e <sanitize_name+0xde>
add    $0x1,%rbx
lea    -0x2f(%rbp),%r14
movabs $0x8000000000002001,%r15
jmp    11f3 <sanitize_name+0x93>
nopl   0x0(%rax)
mov    %cl,0x0(%r13)
add    $0x1,%r13
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
je     123e <sanitize_name+0xde>
movsbl %al,%edi
call   1050 <isalnum@plt>
movzbl -0x1(%rbx),%ecx
test   %rax,%rax
jne    11e0 <sanitize_name+0x80>
lea    -0x20(%rcx),%eax
cmp    $0x3f,%al
ja     121b <sanitize_name+0xbb>
movzbl %al,%eax
bt     %rax,%r15
jae    121b <sanitize_name+0xbb>
movb   $0x5f,0x0(%r13)
jmp    11e4 <sanitize_name+0x84>
movsbl %cl,%edx
mov    %r14,%rdi
lea    0xdd8(%rip),%rsi
call   1080 <sprintf@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
add    $0x2,%r13
jmp    11e8 <sanitize_name+0x88>
movb   $0x0,0x0(%r13)
mov    -0x40(%rbp),%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    -0x2c(%rbp),%edx
call   1060 <strlcpy@plt>
mov    -0x38(%rbp),%rsp
mov    %rbx,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%.2x\00"
define i8* @sanitize_name(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [3 x i8]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
%13 = mul nsw i32 %12, 2
%14 = add nsw i32 %13, 1
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
store i8* %17, i8** %10
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = call i32 @isalpha(i8 signext %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %30, label %22
22: ; preds = %3
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp ne i32 %25, 95
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i8*, i8** %10
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %10
store i8 95, i8* %28
br label %30
30: ; preds = %27, %22, %3
br label %31
31: ; preds = %74, %30
%32 = load i8*, i8** %5
%33 = load i8, i8* %32
%34 = icmp ne i8 %33, 0
br i1 %34, label %35, label %77
35: ; preds = %31
%36 = load i8*, i8** %5
%37 = load i8, i8* %36
%38 = call i64 @isalnum(i8 signext %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i8*, i8** %5
%42 = load i8, i8* %41
%43 = load i8*, i8** %10
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %10
store i8 %42, i8* %43
br label %74
45: ; preds = %35
%46 = load i8*, i8** %5
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 32
br i1 %49, label %60, label %50
50: ; preds = %45
%51 = load i8*, i8** %5
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 45
br i1 %54, label %60, label %55
55: ; preds = %50
%56 = load i8*, i8** %5
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 95
br i1 %59, label %60, label %63
60: ; preds = %55, %50, %45
%61 = load i8*, i8** %10
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %10
store i8 95, i8* %61
br label %73
63: ; preds = %55
%64 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%65 = load i8*, i8** %5
%66 = load i8, i8* %65
%67 = call i32 @sprintf(i8* %64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %66)
%68 = load i8*, i8** %10
%69 = getelementptr inbounds [3 x i8], [3 x i8]* %7, i64 0, i64 0
%70 = call i32 @strcpy(i8* %68, i8* %69)
%71 = load i8*, i8** %10
%72 = getelementptr inbounds i8, i8* %71, i64 2
store i8* %72, i8** %10
br label %73
73: ; preds = %63, %60
br label %74
74: ; preds = %73, %40
%75 = load i8*, i8** %5
%76 = getelementptr inbounds i8, i8* %75, i32 1
store i8* %76, i8** %5
br label %31
77: ; preds = %31
%78 = load i8*, i8** %10
store i8 0, i8* %78
%79 = load i8*, i8** %4
%80 = load i32, i32* %6
%81 = call i32 @strlcpy(i8* %79, i8* %17, i32 %80)
%82 = load i8*, i8** %4
%83 = load i8*, i8** %8
ret i8* %82
}

LLVM IR (-O2):
@.str = [5 x i8] c"%.2x\00"
define i8* @sanitize_name(i8* returned %0, i8* %1, i32 %2) {
%4 = alloca [3 x i8]
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @strlen(i8* %1)
%7 = shl nsw i32 %6, 1
%8 = or i32 %7, 1
%9 = zext i32 %8 to i64
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = load i8, i8* %1
%13 = tail call i32 @isalpha(i8 signext %12)
%14 = icmp eq i32 %13, 0
%15 = load i8, i8* %1
br i1 %14, label %16, label %20
16: ; preds = %3
%17 = icmp eq i8 %15, 95
br i1 %17, label %23, label %18
18: ; preds = %16
%19 = getelementptr inbounds i8, i8* %11, i64 1
store i8 95, i8* %11
br label %20
20: ; preds = %3, %18
%21 = phi i8* [ %19, %18 ], [ %11, %3 ]
%22 = icmp eq i8 %15, 0
br i1 %22, label %47, label %23
23: ; preds = %16, %20
%24 = phi i8 [ 95, %16 ], [ %15, %20 ]
%25 = phi i8* [ %11, %16 ], [ %21, %20 ]
br label %26
26: ; preds = %23, %42
%27 = phi i8 [ %45, %42 ], [ %24, %23 ]
%28 = phi i8* [ %43, %42 ], [ %25, %23 ]
%29 = phi i8* [ %44, %42 ], [ %1, %23 ]
%30 = call i64 @isalnum(i8 signext %27)
%31 = icmp eq i64 %30, 0
%32 = load i8, i8* %29
br i1 %31, label %35, label %33
33: ; preds = %26
%34 = getelementptr inbounds i8, i8* %28, i64 1
store i8 %32, i8* %28
br label %42
35: ; preds = %26
switch i8 %32, label %38 [
i8 32, label %36
i8 45, label %36
i8 95, label %36
]
36: ; preds = %35, %35, %35
%37 = getelementptr inbounds i8, i8* %28, i64 1
store i8 95, i8* %28
br label %42
38: ; preds = %35
%39 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %32)
%40 = call i32 @strcpy(i8* %28, i8* %5)
%41 = getelementptr inbounds i8, i8* %28, i64 2
br label %42
42: ; preds = %36, %38, %33
%43 = phi i8* [ %34, %33 ], [ %37, %36 ], [ %41, %38 ]
%44 = getelementptr inbounds i8, i8* %29, i64 1
%45 = load i8, i8* %44
%46 = icmp eq i8 %45, 0
br i1 %46, label %47, label %26
47: ; preds = %42, %20
%48 = phi i8* [ %21, %20 ], [ %43, %42 ]
store i8 0, i8* %48
%49 = call i32 @strlcpy(i8* %0, i8* %11, i32 %2)
ret i8* %0
}