#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 */
scalar_t__ write_bits(int /*<<< orphan*/ *, int, int, int *);
scalar_t__ write_byte(int /*<<< orphan*/ *, unsigned char, int *);
long write_block(FILE *out, int block, unsigned char *buf, int sync_bits) {
  int a;
  int sync = -1;
  long flen = 0;
  unsigned char crc = 0;
  flen += write_bits(out, (1200), sync_bits, &sync);
  flen += write_bits(out, (600), 1, &sync);
  flen += write_byte(out, block, &sync);
  for (a = 0; a < 128; a++) {
    crc += buf[a];
    flen += write_byte(out, buf[a], &sync);
  }
  flen += write_byte(out, crc, &sync);
  return flen;
}


x86 assembly (clang, -O0):
<write_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movb   $0x0,-0x31(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    $0x4b0,%esi
lea    -0x24(%rbp),%rcx
call   1030 <write_bits@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x258,%esi
mov    $0x1,%edx
lea    -0x24(%rbp),%rcx
call   1030 <write_bits@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
lea    -0x24(%rbp),%rdx
movzbl %al,%esi
call   1040 <write_byte@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x80,-0x20(%rbp)
jge    11f8 <write_block+0xd8>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movzbl (%rax,%rcx,1),%ecx
movzbl -0x31(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x31(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
lea    -0x24(%rbp),%rdx
movzbl (%rax,%rcx,1),%esi
call   1040 <write_byte@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11a7 <write_block+0x87>
mov    -0x8(%rbp),%rdi
lea    -0x24(%rbp),%rdx
movzbl -0x31(%rbp),%esi
call   1040 <write_byte@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_block>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r13d
mov    %rdi,%r12
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x4(%rsp),%r15
mov    $0x4b0,%esi
mov    %ecx,%edx
mov    %r15,%rcx
call   1030 <write_bits@plt>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x258,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <write_bits@plt>
mov    %rax,%rbp
add    %rbx,%rbp
movzbl %r13b,%esi
mov    %r12,%rdi
mov    %r15,%rdx
call   1040 <write_byte@plt>
mov    %rax,%r13
add    %rbp,%r13
xor    %ebp,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movzbl (%r14,%rbp,1),%esi
add    %sil,%bl
mov    %r12,%rdi
mov    %r15,%rdx
call   1040 <write_byte@plt>
add    %rax,%r13
add    $0x1,%rbp
cmp    $0x80,%rbp
jne    1190 <write_block+0x70>
movzbl %bl,%esi
lea    0x4(%rsp),%rdx
mov    %r12,%rdi
call   1040 <write_byte@plt>
add    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i64 @write_block(i32* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i8
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 -1, i32* %10
store i64 0, i64* %11
store i8 0, i8* %12
%13 = load i32*, i32** %5
%14 = load i32, i32* %8
%15 = call i64 @write_bits(i32* %13, i32 1200, i32 %14, i32* %10)
%16 = load i64, i64* %11
%17 = add nsw i64 %16, %15
store i64 %17, i64* %11
%18 = load i32*, i32** %5
%19 = call i64 @write_bits(i32* %18, i32 600, i32 1, i32* %10)
%20 = load i64, i64* %11
%21 = add nsw i64 %20, %19
store i64 %21, i64* %11
%22 = load i32*, i32** %5
%23 = load i32, i32* %6
%24 = trunc i32 %23 to i8
%25 = call i64 @write_byte(i32* %22, i8 zeroext %24, i32* %10)
%26 = load i64, i64* %11
%27 = add nsw i64 %26, %25
store i64 %27, i64* %11
store i32 0, i32* %9
br label %28
28: ; preds = %51, %4
%29 = load i32, i32* %9
%30 = icmp slt i32 %29, 128
br i1 %30, label %31, label %54
31: ; preds = %28
%32 = load i8*, i8** %7
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = load i8, i8* %12
%39 = zext i8 %38 to i32
%40 = add nsw i32 %39, %37
%41 = trunc i32 %40 to i8
store i8 %41, i8* %12
%42 = load i32*, i32** %5
%43 = load i8*, i8** %7
%44 = load i32, i32* %9
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i8, i8* %46
%48 = call i64 @write_byte(i32* %42, i8 zeroext %47, i32* %10)
%49 = load i64, i64* %11
%50 = add nsw i64 %49, %48
store i64 %50, i64* %11
br label %51
51: ; preds = %31
%52 = load i32, i32* %9
%53 = add nsw i32 %52, 1
store i32 %53, i32* %9
br label %28
54: ; preds = %28
%55 = load i32*, i32** %5
%56 = load i8, i8* %12
%57 = call i64 @write_byte(i32* %55, i8 zeroext %56, i32* %10)
%58 = load i64, i64* %11
%59 = add nsw i64 %58, %57
store i64 %59, i64* %11
%60 = load i64, i64* %11
ret i64 %60
}

LLVM IR (-O2):
define i64 @write_block(i32* %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
store i32 -1, i32* %5
%7 = call i64 @write_bits(i32* %0, i32 1200, i32 %3, i32* %5)
%8 = call i64 @write_bits(i32* %0, i32 600, i32 1, i32* %5)
%9 = add nsw i64 %8, %7
%10 = trunc i32 %1 to i8
%11 = call i64 @write_byte(i32* %0, i8 zeroext %10, i32* %5)
%12 = add nsw i64 %9, %11
br label %13
13: ; preds = %4, %13
%14 = phi i64 [ 0, %4 ], [ %22, %13 ]
%15 = phi i8 [ 0, %4 ], [ %19, %13 ]
%16 = phi i64 [ %12, %4 ], [ %21, %13 ]
%17 = getelementptr inbounds i8, i8* %2, i64 %14
%18 = load i8, i8* %17
%19 = add i8 %18, %15
%20 = call i64 @write_byte(i32* %0, i8 zeroext %18, i32* %5)
%21 = add nsw i64 %20, %16
%22 = add nuw nsw i64 %14, 1
%23 = icmp eq i64 %22, 128
br i1 %23, label %24, label %13
24: ; preds = %13
%25 = call i64 @write_byte(i32* %0, i8 zeroext %19, i32* %5)
%26 = add nsw i64 %25, %21
ret i64 %26
}