#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 */
unsigned long pfdc2_crc(unsigned long, void const *, unsigned int);
scalar_t__ pfdc2_write(int /*<<< orphan*/ *, unsigned char *, int);
int /*<<< orphan*/ psi_set_uint16_be(unsigned char *, int, unsigned int);
int /*<<< orphan*/ psi_set_uint32_be(unsigned char *, int /*<<< orphan*/,
                                     unsigned long);
int pfdc2_save_chunk(FILE *fp, unsigned ckid, unsigned size, const void *data) {
  unsigned long crc;
  unsigned char buf[4];
  psi_set_uint16_be(buf, 0, ckid);
  psi_set_uint16_be(buf, 2, size);
  crc = pfdc2_crc(0, buf, 4);
  if (pfdc2_write(fp, buf, 4)) {
    return (1);
  }
  if (size > 0) {
    crc = pfdc2_crc(crc, data, size);
    if (pfdc2_write(fp, data, size)) {
      return (1);
    }
  }
  psi_set_uint32_be(buf, 0, crc);
  if (pfdc2_write(fp, buf, 4)) {
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<pfdc2_save_chunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x2c(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <psi_set_uint16_be@plt>
lea    -0x2c(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    $0x2,%esi
call   1050 <psi_set_uint16_be@plt>
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <pfdc2_crc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <pfdc2_write@plt>
cmp    $0x0,%rax
je     11b3 <pfdc2_save_chunk+0x73>
movl   $0x1,-0x4(%rbp)
jmp    123a <pfdc2_save_chunk+0xfa>
cmpl   $0x0,-0x18(%rbp)
jbe    11fc <pfdc2_save_chunk+0xbc>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <pfdc2_crc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <pfdc2_write@plt>
cmp    $0x0,%rax
je     11f7 <pfdc2_save_chunk+0xb7>
movl   $0x1,-0x4(%rbp)
jmp    123a <pfdc2_save_chunk+0xfa>
jmp    11fc <pfdc2_save_chunk+0xbc>
lea    -0x2c(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1030 <psi_set_uint32_be@plt>
mov    -0x10(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
mov    $0x4,%edx
call   1060 <pfdc2_write@plt>
cmp    $0x0,%rax
je     1233 <pfdc2_save_chunk+0xf3>
movl   $0x1,-0x4(%rbp)
jmp    123a <pfdc2_save_chunk+0xfa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pfdc2_save_chunk>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %edx,%ebp
mov    %esi,%edx
mov    %rdi,%r15
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <psi_set_uint16_be@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
mov    %ebp,%edx
call   1050 <psi_set_uint16_be@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <pfdc2_crc@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <pfdc2_write@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    11f0 <pfdc2_save_chunk+0xb0>
test   %ebp,%ebp
je     11c6 <pfdc2_save_chunk+0x86>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <pfdc2_crc@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1060 <pfdc2_write@plt>
test   %rax,%rax
jne    11f0 <pfdc2_save_chunk+0xb0>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1030 <psi_set_uint32_be@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1060 <pfdc2_write@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pfdc2_save_chunk(i32* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca [4 x i8]
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%13 = load i32, i32* %7
%14 = call i32 @psi_set_uint16_be(i8* %12, i32 0, i32 %13)
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%16 = load i32, i32* %8
%17 = call i32 @psi_set_uint16_be(i8* %15, i32 2, i32 %16)
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%19 = call i64 @pfdc2_crc(i64 0, i8* %18, i32 4)
store i64 %19, i64* %10
%20 = load i32*, i32** %6
%21 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%22 = call i64 @pfdc2_write(i32* %20, i8* %21, i32 4)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %4
store i32 1, i32* %5
br label %50
25: ; preds = %4
%26 = load i32, i32* %8
%27 = icmp ugt i32 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %25
%29 = load i64, i64* %10
%30 = load i8*, i8** %9
%31 = load i32, i32* %8
%32 = call i64 @pfdc2_crc(i64 %29, i8* %30, i32 %31)
store i64 %32, i64* %10
%33 = load i32*, i32** %6
%34 = load i8*, i8** %9
%35 = load i32, i32* %8
%36 = call i64 @pfdc2_write(i32* %33, i8* %34, i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %28
store i32 1, i32* %5
br label %50
39: ; preds = %28
br label %40
40: ; preds = %39, %25
%41 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%42 = load i64, i64* %10
%43 = call i32 @psi_set_uint32_be(i8* %41, i32 0, i64 %42)
%44 = load i32*, i32** %6
%45 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%46 = call i64 @pfdc2_write(i32* %44, i8* %45, i32 4)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %40
store i32 1, i32* %5
br label %50
49: ; preds = %40
store i32 0, i32* %5
br label %50
50: ; preds = %49, %48, %38, %24
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
define i32 @pfdc2_save_chunk(i32* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca [4 x i8]
%6 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%7 = call i32 @psi_set_uint16_be(i8* %6, i32 0, i32 %1)
%8 = call i32 @psi_set_uint16_be(i8* %6, i32 2, i32 %2)
%9 = call i64 @pfdc2_crc(i64 0, i8* %6, i32 4)
%10 = call i64 @pfdc2_write(i32* %0, i8* %6, i32 4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %4
%13 = icmp eq i32 %2, 0
br i1 %13, label %18, label %14
14: ; preds = %12
%15 = call i64 @pfdc2_crc(i64 %9, i8* %3, i32 %2)
%16 = call i64 @pfdc2_write(i32* %0, i8* %3, i32 %2)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14, %12
%19 = phi i64 [ %15, %14 ], [ %9, %12 ]
%20 = call i32 @psi_set_uint32_be(i8* %6, i32 0, i64 %19)
%21 = call i64 @pfdc2_write(i32* %0, i8* %6, i32 4)
%22 = icmp ne i64 %21, 0
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %18, %14, %4
%25 = phi i32 [ 1, %4 ], [ 1, %14 ], [ %23, %18 ]
ret i32 %25
}