#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ str_to_upper(char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *stripext(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
int cas_write_header(FILE *out, char *name) {
  int i, namelength;
  char htname[7];
  strcpy(htname, "      ");
  name = stripext(name);
  namelength = strlen(name);
  if (namelength > 6)
    namelength = 6;
  else if (namelength < 1)
    return 0;
  strncpy(htname, str_to_upper(name), namelength);
  for (i = 0; i < 256; i++)
    fputc(0x00, out);
  fputc(0xA5, out);
  fputc(0x55, out);
  for (i = 0; i < 6; i++)
    fputc(htname[i], out);
  return -1;
}


x86 assembly (clang, -O0):
<cas_write_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x27(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1070 <stripext@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x6,-0x20(%rbp)
jle    11af <cas_write_header+0x4f>
movl   $0x6,-0x20(%rbp)
jmp    11ca <cas_write_header+0x6a>
cmpl   $0x1,-0x20(%rbp)
jge    11c5 <cas_write_header+0x65>
movl   $0x0,-0x4(%rbp)
jmp    126c <cas_write_header+0x10c>
jmp    11ca <cas_write_header+0x6a>
lea    -0x27(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <str_to_upper@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
mov    -0x20(%rbp),%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x100,-0x1c(%rbp)
jge    1216 <cas_write_header+0xb6>
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
call   1080 <fputc@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f0 <cas_write_header+0x90>
mov    -0x10(%rbp),%rsi
mov    $0xffffffa5,%edi
call   1080 <fputc@plt>
mov    -0x10(%rbp),%rsi
mov    $0x55,%edi
call   1080 <fputc@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x6,-0x1c(%rbp)
jge    1265 <cas_write_header+0x105>
movslq -0x1c(%rbp),%rax
mov    -0x27(%rbp,%rax,1),%al
mov    -0x10(%rbp),%rsi
movsbl %al,%edi
call   1080 <fputc@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1239 <cas_write_header+0xd9>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cas_write_header>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe8b(%rip),%rsi
lea    0x9(%rsp),%rdi
call   1050 <strcpy@plt>
mov    %rbp,%rdi
call   1070 <stripext@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    $0x6,%ebp
cmp    $0x6,%eax
jg     11a6 <cas_write_header+0x46>
mov    %eax,%ebp
test   %eax,%eax
jle    124e <cas_write_header+0xee>
mov    %r14,%rdi
call   1040 <str_to_upper@plt>
lea    0x9(%rsp),%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1030 <strncpy@plt>
mov    $0x100,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
add    $0xffffffff,%ebp
jne    11d0 <cas_write_header+0x70>
mov    $0xffffffa5,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
mov    $0x55,%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
movsbl 0x9(%rsp),%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
movsbl 0xa(%rsp),%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
movsbl 0xb(%rsp),%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
movsbl 0xc(%rsp),%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
movsbl 0xd(%rsp),%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
movsbl 0xe(%rsp),%edi
mov    %rbx,%rsi
call   1080 <fputc@plt>
mov    $0xffffffff,%eax
jmp    1250 <cas_write_header+0xf0>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c" \00"
define i32 @cas_write_header(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [7 x i8]
store i32* %0, i32** %4
store i8* %1, i8** %5
%9 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%10 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = load i8*, i8** %5
%12 = call i8* @stripext(i8* %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 6
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 6, i32* %7
br label %23
18: ; preds = %2
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 0, i32* %3
br label %57
22: ; preds = %18
br label %23
23: ; preds = %22, %17
%24 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%25 = load i8*, i8** %5
%26 = call i32 @str_to_upper(i8* %25)
%27 = load i32, i32* %7
%28 = call i32 @strncpy(i8* %24, i32 %26, i32 %27)
store i32 0, i32* %6
br label %29
29: ; preds = %35, %23
%30 = load i32, i32* %6
%31 = icmp slt i32 %30, 256
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i32*, i32** %4
%34 = call i32 @fputc(i8 signext 0, i32* %33)
br label %35
35: ; preds = %32
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 1
store i32 %37, i32* %6
br label %29
38: ; preds = %29
%39 = load i32*, i32** %4
%40 = call i32 @fputc(i8 signext -91, i32* %39)
%41 = load i32*, i32** %4
%42 = call i32 @fputc(i8 signext 85, i32* %41)
store i32 0, i32* %6
br label %43
43: ; preds = %53, %38
%44 = load i32, i32* %6
%45 = icmp slt i32 %44, 6
br i1 %45, label %46, label %56
46: ; preds = %43
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = load i32*, i32** %4
%52 = call i32 @fputc(i8 signext %50, i32* %51)
br label %53
53: ; preds = %46
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
store i32 %55, i32* %6
br label %43
56: ; preds = %43
store i32 -1, i32* %3
br label %57
57: ; preds = %56, %21
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str = [7 x i8] c" \00"
define i32 @cas_write_header(i32* nocapture %0, i8* %1) {
%3 = alloca [7 x i8]
%4 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
%5 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = call i8* @stripext(i8* %1)
%7 = call i32 @strlen(i8* %6)
%8 = icmp sgt i32 %7, 6
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = icmp slt i32 %7, 1
br i1 %10, label %40, label %11
11: ; preds = %2, %9
%12 = phi i32 [ %7, %9 ], [ 6, %2 ]
%13 = call i32 @str_to_upper(i8* %6)
%14 = call i32 @strncpy(i8* %4, i32 %13, i32 %12)
br label %15
15: ; preds = %11, %15
%16 = phi i32 [ 0, %11 ], [ %18, %15 ]
%17 = call i32 @fputc(i8 signext 0, i32* %0)
%18 = add nuw nsw i32 %16, 1
%19 = icmp eq i32 %18, 256
br i1 %19, label %20, label %15
20: ; preds = %15
%21 = call i32 @fputc(i8 signext -91, i32* %0)
%22 = call i32 @fputc(i8 signext 85, i32* %0)
%23 = load i8, i8* %4
%24 = call i32 @fputc(i8 signext %23, i32* %0)
%25 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 1
%26 = load i8, i8* %25
%27 = call i32 @fputc(i8 signext %26, i32* %0)
%28 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 2
%29 = load i8, i8* %28
%30 = call i32 @fputc(i8 signext %29, i32* %0)
%31 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 3
%32 = load i8, i8* %31
%33 = call i32 @fputc(i8 signext %32, i32* %0)
%34 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 4
%35 = load i8, i8* %34
%36 = call i32 @fputc(i8 signext %35, i32* %0)
%37 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 5
%38 = load i8, i8* %37
%39 = call i32 @fputc(i8 signext %38, i32* %0)
br label %40
40: ; preds = %20, %9
%41 = phi i32 [ 0, %9 ], [ -1, %20 ]
ret i32 %41
}