#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 off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int fsync(int);
int lseek(int, int, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, unsigned char *, int);
void wfc_util_fset_buffer(char *pFileName, int positionStart,
                          unsigned char *pNewValue, int newValueLength) {
  int fd;
  off_t sz_file;
  char *pReadBuff = NULL;
  fd = open(pFileName, O_RDWR);
  if (fd >= 0) {
    sz_file = lseek(fd, 0, SEEK_END);
    pReadBuff = malloc(sz_file + 1);
    if (NULL != pReadBuff) {
      memset(pReadBuff, 0, sz_file + 1);
      lseek(fd, 0, SEEK_SET);
      read(fd, pReadBuff, sz_file);
      if (sz_file >= (positionStart + newValueLength)) {
        lseek(fd, positionStart, SEEK_SET);
        write(fd, pNewValue, newValueLength);
      } else {
        printf("wfc_util_fset_buffer : file size(%d) is less than to write "
               "position(%d)",
               (int)sz_file, (positionStart + newValueLength));
        printf("\n");
      }
      free(pReadBuff);
    } else {
      printf("wfc_util_fset_buffer : can not malloc(%d)", (int)sz_file);
      printf("\n");
    }
    if (-1 == fsync(fd)) {
      printf("wfc_util_fset_buffer : fail to fsync()");
      printf("\n");
    }
    close(fd);
  } else {
    printf("wfc_util_fset_buffer : can not open file");
    printf("\n");
  }
  return;
}


x86 assembly (clang, -O0):
<wfc_util_fset_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     130c <wfc_util_fset_buffer+0x16c>
mov    -0x20(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
je     12ad <wfc_util_fset_buffer+0x10d>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x20(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    -0x20(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1090 <read@plt>
mov    -0x24(%rbp),%eax
mov    -0xc(%rbp),%ecx
add    -0x1c(%rbp),%ecx
cmp    %ecx,%eax
jl     127a <wfc_util_fset_buffer+0xda>
mov    -0x20(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2d71(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <write@plt>
jmp    129f <wfc_util_fset_buffer+0xff>
mov    -0x24(%rbp),%esi
mov    -0xc(%rbp),%edx
add    -0x1c(%rbp),%edx
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    12cc <wfc_util_fset_buffer+0x12c>
mov    -0x24(%rbp),%esi
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
call   10b0 <fsync@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12ff <wfc_util_fset_buffer+0x15f>
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
call   1080 <close@plt>
jmp    1328 <wfc_util_fset_buffer+0x188>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wfc_util_fset_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%rbp
mov    %esi,%r12d
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
test   %eax,%eax
js     1254 <wfc_util_fset_buffer+0xa4>
mov    %eax,%ebx
mov    %rbp,(%rsp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,%ebp
lea    0x1(%rbp),%r15d
mov    %r15d,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     127a <wfc_util_fset_buffer+0xca>
mov    %rax,%r13
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1080 <memset@plt>
mov    0x2db8(%rip),%r15
mov    (%r15),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %ebp,%edx
call   10a0 <read@plt>
lea    (%r14,%r12,1),%edx
cmp    %edx,%ebp
jge    1296 <wfc_util_fset_buffer+0xe6>
lea    0xdc1(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
jmp    12b1 <wfc_util_fset_buffer+0x101>
lea    0xe3e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <putchar@plt>
lea    0xdc7(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
jmp    12b9 <wfc_util_fset_buffer+0x109>
mov    (%r15),%edx
mov    %ebx,%edi
mov    %r12d,%esi
call   1070 <lseek@plt>
mov    %ebx,%edi
mov    (%rsp),%rsi
mov    %r14d,%edx
call   1050 <write@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   10c0 <fsync@plt>
cmp    $0xffffffff,%eax
jne    12dd <wfc_util_fset_buffer+0x12d>
lea    0xda6(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <close@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str = [72 x i8] c"wfc_util_fset_buffer : file size(%d) is less than to write position(%d)\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [42 x i8] c"wfc_util_fset_buffer : can not malloc(%d)\00"
@.str.3 = [39 x i8] c"wfc_util_fset_buffer : fail to fsync()\00"
@.str.4 = [41 x i8] c"wfc_util_fset_buffer : can not open file\00"
define void @wfc_util_fset_buffer(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i8* null, i8** %11
%12 = load i8*, i8** %5
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %76
17: ; preds = %4
%18 = load i32, i32* %9
%19 = load i32, i32* @SEEK_END
%20 = call i32 @lseek(i32 %18, i32 0, i32 %19)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = add nsw i32 %21, 1
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %11
%24 = load i8*, i8** %11
%25 = icmp ne i8* null, %24
br i1 %25, label %26, label %62
26: ; preds = %17
%27 = load i8*, i8** %11
%28 = load i32, i32* %10
%29 = add nsw i32 %28, 1
%30 = call i32 @memset(i8* %27, i32 0, i32 %29)
%31 = load i32, i32* %9
%32 = load i32, i32* @SEEK_SET
%33 = call i32 @lseek(i32 %31, i32 0, i32 %32)
%34 = load i32, i32* %9
%35 = load i8*, i8** %11
%36 = load i32, i32* %10
%37 = call i32 @read(i32 %34, i8* %35, i32 %36)
%38 = load i32, i32* %10
%39 = load i32, i32* %6
%40 = load i32, i32* %8
%41 = add nsw i32 %39, %40
%42 = icmp sge i32 %38, %41
br i1 %42, label %43, label %52
43: ; preds = %26
%44 = load i32, i32* %9
%45 = load i32, i32* %6
%46 = load i32, i32* @SEEK_SET
%47 = call i32 @lseek(i32 %44, i32 %45, i32 %46)
%48 = load i32, i32* %9
%49 = load i8*, i8** %7
%50 = load i32, i32* %8
%51 = call i32 @write(i32 %48, i8* %49, i32 %50)
br label %59
52: ; preds = %26
%53 = load i32, i32* %10
%54 = load i32, i32* %6
%55 = load i32, i32* %8
%56 = add nsw i32 %54, %55
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0), i32 %53, i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %59
59: ; preds = %52, %43
%60 = load i8*, i8** %11
%61 = call i32 @free(i8* %60)
br label %66
62: ; preds = %17
%63 = load i32, i32* %10
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %63)
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %66
66: ; preds = %62, %59
%67 = load i32, i32* %9
%68 = call i32 @fsync(i32 %67)
%69 = icmp eq i32 -1, %68
br i1 %69, label %70, label %73
70: ; preds = %66
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %73
73: ; preds = %70, %66
%74 = load i32, i32* %9
%75 = call i32 @close(i32 %74)
br label %79
76: ; preds = %4
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %79
79: ; preds = %76, %73
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str = [72 x i8] c"wfc_util_fset_buffer : file size(%d) is less than to write position(%d)\00"
@.str.2 = [42 x i8] c"wfc_util_fset_buffer : can not malloc(%d)\00"
@.str.3 = [39 x i8] c"wfc_util_fset_buffer : fail to fsync()\00"
@.str.4 = [41 x i8] c"wfc_util_fset_buffer : can not open file\00"
define void @wfc_util_fset_buffer(i8* nocapture %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %41
8: ; preds = %4
%9 = load i32, i32* @SEEK_END
%10 = tail call i32 @lseek(i32 %6, i32 0, i32 %9)
%11 = add nsw i32 %10, 1
%12 = tail call i8* @malloc(i32 %11)
%13 = icmp eq i8* %12, null
br i1 %13, label %30, label %14
14: ; preds = %8
%15 = tail call i32 @memset(i8* %12, i32 0, i32 %11)
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @lseek(i32 %6, i32 0, i32 %16)
%18 = tail call i32 @read(i32 %6, i8* %12, i32 %10)
%19 = add nsw i32 %3, %1
%20 = icmp slt i32 %10, %19
br i1 %20, label %25, label %21
21: ; preds = %14
%22 = load i32, i32* @SEEK_SET
%23 = tail call i32 @lseek(i32 %6, i32 %1, i32 %22)
%24 = tail call i32 @write(i32 %6, i8* %2, i32 %3)
br label %28
25: ; preds = %14
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %19)
%27 = tail call i32 @putchar(i32 10)
br label %28
28: ; preds = %25, %21
%29 = tail call i32 @free(i8* %12)
br label %33
30: ; preds = %8
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%32 = tail call i32 @putchar(i32 10)
br label %33
33: ; preds = %30, %28
%34 = tail call i32 @fsync(i32 %6)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%38 = tail call i32 @putchar(i32 10)
br label %39
39: ; preds = %36, %33
%40 = tail call i32 @close(i32 %6)
br label %44
41: ; preds = %4
%42 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%43 = tail call i32 @putchar(i32 10)
br label %44
44: ; preds = %41, %39
ret void
}