#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memmove(char *, char *, size_t);
size_t strlen(char const *);
char *strstr(char const *, char *);
int InsertInPrefsFile(const char *path, const char *filebuf, size_t filebuflen,
                      const char *entry) {
  FILE *f;
  size_t entrylen;
  char *insertat = strstr(filebuf, "</dict>");
  if (insertat == NULL || insertat <= filebuf)
    return -1;
  entrylen = strlen(entry);
  memmove(insertat + entrylen, insertat, entrylen);
  memcpy(insertat, entry, entrylen);
  f = fopen(path, "wb");
  if (f == NULL)
    return -1;
  fprintf(f, "%s", filebuf);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<InsertInPrefsFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe6d(%rip),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     11b5 <InsertInPrefsFile+0x45>
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     11c1 <InsertInPrefsFile+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <InsertInPrefsFile+0xd3>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rdi
add    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1070 <memmove@plt>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rdi
lea    0xe09(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121f <InsertInPrefsFile+0xaf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <InsertInPrefsFile+0xd3>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xddd(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<InsertInPrefsFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
lea    0xe75(%rip),%rsi
mov    %r13,%rdi
call   1090 <strstr@plt>
mov    $0xffffffff,%r15d
cmp    %r13,%rax
jbe    11f8 <InsertInPrefsFile+0x88>
mov    %rax,%rbp
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
add    %rax,%rdi
mov    %rbp,%rsi
mov    %rax,%rdx
call   1070 <memmove@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
lea    0xe36(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11f8 <InsertInPrefsFile+0x88>
mov    %rax,%rbp
mov    %r13,%rdi
mov    %rax,%rsi
call   1050 <fputs@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"</dict>\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @InsertInPrefsFile(i8* %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i64
%12 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%13 = load i8*, i8** %7
%14 = call i8* @strstr(i8* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %12
%15 = load i8*, i8** %12
%16 = icmp eq i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %4
%18 = load i8*, i8** %12
%19 = load i8*, i8** %7
%20 = icmp ule i8* %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17, %4
store i32 -1, i32* %5
br label %46
22: ; preds = %17
%23 = load i8*, i8** %9
%24 = call i64 @strlen(i8* %23)
store i64 %24, i64* %11
%25 = load i8*, i8** %12
%26 = load i64, i64* %11
%27 = getelementptr inbounds i8, i8* %25, i64 %26
%28 = load i8*, i8** %12
%29 = load i64, i64* %11
%30 = call i32 @memmove(i8* %27, i8* %28, i64 %29)
%31 = load i8*, i8** %12
%32 = load i8*, i8** %9
%33 = load i64, i64* %11
%34 = call i32 @memcpy(i8* %31, i8* %32, i64 %33)
%35 = load i8*, i8** %6
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %36, i32** %10
%37 = load i32*, i32** %10
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %40
39: ; preds = %22
store i32 -1, i32* %5
br label %46
40: ; preds = %22
%41 = load i32*, i32** %10
%42 = load i8*, i8** %7
%43 = call i32 @fprintf(i32* %41, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = load i32*, i32** %10
%45 = call i32 @fclose(i32* %44)
store i32 0, i32* %5
br label %46
46: ; preds = %40, %39, %21
%47 = load i32, i32* %5
ret i32 %47
}

LLVM IR (-O2):
@.str = [8 x i8] c"</dict>\00"
@.str.1 = [3 x i8] c"wb\00"
define i32 @InsertInPrefsFile(i8* nocapture %0, i8* %1, i64 %2, i8* %3) {
%5 = tail call i8* @strstr(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%6 = icmp ugt i8* %5, %1
br i1 %6, label %7, label %17
7: ; preds = %4
%8 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%9 = getelementptr inbounds i8, i8* %5, i64 %8
%10 = tail call i32 @memmove(i8* %9, i8* %5, i64 %8)
%11 = tail call i32 @memcpy(i8* %5, i8* %3, i64 %8)
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %7
%15 = tail call i32 @fputs(i8* %1, i32* %12)
%16 = tail call i32 @fclose(i32* %12)
br label %17
17: ; preds = %7, %4, %14
%18 = phi i32 [ 0, %14 ], [ -1, %4 ], [ -1, %7 ]
ret i32 %18
}