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

/* Variables and functions */
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ write_and_sum(void *, int, int /*<<< orphan*/ *);
int write_blk_dat(const char *dat, int dat_size, uint16_t load_adr, FILE *f) {
  struct {
    char d1;
    char d2;
    uint16_t block_size;
    uint16_t load_adr;
  } block_header = {1, 0, .block_size = dat_size + sizeof(block_header),
                    .load_adr = load_adr};
  int sum = 0;
  sum += write_and_sum((void *)&block_header, sizeof(block_header), f);
  sum += write_and_sum((void *)dat, dat_size, f);
  char csum = 0 - (char)(sum & 0xff);
  fwrite(&csum, 1, 1, f);
  return (sizeof(block_header) + dat_size + 1);
}


x86 assembly (clang, -O0):
<write_blk_dat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movb   $0x1,-0x28(%rbp)
movb   $0x0,-0x27(%rbp)
movslq -0xc(%rbp),%rax
add    $0xc,%rax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xc,%esi
call   1030 <write_and_sum@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1030 <write_and_sum@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
and    $0xff,%eax
movsbl %al,%ecx
xor    %eax,%eax
sub    %ecx,%eax
mov    %al,-0x2d(%rbp)
mov    -0x18(%rbp),%rcx
lea    -0x2d(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1040 <fwrite@plt>
movslq -0xc(%rbp),%rax
add    $0xc,%rax
add    $0x1,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_blk_dat>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %esi,%r12d
mov    %rdi,%r15
movw   $0x1,0x8(%rsp)
lea    0xc(%r12),%eax
mov    %eax,0xc(%rsp)
mov    %edx,0x10(%rsp)
lea    0x8(%rsp),%rdi
mov    $0xc,%esi
mov    %rcx,%rdx
call   1040 <write_and_sum@plt>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r12d,%esi
mov    %r14,%rdx
call   1040 <write_and_sum@plt>
add    %eax,%ebx
shl    $0x18,%ebx
neg    %ebx
sar    $0x18,%ebx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <fputc@plt>
add    $0xd,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.anon = type { i8, i8, i32, i32 }
define i32 @write_blk_dat(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.anon
%10 = alloca i32
%11 = alloca i8
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%12 = getelementptr inbounds %struct.anon, %struct.anon* %9, i32 0, i32 0
store i8 1, i8* %12
%13 = getelementptr inbounds %struct.anon, %struct.anon* %9, i32 0, i32 1
store i8 0, i8* %13
%14 = getelementptr inbounds %struct.anon, %struct.anon* %9, i32 0, i32 2
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = add i64 %16, 12
%18 = trunc i64 %17 to i32
store i32 %18, i32* %14
%19 = getelementptr inbounds %struct.anon, %struct.anon* %9, i32 0, i32 3
%20 = load i32, i32* %7
store i32 %20, i32* %19
store i32 0, i32* %10
%21 = bitcast %struct.anon* %9 to i8*
%22 = load i32*, i32** %8
%23 = call i64 @write_and_sum(i8* %21, i32 12, i32* %22)
%24 = load i32, i32* %10
%25 = sext i32 %24 to i64
%26 = add nsw i64 %25, %23
%27 = trunc i64 %26 to i32
store i32 %27, i32* %10
%28 = load i8*, i8** %5
%29 = load i32, i32* %6
%30 = load i32*, i32** %8
%31 = call i64 @write_and_sum(i8* %28, i32 %29, i32* %30)
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = add nsw i64 %33, %31
%35 = trunc i64 %34 to i32
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = and i32 %36, 255
%38 = trunc i32 %37 to i8
%39 = sext i8 %38 to i32
%40 = sub nsw i32 0, %39
%41 = trunc i32 %40 to i8
store i8 %41, i8* %11
%42 = load i32*, i32** %8
%43 = call i32 @fwrite(i8* %11, i32 1, i32 1, i32* %42)
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = add i64 12, %45
%47 = add i64 %46, 1
%48 = trunc i64 %47 to i32
ret i32 %48
}

LLVM IR (-O2):
%struct.anon = type { i8, i8, i32, i32 }
define i32 @write_blk_dat(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca %struct.anon
%6 = getelementptr inbounds %struct.anon, %struct.anon* %5, i64 0, i32 0
store i8 1, i8* %6
%7 = getelementptr inbounds %struct.anon, %struct.anon* %5, i64 0, i32 1
store i8 0, i8* %7
%8 = getelementptr inbounds %struct.anon, %struct.anon* %5, i64 0, i32 2
%9 = add i32 %1, 12
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.anon, %struct.anon* %5, i64 0, i32 3
store i32 %2, i32* %10
%11 = call i64 @write_and_sum(i8* %6, i32 12, i32* %3)
%12 = call i64 @write_and_sum(i8* %0, i32 %1, i32* %3)
%13 = add i64 %12, %11
%14 = trunc i64 %13 to i32
%15 = mul i32 %14, -16777216
%16 = ashr exact i32 %15, 24
%17 = call i32 @fputc(i32 %16, i32* %3)
%18 = add i32 %1, 13
ret i32 %18
}