#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*/ buffer;

/* Variables and functions */
unsigned int CON_LogRead(char *, int);
int /*<<< orphan*/ Com_Printf(char *, char const *);
char *Cvar_VariableString(char *);
char *FS_BuildOSPath(char const *, char const *, char const *);
scalar_t__ FS_CreatePath(char *);
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ Sys_Print(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ va(char *, char const *);
unsigned int write(int, char *, unsigned int);
void Sys_ErrorDialog(const char *error) {
  char buffer[1024];
  unsigned int size;
  int f = -1;
  const char *homepath = Cvar_VariableString("fs_homepath");
  const char *gamedir = Cvar_VariableString("fs_gamedir");
  const char *fileName = "crashlog.txt";
  char *ospath = FS_BuildOSPath(homepath, gamedir, fileName);
  Sys_Print(va("%s\n", error));
  if (FS_CreatePath(ospath)) {
    Com_Printf("ERROR: couldn't create path '%s' for crash log.\n", ospath);
    return;
  }
  f = open(ospath, O_CREAT | O_TRUNC | O_WRONLY, 0640);
  if (f == -1) {
    Com_Printf("ERROR: couldn't open %s\n", fileName);
    return;
  }
  while ((size = CON_LogRead(buffer, sizeof(buffer))) > 0) {
    if (write(f, buffer, size) != size) {
      Com_Printf("ERROR: couldn't fully write to %s\n", fileName);
      break;
    }
  }
  close(f);
}


x86 assembly (clang, -O0):
<Sys_ErrorDialog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0x418(%rbp)
ff ff ff
lea    0xe40(%rip),%rdi
call   10c0 <Cvar_VariableString@plt>
mov    %rax,-0x420(%rbp)
lea    0xe39(%rip),%rdi
call   10c0 <Cvar_VariableString@plt>
mov    %rax,-0x428(%rbp)
lea    0xe31(%rip),%rax
mov    %rax,-0x430(%rbp)
mov    -0x420(%rbp),%rdi
mov    -0x428(%rbp),%rsi
mov    -0x430(%rbp),%rdx
call   1070 <FS_BuildOSPath@plt>
mov    %rax,-0x438(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe51(%rip),%rdi
call   1090 <va@plt>
mov    %eax,%edi
call   1040 <Sys_Print@plt>
mov    -0x438(%rbp),%rdi
call   1060 <FS_CreatePath@plt>
cmp    $0x0,%rax
je     1253 <Sys_ErrorDialog+0xb3>
mov    -0x438(%rbp),%rsi
lea    0xddb(%rip),%rdi
call   1080 <Com_Printf@plt>
jmp    131b <Sys_ErrorDialog+0x17b>
mov    -0x438(%rbp),%rdi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
or     (%rax),%esi
mov    0x2d55(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
call   10a0 <open@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0xffffffff,-0x418(%rbp)
jne    12aa <Sys_ErrorDialog+0x10a>
mov    -0x430(%rbp),%rsi
lea    0xdb5(%rip),%rdi
call   1080 <Com_Printf@plt>
jmp    131b <Sys_ErrorDialog+0x17b>
jmp    12af <Sys_ErrorDialog+0x10f>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10b0 <CON_LogRead@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jbe    1310 <Sys_ErrorDialog+0x170>
mov    -0x418(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
cmp    -0x414(%rbp),%eax
je     130b <Sys_ErrorDialog+0x16b>
mov    -0x430(%rbp),%rsi
lea    0xd6d(%rip),%rdi
call   1080 <Com_Printf@plt>
jmp    1310 <Sys_ErrorDialog+0x170>
jmp    12af <Sys_ErrorDialog+0x10f>
mov    -0x418(%rbp),%edi
call   1050 <close@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Sys_ErrorDialog>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rbp
lea    0xe4b(%rip),%rdi
call   10c0 <Cvar_VariableString@plt>
mov    %rax,%rbx
lea    0xe48(%rip),%rdi
call   10c0 <Cvar_VariableString@plt>
lea    0xe47(%rip),%rdx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <FS_BuildOSPath@plt>
mov    %rax,%rbx
lea    0xe85(%rip),%rdi
mov    %rbp,%rsi
call   1090 <va@plt>
mov    %eax,%edi
call   1040 <Sys_Print@plt>
mov    %rbx,%rdi
call   1060 <FS_CreatePath@plt>
test   %rax,%rax
je     121b <Sys_ErrorDialog+0x7b>
lea    0xe1c(%rip),%rdi
mov    %rbx,%rsi
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <Com_Printf@plt>
mov    0x2dd6(%rip),%rax
mov    0x2dc7(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d94(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a0,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     129b <Sys_ErrorDialog+0xfb>
mov    %eax,%r14d
mov    %rsp,%rbx
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x400,%esi
call   10b0 <CON_LogRead@plt>
test   %eax,%eax
je     1287 <Sys_ErrorDialog+0xe7>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1250 <Sys_ErrorDialog+0xb0>
lea    0xdf3(%rip),%rdi
lea    0xd95(%rip),%rsi
call   1080 <Com_Printf@plt>
mov    %r14d,%edi
call   1050 <close@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb3(%rip),%rdi
lea    0xd6e(%rip),%rsi
jmp    120b <Sys_ErrorDialog+0x6b>

LLVM IR (-O0):
@.str = [12 x i8] c"fs_homepath\00"
@.str.1 = [11 x i8] c"fs_gamedir\00"
@.str.2 = [13 x i8] c"crashlog.txt\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [49 x i8] c"ERROR: couldn't create path '%s' for crash log.\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@.str.5 = [25 x i8] c"ERROR: couldn't open %s\0A\00"
@.str.6 = [35 x i8] c"ERROR: couldn't fully write to %s\0A\00"
define void @Sys_ErrorDialog(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %2
store i32 -1, i32* %5
%10 = call i8* @Cvar_VariableString(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %6
%11 = call i8* @Cvar_VariableString(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i8* %11, i8** %7
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8** %8
%12 = load i8*, i8** %6
%13 = load i8*, i8** %7
%14 = load i8*, i8** %8
%15 = call i8* @FS_BuildOSPath(i8* %12, i8* %13, i8* %14)
store i8* %15, i8** %9
%16 = load i8*, i8** %2
%17 = call i32 @va(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%18 = call i32 @Sys_Print(i32 %17)
%19 = load i8*, i8** %9
%20 = call i64 @FS_CreatePath(i8* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %1
%23 = load i8*, i8** %9
%24 = call i32 @Com_Printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %23)
br label %57
25: ; preds = %1
%26 = load i8*, i8** %9
%27 = load i32, i32* @O_CREAT
%28 = load i32, i32* @O_TRUNC
%29 = or i32 %27, %28
%30 = load i32, i32* @O_WRONLY
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %26, i32 %31, i32 416)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = load i8*, i8** %8
%37 = call i32 @Com_Printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %36)
br label %57
38: ; preds = %25
br label %39
39: ; preds = %53, %38
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%41 = call i32 @CON_LogRead(i8* %40, i32 1024)
store i32 %41, i32* %4
%42 = icmp ugt i32 %41, 0
br i1 %42, label %43, label %54
43: ; preds = %39
%44 = load i32, i32* %5
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%46 = load i32, i32* %4
%47 = call i32 @write(i32 %44, i8* %45, i32 %46)
%48 = load i32, i32* %4
%49 = icmp ne i32 %47, %48
br i1 %49, label %50, label %53
50: ; preds = %43
%51 = load i8*, i8** %8
%52 = call i32 @Com_Printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i8* %51)
br label %54
53: ; preds = %43
br label %39
54: ; preds = %50, %39
%55 = load i32, i32* %5
%56 = call i32 @close(i32 %55)
br label %57
57: ; preds = %54, %35, %22
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"fs_homepath\00"
@.str.1 = [11 x i8] c"fs_gamedir\00"
@.str.2 = [13 x i8] c"crashlog.txt\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [49 x i8] c"ERROR: couldn't create path '%s' for crash log.\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@.str.5 = [25 x i8] c"ERROR: couldn't open %s\0A\00"
@.str.6 = [35 x i8] c"ERROR: couldn't fully write to %s\0A\00"
define void @Sys_ErrorDialog(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @Cvar_VariableString(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = tail call i8* @Cvar_VariableString(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i8* @FS_BuildOSPath(i8* %4, i8* %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%7 = tail call i32 @va(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%8 = tail call i32 @Sys_Print(i32 %7)
%9 = tail call i64 @FS_CreatePath(i8* %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %1
%12 = tail call i32 @Com_Printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %33
13: ; preds = %1
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %15, %14
%17 = load i32, i32* @O_WRONLY
%18 = or i32 %16, %17
%19 = tail call i32 @open(i8* %6, i32 %18, i32 416)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %13
%22 = tail call i32 @Com_Printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %33
23: ; preds = %13, %26
%24 = call i32 @CON_LogRead(i8* %3, i32 1024)
%25 = icmp eq i32 %24, 0
br i1 %25, label %31, label %26
26: ; preds = %23
%27 = call i32 @write(i32 %19, i8* %3, i32 %24)
%28 = icmp eq i32 %27, %24
br i1 %28, label %23, label %29
29: ; preds = %26
%30 = call i32 @Com_Printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %23, %29
%32 = call i32 @close(i32 %19)
br label %33
33: ; preds = %31, %21, %11
ret void
}