#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 */
unsigned int htonl(unsigned int);
int /*<<< orphan*/ write(int, unsigned int *, int);
unsigned int writecrc(int, void const *, int, unsigned int);
void writechunk(int fh, const char *typ, const void *ptr, int len) {
  unsigned int v = htonl(len), crc;
  write(fh, &v, 4);
  crc = writecrc(fh, typ, 4, ~0);
  if (len)
    crc = writecrc(fh, ptr, len, crc);
  v = htonl(~crc);
  write(fh, &v, 4);
}


x86 assembly (clang, -O0):
<writechunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
mov    $0xffffffff,%ecx
call   1040 <writecrc@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     119a <writechunk+0x6a>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x24(%rbp),%ecx
call   1040 <writecrc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
xor    $0xffffffff,%edi
call   1050 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writechunk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    %ecx,%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
mov    $0xffffffff,%ecx
call   1040 <writecrc@plt>
test   %ebp,%ebp
je     1183 <writechunk+0x53>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %eax,%ecx
call   1040 <writecrc@plt>
not    %eax
mov    %eax,%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1030 <write@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @writechunk(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = load i32, i32* %8
%12 = call i32 @htonl(i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %5
%14 = call i32 @write(i32 %13, i32* %9, i32 4)
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = call i32 @writecrc(i32 %15, i8* %16, i32 4, i32 -1)
store i32 %17, i32* %10
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %4
%21 = load i32, i32* %5
%22 = load i8*, i8** %7
%23 = load i32, i32* %8
%24 = load i32, i32* %10
%25 = call i32 @writecrc(i32 %21, i8* %22, i32 %23, i32 %24)
store i32 %25, i32* %10
br label %26
26: ; preds = %20, %4
%27 = load i32, i32* %10
%28 = xor i32 %27, -1
%29 = call i32 @htonl(i32 %28)
store i32 %29, i32* %9
%30 = load i32, i32* %5
%31 = call i32 @write(i32 %30, i32* %9, i32 4)
ret void
}

LLVM IR (-O2):
define void @writechunk(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i32 @htonl(i32 %3)
store i32 %7, i32* %5
%8 = call i32 @write(i32 %0, i32* %5, i32 4)
%9 = tail call i32 @writecrc(i32 %0, i8* %1, i32 4, i32 -1)
%10 = icmp eq i32 %3, 0
br i1 %10, label %13, label %11
11: ; preds = %4
%12 = tail call i32 @writecrc(i32 %0, i8* %2, i32 %3, i32 %9)
br label %13
13: ; preds = %11, %4
%14 = phi i32 [ %12, %11 ], [ %9, %4 ]
%15 = xor i32 %14, -1
%16 = tail call i32 @htonl(i32 %15)
store i32 %16, i32* %5
%17 = call i32 @write(i32 %0, i32* %5, i32 4)
ret void
}