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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ logging(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int writeFile(char *str, int size, char *file) {
  FILE *fp;
  if ((fp = fopen(file, "w")) < 0) {
    char logmsg[128];
    snprintf(logmsg, sizeof(logmsg), "writefile: Failed to open file: %s\n",
             file);
    logging("lsrp-router.log", logmsg);
    return -1;
  }
  if ((fwrite(str, 1, size, fp)) < 0) {
    char logmsg[128];
    snprintf(logmsg, sizeof(logmsg), "writefile: Failed to write file %s.",
             file);
    logging("lsrp-router.log", logmsg);
    return -1;
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<writeFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jae    11bf <writeFile+0x6f>
lea    -0xb0(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x80,%esi
lea    0xe67(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0xb0(%rbp),%rsi
lea    0xe78(%rip),%rdi
call   1050 <logging@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <writeFile+0xd9>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
cmp    $0x0,%rax
jge    1219 <writeFile+0xc9>
lea    -0x130(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x80,%esi
lea    0xe41(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x130(%rbp),%rsi
lea    0xe1e(%rip),%rdi
call   1050 <logging@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1229 <writeFile+0xd9>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbp
lea    0xe93(%rip),%rsi
mov    %rdx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
test   %rax,%rax
js     119c <writeFile+0x4c>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11ca <writeFile+0x7a>
lea    0xe6f(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
lea    0xe45(%rip),%rdi
mov    %rbx,%rsi
call   1050 <logging@plt>
mov    $0xffffffff,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [36 x i8] c"writefile: Failed to open file: %s\0A\00"
@.str.2 = [16 x i8] c"lsrp-router.log\00"
@.str.3 = [36 x i8] c"writefile: Failed to write file %s.\00"
define i32 @writeFile(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca [128 x i8]
%10 = alloca [128 x i8]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i8*, i8** %7
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = icmp ult i32* %12, null
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%16 = load i8*, i8** %7
%17 = call i32 @snprintf(i8* %15, i32 128, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%19 = call i32 @logging(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %18)
store i32 -1, i32* %4
br label %35
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = load i32, i32* %6
%23 = load i32*, i32** %8
%24 = call i64 @fwrite(i8* %21, i32 1, i32 %22, i32* %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %20
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%28 = load i8*, i8** %7
%29 = call i32 @snprintf(i8* %27, i32 128, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %10, i64 0, i64 0
%31 = call i32 @logging(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %30)
store i32 -1, i32* %4
br label %35
32: ; preds = %20
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
store i32 0, i32* %4
br label %35
35: ; preds = %32, %26, %14
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.2 = [16 x i8] c"lsrp-router.log\00"
@.str.3 = [36 x i8] c"writefile: Failed to write file %s.\00"
define i32 @writeFile(i8* nocapture %0, i32 %1, i8* %2) {
%4 = alloca [128 x i8]
%5 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = tail call i64 @fwrite(i8* %0, i32 1, i32 %1, i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %3
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%10 = call i32 @snprintf(i8* %9, i32 128, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%11 = call i32 @logging(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %14
12: ; preds = %3
%13 = tail call i32 @fclose(i32* %5)
br label %14
14: ; preds = %12, %8
%15 = phi i32 [ -1, %8 ], [ 0, %12 ]
ret i32 %15
}