#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*/ chaz_Util_die(char *, unsigned long);
scalar_t__ isalnum(char const);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char const *);
char toupper(char const);
void chaz_Headers_encode_affirmation(const char *header_name, char *buffer,
                                     size_t buf_size) {
  char *buf, *buf_end;
  size_t len = strlen(header_name) + sizeof("HAS_");
  if (len + 1 > buf_size) {
    chaz_Util_die("Buffer too small: %lu", (unsigned long)buf_size);
  }
  strcpy(buffer, "HAS_");
  for (buf = buffer + sizeof("HAS_") - 1, buf_end = buffer + len; buf < buf_end;
       header_name++, buf++) {
    if (*header_name == '\0') {
      *buf = '\0';
      break;
    } else if (isalnum(*header_name)) {
      *buf = toupper(*header_name);
    } else {
      *buf = '_';
    }
  }
}


x86 assembly (clang, -O0):
<chaz_Headers_encode_affirmation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x5,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
cmp    -0x18(%rbp),%rax
jbe    1197 <chaz_Headers_encode_affirmation+0x47>
mov    -0x18(%rbp),%rsi
lea    0xe6e(%rip),%rdi
call   1070 <chaz_Util_die@plt>
mov    -0x10(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
add    $0x5,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    124a <chaz_Headers_encode_affirmation+0xfa>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11ed <chaz_Headers_encode_affirmation+0x9d>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    124a <chaz_Headers_encode_affirmation+0xfa>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1060 <isalnum@plt>
cmp    $0x0,%rax
je     121c <chaz_Headers_encode_affirmation+0xcc>
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
call   1040 <toupper@plt>
mov    %al,%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
jmp    1223 <chaz_Headers_encode_affirmation+0xd3>
mov    -0x20(%rbp),%rax
movb   $0x5f,(%rax)
jmp    1228 <chaz_Headers_encode_affirmation+0xd8>
jmp    122d <chaz_Headers_encode_affirmation+0xdd>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11c3 <chaz_Headers_encode_affirmation+0x73>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chaz_Headers_encode_affirmation>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
call   1050 <strlen@plt>
mov    %rax,%r15
add    $0x6,%rax
cmp    %r12,%rax
jbe    1181 <chaz_Headers_encode_affirmation+0x31>
lea    0xe87(%rip),%rdi
mov    %r12,%rsi
call   1070 <chaz_Util_die@plt>
lea    0xe8e(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
movabs $0x7ffffffffffffffa,%rax
ff ff 7f
cmp    %rax,%r15
ja     11e4 <chaz_Headers_encode_affirmation+0x94>
add    $0x5,%r15
add    %rbx,%r15
add    $0x4,%rbx
jmp    11c8 <chaz_Headers_encode_affirmation+0x78>
nopl   0x0(%rax)
movsbl (%r14),%edi
call   1040 <toupper@plt>
mov    %al,(%rbx)
add    $0x1,%r14
add    $0x1,%rbx
cmp    %r15,%rbx
jae    11e4 <chaz_Headers_encode_affirmation+0x94>
movzbl (%r14),%eax
test   %al,%al
je     11e1 <chaz_Headers_encode_affirmation+0x91>
movsbl %al,%edi
call   1060 <isalnum@plt>
test   %rax,%rax
jne    11b0 <chaz_Headers_encode_affirmation+0x60>
mov    $0x5f,%al
jmp    11b9 <chaz_Headers_encode_affirmation+0x69>
movb   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Buffer too small: %lu\00"
@.str.1 = [5 x i8] c"HAS_\00"
define void @chaz_Headers_encode_affirmation(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
%12 = add i64 %11, 5
store i64 %12, i64* %9
%13 = load i64, i64* %9
%14 = add i64 %13, 1
%15 = load i64, i64* %6
%16 = icmp ugt i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i64, i64* %6
%19 = call i32 @chaz_Util_die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 %18)
br label %20
20: ; preds = %17, %3
%21 = load i8*, i8** %5
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = load i8*, i8** %5
%24 = getelementptr inbounds i8, i8* %23, i64 5
%25 = getelementptr inbounds i8, i8* %24, i64 -1
store i8* %25, i8** %7
%26 = load i8*, i8** %5
%27 = load i64, i64* %9
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8* %28, i8** %8
br label %29
29: ; preds = %54, %20
%30 = load i8*, i8** %7
%31 = load i8*, i8** %8
%32 = icmp ult i8* %30, %31
br i1 %32, label %33, label %59
33: ; preds = %29
%34 = load i8*, i8** %4
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = load i8*, i8** %7
store i8 0, i8* %39
br label %59
40: ; preds = %33
%41 = load i8*, i8** %4
%42 = load i8, i8* %41
%43 = call i64 @isalnum(i8 signext %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = load i8*, i8** %4
%47 = load i8, i8* %46
%48 = call signext i8 @toupper(i8 signext %47)
%49 = load i8*, i8** %7
store i8 %48, i8* %49
br label %52
50: ; preds = %40
%51 = load i8*, i8** %7
store i8 95, i8* %51
br label %52
52: ; preds = %50, %45
br label %53
53: ; preds = %52
br label %54
54: ; preds = %53
%55 = load i8*, i8** %4
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %4
%57 = load i8*, i8** %7
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %7
br label %29
59: ; preds = %38, %29
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"Buffer too small: %lu\00"
@.str.1 = [5 x i8] c"HAS_\00"
define void @chaz_Headers_encode_affirmation(i8* nocapture %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = add i64 %4, 5
%6 = add i64 %4, 6
%7 = icmp ugt i64 %6, %2
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i32 @chaz_Util_die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i64 %2)
br label %10
10: ; preds = %8, %3
%11 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = getelementptr inbounds i8, i8* %1, i64 %5
%13 = icmp ult i64 %4, 9223372036854775803
br i1 %13, label %14, label %33
14: ; preds = %10
%15 = getelementptr inbounds i8, i8* %1, i64 4
br label %16
16: ; preds = %14, %28
%17 = phi i8* [ %30, %28 ], [ %0, %14 ]
%18 = phi i8* [ %31, %28 ], [ %15, %14 ]
%19 = load i8, i8* %17
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i8 0, i8* %18
br label %33
22: ; preds = %16
%23 = tail call i64 @isalnum(i8 signext %19)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i8, i8* %17
%27 = tail call signext i8 @toupper(i8 signext %26)
br label %28
28: ; preds = %22, %25
%29 = phi i8 [ %27, %25 ], [ 95, %22 ]
store i8 %29, i8* %18
%30 = getelementptr inbounds i8, i8* %17, i64 1
%31 = getelementptr inbounds i8, i8* %18, i64 1
%32 = icmp ult i8* %31, %12
br i1 %32, label %16, label %33
33: ; preds = %28, %10, %21
ret void
}