#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ ftruncate(int, int);
int lseek(int, int, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
char *strstr(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
void wfc_util_finsert_new_string(int fd, char **ppReadedBuff,
                                 char *pNewStringValue, char *pEndOfCfg) {
  off_t sz_file;
  int sz_backupBuff = 0;
  char *pReadBuff = NULL, *pBackupBuff = NULL;
  char *pSPointer = NULL, *pETagPointer = NULL;
  if (0 == fd || NULL == pNewStringValue || 0 == strlen(pNewStringValue)) {
    printf("wfc_util_finsert_new_string : unvalid parameters");
    printf("\n");
    return;
  }
  if (NULL == ppReadedBuff) {
    return;
  } else {
    pReadBuff = *ppReadedBuff;
  }
  pETagPointer = strstr(pReadBuff, pEndOfCfg);
  pSPointer = pETagPointer - 1;
  sz_file = lseek(fd, 0, SEEK_END);
  sz_backupBuff = (int)sz_file - (pETagPointer - pReadBuff);
  pBackupBuff = malloc(sz_backupBuff);
  if (NULL != pBackupBuff) {
    memset(pBackupBuff, 0, sz_backupBuff);
    memcpy(pBackupBuff, pETagPointer, sz_backupBuff);
    lseek(fd, (int)(pSPointer - pReadBuff), SEEK_SET);
    write(fd, pNewStringValue, strlen(pNewStringValue));
    pETagPointer = pSPointer + strlen(pNewStringValue);
    lseek(fd, (int)(pETagPointer - pReadBuff), SEEK_SET);
    write(fd, pBackupBuff, sz_backupBuff);
    ftruncate(fd, sz_file + strlen(pNewStringValue) - 1);
    free(pBackupBuff);
    if (NULL != ppReadedBuff) {
    }
  } else {
    printf("wfc_util_finsert_new_string : can not malloc(%d)", sz_backupBuff);
    printf("\n");
  }
  return;
}


x86 assembly (clang, -O0):
<wfc_util_finsert_new_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
xor    %eax,%eax
cmp    -0x4(%rbp),%eax
je     120a <wfc_util_finsert_new_string+0x6a>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     120a <wfc_util_finsert_new_string+0x6a>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    122b <wfc_util_finsert_new_string+0x8b>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13be <wfc_util_finsert_new_string+0x21e>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    123c <wfc_util_finsert_new_string+0x9c>
jmp    13be <wfc_util_finsert_new_string+0x21e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10c0 <strstr@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x40(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <lseek@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    -0x30(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
je     139a <wfc_util_finsert_new_string+0x1fa>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%edx
xor    %esi,%esi
call   1090 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   10a0 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    0x2cfc(%rip),%rax
mov    (%rax),%edx
call   1080 <lseek@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
mov    0x2c98(%rip),%rax
mov    (%rax),%edx
call   1080 <lseek@plt>
mov    -0x4(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1040 <write@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x50(%rbp),%esi
mov    -0x4c(%rbp),%edi
add    %eax,%esi
sub    $0x1,%esi
call   1070 <ftruncate@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     1395 <wfc_util_finsert_new_string+0x1f5>
jmp    1395 <wfc_util_finsert_new_string+0x1f5>
jmp    13b9 <wfc_util_finsert_new_string+0x219>
mov    -0x28(%rbp),%esi
lea    0xc8f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xc7f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    13be <wfc_util_finsert_new_string+0x21e>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wfc_util_finsert_new_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edi,%edi
je     12f4 <wfc_util_finsert_new_string+0x144>
mov    %rdx,%r15
test   %rdx,%rdx
je     12f4 <wfc_util_finsert_new_string+0x144>
mov    %rcx,%rbp
mov    %rsi,%rbx
mov    %edi,%r12d
mov    %r15,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
je     12f4 <wfc_util_finsert_new_string+0x144>
test   %rbx,%rbx
je     131a <wfc_util_finsert_new_string+0x16a>
mov    (%rbx),%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10d0 <strstr@plt>
mov    %rax,%r14
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1090 <lseek@plt>
mov    %eax,%ebp
mov    %ebx,%r13d
sub    %r14d,%r13d
add    %eax,%r13d
mov    %r13d,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     1329 <wfc_util_finsert_new_string+0x179>
mov    %rbp,0x10(%rsp)
mov    %rbx,0x8(%rsp)
mov    %r14,%rbp
add    $0xffffffffffffffff,%r14
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %r13d,%edx
call   10a0 <memset@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13d,%edx
call   10b0 <memcpy@plt>
mov    %r14d,%esi
mov    0x8(%rsp),%rbp
sub    %ebp,%esi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
call   1090 <lseek@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
add    %eax,%r14d
sub    %ebp,%r14d
mov    0x2d30(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    %r14d,%esi
call   1090 <lseek@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1050 <write@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,1),%esi
add    $0xffffffff,%esi
mov    %r12d,%edi
call   1080 <ftruncate@plt>
mov    %rbx,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>
lea    0xd05(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <putchar@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd01(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1302 <wfc_util_finsert_new_string+0x152>

LLVM IR (-O0):
@.str = [49 x i8] c"wfc_util_finsert_new_string : unvalid parameters\00"
@.str.1 = [2 x i8] c"\0A\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [49 x i8] c"wfc_util_finsert_new_string : can not malloc(%d)\00"
define void @wfc_util_finsert_new_string(i32 %0, i8** %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
store i32 0, i32* %10
store i8* null, i8** %11
store i8* null, i8** %12
store i8* null, i8** %13
store i8* null, i8** %14
%15 = load i32, i32* %5
%16 = icmp eq i32 0, %15
br i1 %16, label %24, label %17
17: ; preds = %4
%18 = load i8*, i8** %7
%19 = icmp eq i8* null, %18
br i1 %19, label %24, label %20
20: ; preds = %17
%21 = load i8*, i8** %7
%22 = call i32 @strlen(i8* %21)
%23 = icmp eq i32 0, %22
br i1 %23, label %24, label %27
24: ; preds = %20, %17, %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %114
27: ; preds = %20
%28 = load i8**, i8*** %6
%29 = icmp eq i8** null, %28
br i1 %29, label %30, label %31
30: ; preds = %27
br label %114
31: ; preds = %27
%32 = load i8**, i8*** %6
%33 = load i8*, i8** %32
store i8* %33, i8** %11
br label %34
34: ; preds = %31
%35 = load i8*, i8** %11
%36 = load i8*, i8** %8
%37 = call i8* @strstr(i8* %35, i8* %36)
store i8* %37, i8** %14
%38 = load i8*, i8** %14
%39 = getelementptr inbounds i8, i8* %38, i64 -1
store i8* %39, i8** %13
%40 = load i32, i32* %5
%41 = load i32, i32* @SEEK_END
%42 = call i32 @lseek(i32 %40, i32 0, i32 %41)
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = load i8*, i8** %14
%46 = load i8*, i8** %11
%47 = ptrtoint i8* %45 to i64
%48 = ptrtoint i8* %46 to i64
%49 = sub i64 %47, %48
%50 = sub nsw i64 %44, %49
%51 = trunc i64 %50 to i32
store i32 %51, i32* %10
%52 = load i32, i32* %10
%53 = call i8* @malloc(i32 %52)
store i8* %53, i8** %12
%54 = load i8*, i8** %12
%55 = icmp ne i8* null, %54
br i1 %55, label %56, label %109
56: ; preds = %34
%57 = load i8*, i8** %12
%58 = load i32, i32* %10
%59 = call i32 @memset(i8* %57, i32 0, i32 %58)
%60 = load i8*, i8** %12
%61 = load i8*, i8** %14
%62 = load i32, i32* %10
%63 = call i32 @memcpy(i8* %60, i8* %61, i32 %62)
%64 = load i32, i32* %5
%65 = load i8*, i8** %13
%66 = load i8*, i8** %11
%67 = ptrtoint i8* %65 to i64
%68 = ptrtoint i8* %66 to i64
%69 = sub i64 %67, %68
%70 = trunc i64 %69 to i32
%71 = load i32, i32* @SEEK_SET
%72 = call i32 @lseek(i32 %64, i32 %70, i32 %71)
%73 = load i32, i32* %5
%74 = load i8*, i8** %7
%75 = load i8*, i8** %7
%76 = call i32 @strlen(i8* %75)
%77 = call i32 @write(i32 %73, i8* %74, i32 %76)
%78 = load i8*, i8** %13
%79 = load i8*, i8** %7
%80 = call i32 @strlen(i8* %79)
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %78, i64 %81
store i8* %82, i8** %14
%83 = load i32, i32* %5
%84 = load i8*, i8** %14
%85 = load i8*, i8** %11
%86 = ptrtoint i8* %84 to i64
%87 = ptrtoint i8* %85 to i64
%88 = sub i64 %86, %87
%89 = trunc i64 %88 to i32
%90 = load i32, i32* @SEEK_SET
%91 = call i32 @lseek(i32 %83, i32 %89, i32 %90)
%92 = load i32, i32* %5
%93 = load i8*, i8** %12
%94 = load i32, i32* %10
%95 = call i32 @write(i32 %92, i8* %93, i32 %94)
%96 = load i32, i32* %5
%97 = load i32, i32* %9
%98 = load i8*, i8** %7
%99 = call i32 @strlen(i8* %98)
%100 = add nsw i32 %97, %99
%101 = sub nsw i32 %100, 1
%102 = call i32 @ftruncate(i32 %96, i32 %101)
%103 = load i8*, i8** %12
%104 = call i32 @free(i8* %103)
%105 = load i8**, i8*** %6
%106 = icmp ne i8** null, %105
br i1 %106, label %107, label %108
107: ; preds = %56
br label %108
108: ; preds = %107, %56
br label %113
109: ; preds = %34
%110 = load i32, i32* %10
%111 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %110)
%112 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %113
113: ; preds = %109, %108
br label %114
114: ; preds = %113, %30, %24
ret void
}

LLVM IR (-O2):
@.str = [49 x i8] c"wfc_util_finsert_new_string : unvalid parameters\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [49 x i8] c"wfc_util_finsert_new_string : can not malloc(%d)\00"
define void @wfc_util_finsert_new_string(i32 %0, i8** %1, i8* %2, i8* nocapture %3) {
%5 = icmp eq i32 %0, 0
%6 = icmp eq i8* %2, null
%7 = or i1 %5, %6
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i32 @strlen(i8* %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8, %4
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @putchar(i32 10)
br label %56
14: ; preds = %8
%15 = icmp eq i8** %1, null
br i1 %15, label %56, label %16
16: ; preds = %14
%17 = load i8*, i8** %1
%18 = tail call i8* @strstr(i8* dereferenceable(1) %17, i8* dereferenceable(1) %3)
%19 = getelementptr inbounds i8, i8* %18, i64 -1
%20 = load i32, i32* @SEEK_END
%21 = tail call i32 @lseek(i32 %0, i32 0, i32 %20)
%22 = ptrtoint i8* %18 to i64
%23 = ptrtoint i8* %17 to i64
%24 = sub i64 %23, %22
%25 = trunc i64 %24 to i32
%26 = add i32 %21, %25
%27 = tail call i8* @malloc(i32 %26)
%28 = icmp eq i8* %27, null
br i1 %28, label %53, label %29
29: ; preds = %16
%30 = tail call i32 @memset(i8* %27, i32 0, i32 %26)
%31 = tail call i32 @memcpy(i8* %27, i8* %18, i32 %26)
%32 = ptrtoint i8* %19 to i64
%33 = sub i64 %32, %23
%34 = trunc i64 %33 to i32
%35 = load i32, i32* @SEEK_SET
%36 = tail call i32 @lseek(i32 %0, i32 %34, i32 %35)
%37 = tail call i32 @strlen(i8* %2)
%38 = tail call i32 @write(i32 %0, i8* %2, i32 %37)
%39 = tail call i32 @strlen(i8* %2)
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %19, i64 %40
%42 = ptrtoint i8* %41 to i64
%43 = sub i64 %42, %23
%44 = trunc i64 %43 to i32
%45 = load i32, i32* @SEEK_SET
%46 = tail call i32 @lseek(i32 %0, i32 %44, i32 %45)
%47 = tail call i32 @write(i32 %0, i8* %27, i32 %26)
%48 = tail call i32 @strlen(i8* %2)
%49 = add i32 %21, -1
%50 = add i32 %49, %48
%51 = tail call i32 @ftruncate(i32 %0, i32 %50)
%52 = tail call i32 @free(i8* %27)
br label %56
53: ; preds = %16
%54 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%55 = tail call i32 @putchar(i32 10)
br label %56
56: ; preds = %29, %53, %14, %11
ret void
}