#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 fwrite(char const *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ log_add(int, int /*<<< orphan*/ *, int, char *,
                           char const *);
int config_write(const char *filename, const char *buf, int length, int start,
                 int end, const char *row) {
  FILE *cfg;
  int a, b;
  cfg = fopen(filename, "w");
  if (cfg == NULL) {
    log_add(-1, NULL, 0x00000002, "config_write: Couldn't open `%s'.",
            filename);
    return -1;
  }
  a = fwrite(buf, 1, start, cfg);
  if (row)
    fprintf(cfg, "%s\n", row);
  b = fwrite(buf + end, 1, length - end, cfg);
  fclose(cfg);
  return 0;
}


x86 assembly (clang, -O0):
<config_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe87(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11b7 <config_write+0x67>
mov    -0x10(%rbp),%r8
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x2,%edx
lea    0xe5c(%rip),%rcx
call   1050 <log_add@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1220 <config_write+0xd0>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11ee <config_write+0x9e>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xe3b(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x1c(%rbp),%edx
sub    -0x24(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<config_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r12
mov    %r8d,0x4(%rsp)
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r13
lea    0xe8a(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11d7 <config_write+0x87>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
test   %r12,%r12
je     11ad <config_write+0x5d>
lea    0xe82(%rip),%rsi
mov    %rbp,%rdi
mov    %r12,%rdx
call   1040 <fprintf@plt>
movslq 0x4(%rsp),%rax
add    %rax,%rbx
sub    %eax,%r14d
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbp,%rcx
call   1070 <fwrite@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
jmp    11f7 <config_write+0xa7>
lea    0xe24(%rip),%rcx
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
xor    %esi,%esi
mov    $0x2,%edx
mov    %r13,%r8
call   1050 <log_add@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [34 x i8] c"config_write: Couldn't open `%s'.\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @config_write(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4, i8* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i8* %5, i8** %13
%17 = load i8*, i8** %8
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %14
%19 = load i32*, i32** %14
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %6
%22 = load i8*, i8** %8
%23 = call i32 @log_add(i32 -1, i32* null, i32 2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 -1, i32* %7
br label %47
24: ; preds = %6
%25 = load i8*, i8** %9
%26 = load i32, i32* %11
%27 = load i32*, i32** %14
%28 = call i32 @fwrite(i8* %25, i32 1, i32 %26, i32* %27)
store i32 %28, i32* %15
%29 = load i8*, i8** %13
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = load i32*, i32** %14
%33 = load i8*, i8** %13
%34 = call i32 @fprintf(i32* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %31, %24
%36 = load i8*, i8** %9
%37 = load i32, i32* %12
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i32, i32* %10
%41 = load i32, i32* %12
%42 = sub nsw i32 %40, %41
%43 = load i32*, i32** %14
%44 = call i32 @fwrite(i8* %39, i32 1, i32 %42, i32* %43)
store i32 %44, i32* %16
%45 = load i32*, i32** %14
%46 = call i32 @fclose(i32* %45)
store i32 0, i32* %7
br label %47
47: ; preds = %35, %21
%48 = load i32, i32* %7
ret i32 %48
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [34 x i8] c"config_write: Couldn't open `%s'.\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @config_write(i8* %0, i8* nocapture %1, i32 %2, i32 %3, i32 %4, i8* %5) {
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @log_add(i32 -1, i32* null, i32 2, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
11: ; preds = %6
%12 = tail call i32 @fwrite(i8* %1, i32 1, i32 %3, i32* %7)
%13 = icmp eq i8* %5, null
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %16
16: ; preds = %14, %11
%17 = sext i32 %4 to i64
%18 = getelementptr inbounds i8, i8* %1, i64 %17
%19 = sub nsw i32 %2, %4
%20 = tail call i32 @fwrite(i8* %18, i32 1, i32 %19, i32* %7)
%21 = tail call i32 @fclose(i32* %7)
br label %22
22: ; preds = %16, %9
%23 = phi i32 [ -1, %9 ], [ 0, %16 ]
ret i32 %23
}