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


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

x86 assembly (clang, -O2):
<finalize_file>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <ftell@plt>
mov    %eax,(%rsp)
mov    0x2e9a(%rip),%r14
mov    (%r14),%edx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <fseek@plt>
mov    %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    (%rsp),%eax
add    $0xffffffd4,%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fwrite@plt>
add    $0x8,%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 i32
%4 = alloca i32
store i32* %0, i32** %2
%5 = load i32*, i32** %2
%6 = call i32 @ftell(i32* %5)
store i32 %6, i32* %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(i32* %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 i32, i32* %3
%16 = sub nsw i32 %15, 44
store i32 %16, i32* %4
%17 = load i32*, i32** %2
%18 = call i32 @fwrite(i32* %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 i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @ftell(i32* %0)
store i32 %5, i32* %2
%6 = load i32, i32* @SEEK_SET
%7 = tail call i32 @fseek(i32* %0, i32 4, i32 %6)
%8 = call i32 @fwrite(i32* %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 i32* %3 to i8*
%12 = load i32, i32* %2
%13 = add nsw i32 %12, -44
store i32 %13, i32* %3
%14 = call i32 @fwrite(i32* %3, i32 4, i32 1, i32* %0)
ret void
}