#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 */
int /*<<< orphan*/ exit(int);
int fwrite(char const *, int, int, int /*<<< orphan*/);
scalar_t__ pad_to_four(size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdout;
scalar_t__ write_le_uint32(size_t);
size_t write_trailer_field(char const *label, char const *data, size_t length) {
  size_t count = fwrite(data, 1, length, stdout);
  if (count != length) {
    perror("write_trailer_field fwrite data");
    exit(1);
  }
  count += pad_to_four(length);
  count += write_le_uint32(length);
  if (fwrite(label, 1, 4, stdout) != 4) {
    perror("write_trailer_field fwrite label");
    exit(1);
  }
  count += 4;
  return count;
}


x86 assembly (clang, -O0):
<write_trailer_field>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%edx
mov    0x2e6b(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1070 <fwrite@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11ab <write_trailer_field+0x5b>
lea    0xe64(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <pad_to_four@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <write_le_uint32@plt>
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x4,%edx
call   1070 <fwrite@plt>
cmp    $0x4,%eax
je     1208 <write_trailer_field+0xb8>
lea    0xe27(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_trailer_field>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rdi,%r14
mov    0x2e7a(%rip),%r13
mov    0x0(%r13),%ecx
mov    %rsi,%rdi
mov    $0x1,%esi
call   1070 <fwrite@plt>
cltq
cmp    %rbx,%rax
jne    11c4 <write_trailer_field+0x74>
mov    %rbx,%rdi
call   1030 <pad_to_four@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <write_le_uint32@plt>
mov    %rax,%r12
mov    0x0(%r13),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1070 <fwrite@plt>
cmp    $0x4,%eax
jne    11cd <write_trailer_field+0x7d>
add    %r15,%rbx
lea    (%r12,%rbx,1),%rax
add    $0x4,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xe35(%rip),%rdi
jmp    11d4 <write_trailer_field+0x84>
lea    0xe4c(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [32 x i8] c"write_trailer_field fwrite data\00"
@.str.1 = [33 x i8] c"write_trailer_field fwrite label\00"
define i64 @write_trailer_field(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%8 = load i8*, i8** %5
%9 = load i64, i64* %6
%10 = trunc i64 %9 to i32
%11 = load i32, i32* @stdout
%12 = call i32 @fwrite(i8* %8, i32 1, i32 %10, i32 %11)
%13 = sext i32 %12 to i64
store i64 %13, i64* %7
%14 = load i64, i64* %7
%15 = load i64, i64* %6
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %3
%21 = load i64, i64* %6
%22 = call i64 @pad_to_four(i64 %21)
%23 = load i64, i64* %7
%24 = add i64 %23, %22
store i64 %24, i64* %7
%25 = load i64, i64* %6
%26 = call i64 @write_le_uint32(i64 %25)
%27 = load i64, i64* %7
%28 = add i64 %27, %26
store i64 %28, i64* %7
%29 = load i8*, i8** %4
%30 = load i32, i32* @stdout
%31 = call i32 @fwrite(i8* %29, i32 1, i32 4, i32 %30)
%32 = icmp ne i32 %31, 4
br i1 %32, label %33, label %36
33: ; preds = %20
%34 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %20
%37 = load i64, i64* %7
%38 = add i64 %37, 4
store i64 %38, i64* %7
%39 = load i64, i64* %7
ret i64 %39
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [32 x i8] c"write_trailer_field fwrite data\00"
@.str.1 = [33 x i8] c"write_trailer_field fwrite label\00"
define i64 @write_trailer_field(i8* %0, i8* %1, i64 %2) {
%4 = trunc i64 %2 to i32
%5 = load i32, i32* @stdout
%6 = tail call i32 @fwrite(i8* %1, i32 1, i32 %4, i32 %5)
%7 = sext i32 %6 to i64
%8 = icmp eq i64 %7, %2
br i1 %8, label %12, label %9
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %3
%13 = tail call i64 @pad_to_four(i64 %2)
%14 = tail call i64 @write_le_uint32(i64 %2)
%15 = load i32, i32* @stdout
%16 = tail call i32 @fwrite(i8* %0, i32 1, i32 4, i32 %15)
%17 = icmp eq i32 %16, 4
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = add nsw i64 %2, 4
%23 = add i64 %22, %13
%24 = add i64 %23, %14
ret i64 %24
}