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


x86 assembly (clang, -O0):
<psi_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    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    %eax,%edx
xor    %esi,%esi
call   1030 <psi_set_uint32_be@plt>
lea    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%eax
mov    %eax,%edx
mov    $0x4,%esi
call   1030 <psi_set_uint32_be@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
lea    -0x28(%rbp),%rcx
call   1040 <psi_write_crc@plt>
cmp    $0x0,%rax
je     118d <psi_save_chunk+0x6d>
movl   $0x1,-0x4(%rbp)
jmp    1208 <psi_save_chunk+0xe8>
cmpl   $0x0,-0x18(%rbp)
jbe    11c6 <psi_save_chunk+0xa6>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
lea    -0x28(%rbp),%rcx
call   1040 <psi_write_crc@plt>
cmp    $0x0,%rax
je     11c1 <psi_save_chunk+0xa1>
movl   $0x1,-0x4(%rbp)
jmp    1208 <psi_save_chunk+0xe8>
jmp    11c6 <psi_save_chunk+0xa6>
lea    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1030 <psi_set_uint32_be@plt>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <psi_write_crc@plt>
cmp    $0x0,%rax
je     1201 <psi_save_chunk+0xe1>
movl   $0x1,-0x4(%rbp)
jmp    1208 <psi_save_chunk+0xe8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<psi_save_chunk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rdi,%r15
mov    %esi,%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <psi_set_uint32_be@plt>
mov    %ebp,%edx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1030 <psi_set_uint32_be@plt>
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rcx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1040 <psi_write_crc@plt>
mov    $0x1,%ebx
test   %rax,%rax
jne    11c3 <psi_save_chunk+0xa3>
test   %ebp,%ebp
je     1195 <psi_save_chunk+0x75>
lea    0x8(%rsp),%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <psi_write_crc@plt>
test   %rax,%rax
jne    11c3 <psi_save_chunk+0xa3>
mov    0x8(%rsp),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <psi_set_uint32_be@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <psi_write_crc@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @psi_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 [8 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 [8 x i8], [8 x i8]* %11, i64 0, i64 0
%13 = load i32, i32* %7
%14 = zext i32 %13 to i64
%15 = call i32 @psi_set_uint32_be(i8* %12, i32 0, i64 %14)
%16 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%17 = load i32, i32* %8
%18 = zext i32 %17 to i64
%19 = call i32 @psi_set_uint32_be(i8* %16, i32 4, i64 %18)
store i64 0, i64* %10
%20 = load i32*, i32** %6
%21 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%22 = call i64 @psi_write_crc(i32* %20, i8* %21, i32 8, i64* %10)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %4
store i32 1, i32* %5
br label %46
25: ; preds = %4
%26 = load i32, i32* %8
%27 = icmp ugt i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = load i32*, i32** %6
%30 = load i8*, i8** %9
%31 = load i32, i32* %8
%32 = call i64 @psi_write_crc(i32* %29, i8* %30, i32 %31, i64* %10)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 1, i32* %5
br label %46
35: ; preds = %28
br label %36
36: ; preds = %35, %25
%37 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%38 = load i64, i64* %10
%39 = call i32 @psi_set_uint32_be(i8* %37, i32 0, i64 %38)
%40 = load i32*, i32** %6
%41 = getelementptr inbounds [8 x i8], [8 x i8]* %11, i64 0, i64 0
%42 = call i64 @psi_write_crc(i32* %40, i8* %41, i32 4, i64* null)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %36
store i32 1, i32* %5
br label %46
45: ; preds = %36
store i32 0, i32* %5
br label %46
46: ; preds = %45, %44, %34, %24
%47 = load i32, i32* %5
ret i32 %47
}

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