#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 */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, int, int);
int unlink(char *);
int write_default_file(char *dflfile, int vid, int svid) {
  FILE *fh;
  if (vid == -1) {
    int result;
    result = unlink(dflfile);
    if (!result || (errno == ENOENT))
      return 0;
    else
      return result;
  }
  fh = fopen(dflfile, "w");
  if (!fh)
    return -1;
  if ((fprintf(fh, "%i.%i\n", vid, svid) < 0)) {
    fclose(fh);
    return -1;
  }
  fclose(fh);
  return 0;
}


x86 assembly (clang, -O0):
<write_default_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11a3 <write_default_file+0x63>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     118c <write_default_file+0x4c>
mov    0x2e67(%rip),%rax
mov    (%rax),%rax
mov    0x2e4d(%rip),%rcx
cmp    (%rcx),%rax
jne    1198 <write_default_file+0x58>
movl   $0x0,-0x4(%rbp)
jmp    1213 <write_default_file+0xd3>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <write_default_file+0xd3>
mov    -0x10(%rbp),%rdi
lea    0xe52(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ce <write_default_file+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <write_default_file+0xd3>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe23(%rip),%rsi
call   1050 <fprintf@plt>
cmp    $0x0,%rax
jge    1203 <write_default_file+0xc3>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <write_default_file+0xd3>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_default_file>:
push   %rbp
push   %r14
push   %rbx
cmp    $0xffffffff,%esi
je     1185 <write_default_file+0x45>
mov    %edx,%ebx
mov    %esi,%ebp
lea    0xeac(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ab <write_default_file+0x6b>
mov    %rax,%r14
lea    0xe9a(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
mov    %ebx,%ecx
call   1050 <fprintf@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <fclose@plt>
sar    $0x3f,%rbx
jmp    11b4 <write_default_file+0x74>
call   1030 <unlink@plt>
test   %eax,%eax
je     11b2 <write_default_file+0x72>
mov    %eax,%ebx
mov    0x2e49(%rip),%rax
mov    (%rax),%rax
mov    0x2e2f(%rip),%rcx
xor    %edx,%edx
cmp    (%rcx),%rax
cmove  %edx,%ebx
jmp    11b4 <write_default_file+0x74>
mov    $0xffffffff,%ebx
jmp    11b4 <write_default_file+0x74>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ENOENT = global i64 0
@.str = [2 x i8] c"w\00"
@.str.1 = [7 x i8] c"%i.%i\0A\00"
define i32 @write_default_file(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %24
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = call i32 @unlink(i8* %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOENT
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %17, %12
store i32 0, i32* %4
br label %42
22: ; preds = %17
%23 = load i32, i32* %9
store i32 %23, i32* %4
br label %42
24: ; preds = %3
%25 = load i8*, i8** %5
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %26, i32** %8
%27 = load i32*, i32** %8
%28 = icmp ne i32* %27, null
br i1 %28, label %30, label %29
29: ; preds = %24
store i32 -1, i32* %4
br label %42
30: ; preds = %24
%31 = load i32*, i32** %8
%32 = load i32, i32* %6
%33 = load i32, i32* %7
%34 = call i64 @fprintf(i32* %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %32, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = load i32*, i32** %8
%38 = call i32 @fclose(i32* %37)
store i32 -1, i32* %4
br label %42
39: ; preds = %30
%40 = load i32*, i32** %8
%41 = call i32 @fclose(i32* %40)
store i32 0, i32* %4
br label %42
42: ; preds = %39, %36, %29, %22, %21
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@errno = global i64 0
@ENOENT = global i64 0
@.str = [2 x i8] c"w\00"
@.str.1 = [7 x i8] c"%i.%i\0A\00"
define i32 @write_default_file(i8* nocapture %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %1, -1
br i1 %4, label %5, label %13
5: ; preds = %3
%6 = tail call i32 @unlink(i8* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %21, label %8
8: ; preds = %5
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
%12 = select i1 %11, i32 0, i32 %6
br label %21
13: ; preds = %3
%14 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %21, label %16
16: ; preds = %13
%17 = tail call i64 @fprintf(i32* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %2)
%18 = tail call i32 @fclose(i32* %14)
%19 = ashr i64 %17, 63
%20 = trunc i64 %19 to i32
br label %21
21: ; preds = %16, %8, %13, %5
%22 = phi i32 [ 0, %5 ], [ -1, %13 ], [ %12, %8 ], [ %20, %16 ]
ret i32 %22
}