#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*/ fprintf(int /*<<< orphan*/, char *);
int fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int fixup_record_count(FILE *output_file, int file_format, int record_count) {
  if (!output_file) {
    fprintf(stderr, "fixup_record_count: No output file given\n");
    return 1;
  }
  int bytes_written, sts;
  unsigned char buffer[2];
  if (file_format == 1)
    sts = fseek(output_file, 0, SEEK_SET);
  else
    sts = fseek(output_file, 6, SEEK_SET);
  if (sts != 0) {
    fprintf(stderr,
            "fixup_record_count: Error repositioning to update record count\n");
    return 1;
  }
  buffer[0] = record_count & 0xFF;
  buffer[1] = (record_count >> 8) & 0xFF;
  bytes_written = fwrite(buffer, sizeof(char), 2, output_file);
  if (bytes_written != 2) {
    fprintf(stderr, "fixup_record_count: Error writing record count\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<fixup_record_count>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116e <fixup_record_count+0x3e>
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
lea    0xea3(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123e <fixup_record_count+0x10e>
cmpl   $0x1,-0x14(%rbp)
jne    1194 <fixup_record_count+0x64>
mov    -0x10(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <fseek@plt>
mov    %eax,-0x20(%rbp)
jmp    11ae <fixup_record_count+0x7e>
mov    -0x10(%rbp),%rdi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    $0x6,%esi
call   1040 <fseek@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11d9 <fixup_record_count+0xa9>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123e <fixup_record_count+0x10e>
mov    -0x18(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x22(%rbp)
mov    -0x18(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x21(%rbp)
lea    -0x22(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x2,%edx
call   1050 <fwrite@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x2,-0x1c(%rbp)
je     1237 <fixup_record_count+0x107>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xe44(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    123e <fixup_record_count+0x10e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fixup_record_count>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     116b <fixup_record_count+0x3b>
mov    %edx,%r14d
mov    %rdi,%rbx
xor    %ebp,%ebp
cmp    $0x1,%esi
mov    0x2e89(%rip),%rax
mov    (%rax),%edx
mov    $0x6,%esi
cmove  %ebp,%esi
call   1040 <fseek@plt>
test   %eax,%eax
je     117d <fixup_record_count+0x4d>
lea    0xec1(%rip),%rsi
jmp    11a6 <fixup_record_count+0x76>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
lea    0xe85(%rip),%rsi
jmp    11af <fixup_record_count+0x7f>
mov    %r14w,0xe(%rsp)
lea    0xe(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2,%edx
mov    %rbx,%rcx
call   1050 <fwrite@plt>
cmp    $0x2,%eax
je     11b9 <fixup_record_count+0x89>
lea    0xec4(%rip),%rsi
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1030 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [42 x i8] c"fixup_record_count: No output file given\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [64 x i8] c"fixup_record_count: Error repositioning to update record count\0A\00"
@.str.2 = [48 x i8] c"fixup_record_count: Error writing record count\0A\00"
define i32 @fixup_record_count(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [2 x i8]
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %52
16: ; preds = %3
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32*, i32** %5
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %20, i32 0, i32 %21)
store i32 %22, i32* %9
br label %27
23: ; preds = %16
%24 = load i32*, i32** %5
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @fseek(i32* %24, i32 6, i32 %25)
store i32 %26, i32* %9
br label %27
27: ; preds = %23, %19
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %52
33: ; preds = %27
%34 = load i32, i32* %7
%35 = and i32 %34, 255
%36 = trunc i32 %35 to i8
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
store i8 %36, i8* %37
%38 = load i32, i32* %7
%39 = ashr i32 %38, 8
%40 = and i32 %39, 255
%41 = trunc i32 %40 to i8
%42 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 1
store i8 %41, i8* %42
%43 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%44 = load i32*, i32** %5
%45 = call i32 @fwrite(i8* %43, i32 1, i32 2, i32* %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp ne i32 %46, 2
br i1 %47, label %48, label %51
48: ; preds = %33
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %4
br label %52
51: ; preds = %33
store i32 0, i32* %4
br label %52
52: ; preds = %51, %48, %30, %13
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [42 x i8] c"fixup_record_count: No output file given\0A\00"
@SEEK_SET = global i32 0
@.str.1 = [64 x i8] c"fixup_record_count: Error repositioning to update record count\0A\00"
@.str.2 = [48 x i8] c"fixup_record_count: Error writing record count\0A\00"
define i32 @fixup_record_count(i32* %0, i32 %1, i32 %2) {
%4 = alloca [2 x i8]
%5 = icmp eq i32* %0, null
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %29
9: ; preds = %3
%10 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%11 = icmp eq i32 %1, 1
%12 = load i32, i32* @SEEK_SET
%13 = select i1 %11, i32 0, i32 6
%14 = tail call i32 @fseek(i32* %0, i32 %13, i32 %12)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %9
%17 = trunc i32 %2 to i8
store i8 %17, i8* %10
%18 = lshr i32 %2, 8
%19 = trunc i32 %18 to i8
%20 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
store i8 %19, i8* %20
%21 = call i32 @fwrite(i8* %10, i32 1, i32 2, i32* %0)
%22 = icmp eq i32 %21, 2
br i1 %22, label %27, label %23
23: ; preds = %16, %9
%24 = phi i8* [ getelementptr inbounds ([64 x i8], [64 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), %16 ]
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* %24)
br label %27
27: ; preds = %23, %16
%28 = phi i32 [ 0, %16 ], [ 1, %23 ]
br label %29
29: ; preds = %27, %6
%30 = phi i32 [ %28, %27 ], [ 1, %6 ]
ret i32 %30
}