#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*/ fprintf(int /*<<< orphan*/ *, char *, char const);
int strlen(char const *);
void header(FILE *outfile, const char *str) {
  int count = strlen(str) + 3;
  unsigned int chksum = count;
  fprintf(outfile, "S0%02X0000", count);
  while (*str) {
    fprintf(outfile, "%02X", *str);
    chksum += *str++;
  }
  fprintf(outfile, "%02X\n", ~chksum & 0xFF);
}


x86 assembly (clang, -O0):
<header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x3,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%eax
lea    0xead(%rip),%rsi
movsbl %al,%edx
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     119c <header+0x7c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
lea    0xe94(%rip),%rsi
movsbl (%rax),%edx
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    115b <header+0x3b>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%eax
xor    $0xffffffff,%eax
and    $0xff,%eax
lea    0xe5e(%rip),%rsi
movsbl %al,%edx
call   1040 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<header>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
add    $0x3,%ebp
movsbl %bpl,%edx
lea    0xebb(%rip),%rsi
mov    %r14,%rdi
call   1040 <fprintf@plt>
mov    (%rbx),%al
test   %al,%al
je     117f <header+0x5f>
add    $0x1,%rbx
lea    0xead(%rip),%r15
xchg   %ax,%ax
movsbl %al,%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <fprintf@plt>
movzbl -0x1(%rbx),%eax
add    %eax,%ebp
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1160 <header+0x40>
not    %bpl
movsbl %bpl,%edx
lea    0xe83(%rip),%rsi
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fprintf@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"S0%02X0000\00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [6 x i8] c"%02X\0A\00"
define void @header(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i32 @strlen(i8* %7)
%9 = add nsw i32 %8, 3
store i32 %9, i32* %5
%10 = load i32, i32* %5
store i32 %10, i32* %6
%11 = load i32*, i32** %3
%12 = load i32, i32* %5
%13 = trunc i32 %12 to i8
%14 = call i32 @fprintf(i32* %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %13)
br label %15
15: ; preds = %19, %2
%16 = load i8*, i8** %4
%17 = load i8, i8* %16
%18 = icmp ne i8 %17, 0
br i1 %18, label %19, label %30
19: ; preds = %15
%20 = load i32*, i32** %3
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 signext %22)
%24 = load i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
%26 = load i8, i8* %24
%27 = sext i8 %26 to i32
%28 = load i32, i32* %6
%29 = add i32 %28, %27
store i32 %29, i32* %6
br label %15
30: ; preds = %15
%31 = load i32*, i32** %3
%32 = load i32, i32* %6
%33 = xor i32 %32, -1
%34 = and i32 %33, 255
%35 = trunc i32 %34 to i8
%36 = call i32 @fprintf(i32* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8 signext %35)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"S0%02X0000\00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [6 x i8] c"%02X\0A\00"
define void @header(i32* nocapture %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = add nsw i32 %3, 3
%5 = trunc i32 %4 to i8
%6 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext %5)
%7 = load i8, i8* %1
%8 = icmp eq i8 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %2, %9
%10 = phi i8 [ %18, %9 ], [ %7, %2 ]
%11 = phi i32 [ %17, %9 ], [ %4, %2 ]
%12 = phi i8* [ %14, %9 ], [ %1, %2 ]
%13 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8 signext %10)
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = load i8, i8* %12
%16 = zext i8 %15 to i32
%17 = add i32 %11, %16
%18 = load i8, i8* %14
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %9
20: ; preds = %9
%21 = trunc i32 %17 to i8
br label %22
22: ; preds = %20, %2
%23 = phi i8 [ %21, %20 ], [ %5, %2 ]
%24 = xor i8 %23, -1
%25 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8 signext %24)
ret void
}