#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*/ SEEK_SET;
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
size_t ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(size_t *, int, int, int /*<<< orphan*/ *);
void finalize_file(FILE *fp) {
  size_t file_size = ftell(fp);
  fseek(fp, 4, SEEK_SET);
  fwrite(&file_size, sizeof(int), 1, fp);
  fseek(fp, 40, SEEK_SET);
  size_t data_size = file_size - 44;
  fwrite(&data_size, sizeof(int), 1, fp);
}


x86 assembly (clang, -O0):
<finalize_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <ftell@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e8c(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1040 <fseek@plt>
mov    -0x8(%rbp),%rcx
lea    -0x10(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1050 <fwrite@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    $0x28,%esi
call   1040 <fseek@plt>
mov    -0x10(%rbp),%rax
sub    $0x2c,%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1050 <fwrite@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<finalize_file>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1030 <ftell@plt>
mov    %rax,0x8(%rsp)
mov    0x2e95(%rip),%r14
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <fseek@plt>
lea    0x8(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fwrite@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x28,%esi
call   1040 <fseek@plt>
mov    0x8(%rsp),%rax
add    $0xffffffffffffffd4,%rax
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fwrite@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define void @finalize_file(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i64
store i32* %0, i32** %2
%5 = load i32*, i32** %2
%6 = call i64 @ftell(i32* %5)
store i64 %6, i64* %3
%7 = load i32*, i32** %2
%8 = load i32, i32* @SEEK_SET
%9 = call i32 @fseek(i32* %7, i32 4, i32 %8)
%10 = load i32*, i32** %2
%11 = call i32 @fwrite(i64* %3, i32 4, i32 1, i32* %10)
%12 = load i32*, i32** %2
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @fseek(i32* %12, i32 40, i32 %13)
%15 = load i64, i64* %3
%16 = sub i64 %15, 44
store i64 %16, i64* %4
%17 = load i32*, i32** %2
%18 = call i32 @fwrite(i64* %4, i32 4, i32 1, i32* %17)
ret void
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define void @finalize_file(i32* nocapture %0) {
%2 = alloca i64
%3 = alloca i64
%4 = bitcast i64* %2 to i8*
%5 = tail call i64 @ftell(i32* %0)
store i64 %5, i64* %2
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @fseek(i32* %0, i32 4, i32 %6)
%8 = call i32 @fwrite(i64* %2, i32 4, i32 1, i32* %0)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %0, i32 40, i32 %9)
%11 = bitcast i64* %3 to i8*
%12 = load i64, i64* %2
%13 = add i64 %12, -44
store i64 %13, i64* %3
%14 = call i32 @fwrite(i64* %3, i32 4, i32 1, i32* %0)
ret void
}