#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 uint32_t;
typedef int /*<<< orphan*/ int32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *);
void UpdateDataFile(const char *filename, uint32_t count, void *data,
                    uint32_t record_size, int32_t new_record) {
  FILE *fp;
  uint32_t fs;
  fp = fopen(filename, "r+b");
  if (fp) {
    fseek(fp, 0, SEEK_END);
    fs = ftell(fp);
    if ((count * record_size) <= fs) {
      fseek(fp, count * record_size, SEEK_SET);
      fwrite(data, 1, record_size, fp);
    } else
      printf("Could not seek to record for updating in %s\n", filename);
    fclose(fp);
  } else {
    fp = fopen(filename, "wb");
    if (fp) {
      fwrite(data, 1, record_size, fp);
      fclose(fp);
    } else
      printf("Could not open %s for writing!\n", filename);
  }
}


x86 assembly (clang, -O0):
<UpdateDataFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     121a <UpdateDataFile+0xba>
mov    -0x28(%rbp),%rdi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x1c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jg     11fc <UpdateDataFile+0x9c>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
imul   -0x1c(%rbp),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
jmp    120c <UpdateDataFile+0xac>
mov    -0x8(%rbp),%rsi
lea    0xdfd(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1271 <UpdateDataFile+0x111>
mov    -0x8(%rbp),%rdi
lea    0xe0c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     125c <UpdateDataFile+0xfc>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
jmp    126c <UpdateDataFile+0x10c>
mov    -0x8(%rbp),%rsi
lea    0xdcd(%rip),%rdi
call   1040 <printf@plt>
jmp    1271 <UpdateDataFile+0x111>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UpdateDataFile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r12
lea    0xe86(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11bb <UpdateDataFile+0x5b>
mov    %rax,%rbx
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
mov    %rbx,%rdi
call   1050 <ftell@plt>
imul   %r15d,%ebp
cmp    %eax,%ebp
jle    11d4 <UpdateDataFile+0x74>
lea    0xe53(%rip),%rdi
mov    %r12,%rsi
call   1040 <printf@plt>
jmp    11fa <UpdateDataFile+0x9a>
lea    0xe6f(%rip),%rsi
mov    %r12,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     120a <UpdateDataFile+0xaa>
mov    %rax,%rbx
jmp    11e7 <UpdateDataFile+0x87>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
lea    0xe23(%rip),%rdi
mov    %r12,%rsi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [45 x i8] c"Could not seek to record for updating in %s\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [32 x i8] c"Could not open %s for writing!\0A\00"
define void @UpdateDataFile(i8* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%13 = load i8*, i8** %6
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %11
%15 = load i32*, i32** %11
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %45
17: ; preds = %5
%18 = load i32*, i32** %11
%19 = load i32, i32* @SEEK_END
%20 = call i32 @fseek(i32* %18, i32 0, i32 %19)
%21 = load i32*, i32** %11
%22 = call i32 @ftell(i32* %21)
store i32 %22, i32* %12
%23 = load i32, i32* %7
%24 = load i32, i32* %9
%25 = mul nsw i32 %23, %24
%26 = load i32, i32* %12
%27 = icmp sle i32 %25, %26
br i1 %27, label %28, label %39
28: ; preds = %17
%29 = load i32*, i32** %11
%30 = load i32, i32* %7
%31 = load i32, i32* %9
%32 = mul nsw i32 %30, %31
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %29, i32 %32, i32 %33)
%35 = load i8*, i8** %8
%36 = load i32, i32* %9
%37 = load i32*, i32** %11
%38 = call i32 @fwrite(i8* %35, i32 1, i32 %36, i32* %37)
br label %42
39: ; preds = %17
%40 = load i8*, i8** %6
%41 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %28
%43 = load i32*, i32** %11
%44 = call i32 @fclose(i32* %43)
br label %61
45: ; preds = %5
%46 = load i8*, i8** %6
%47 = call i32* @fopen(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %47, i32** %11
%48 = load i32*, i32** %11
%49 = icmp ne i32* %48, null
br i1 %49, label %50, label %57
50: ; preds = %45
%51 = load i8*, i8** %8
%52 = load i32, i32* %9
%53 = load i32*, i32** %11
%54 = call i32 @fwrite(i8* %51, i32 1, i32 %52, i32* %53)
%55 = load i32*, i32** %11
%56 = call i32 @fclose(i32* %55)
br label %60
57: ; preds = %45
%58 = load i8*, i8** %6
%59 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %57, %50
br label %61
61: ; preds = %60, %42
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [45 x i8] c"Could not seek to record for updating in %s\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [32 x i8] c"Could not open %s for writing!\0A\00"
define void @UpdateDataFile(i8* %0, i32 %1, i8* nocapture %2, i32 %3, i32 %4) {
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %22, label %8
8: ; preds = %5
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @fseek(i32* %6, i32 0, i32 %9)
%11 = tail call i32 @ftell(i32* %6)
%12 = mul nsw i32 %3, %1
%13 = icmp sgt i32 %12, %11
br i1 %13, label %18, label %14
14: ; preds = %8
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @fseek(i32* %6, i32 %12, i32 %15)
%17 = tail call i32 @fwrite(i8* %2, i32 1, i32 %3, i32* %6)
br label %20
18: ; preds = %8
%19 = tail call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %20
20: ; preds = %18, %14
%21 = tail call i32 @fclose(i32* %6)
br label %30
22: ; preds = %5
%23 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = tail call i32 @fwrite(i8* %2, i32 1, i32 %3, i32* %23)
%27 = tail call i32 @fclose(i32* %23)
br label %30
28: ; preds = %22
%29 = tail call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %30
30: ; preds = %25, %28, %20
ret void
}