#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(int /*<<< orphan*/, 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_Gettext(char *);
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(Sys_Gettext("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(Sys_Gettext("ERROR: couldn't open %s\n"), fileName);
    return;
  }
  while ((size = CON_LogRead(buffer, sizeof(buffer))) > 0) {
    if (write(f, buffer, size) != size) {
      Com_Printf(Sys_Gettext("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    0xe30(%rip),%rdi
call   10d0 <Cvar_VariableString@plt>
mov    %rax,-0x420(%rbp)
lea    0xe29(%rip),%rdi
call   10d0 <Cvar_VariableString@plt>
mov    %rax,-0x428(%rbp)
lea    0xe21(%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    0xe41(%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     126a <Sys_ErrorDialog+0xba>
lea    0xdd2(%rip),%rdi
call   10b0 <Sys_Gettext@plt>
mov    %eax,%edi
mov    -0x438(%rbp),%rsi
call   1080 <Com_Printf@plt>
jmp    1340 <Sys_ErrorDialog+0x190>
mov    -0x438(%rbp),%rdi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    0x2d6f(%rip),%rax
or     (%rax),%esi
mov    0x2d3e(%rip),%rax
or     (%rax),%esi
mov    $0x1a0,%edx
call   10a0 <open@plt>
mov    %eax,-0x418(%rbp)
cmpl   $0xffffffff,-0x418(%rbp)
jne    12c8 <Sys_ErrorDialog+0x118>
lea    0xda5(%rip),%rdi
call   10b0 <Sys_Gettext@plt>
mov    %eax,%edi
mov    -0x430(%rbp),%rsi
call   1080 <Com_Printf@plt>
jmp    1340 <Sys_ErrorDialog+0x190>
jmp    12cd <Sys_ErrorDialog+0x11d>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   10c0 <CON_LogRead@plt>
mov    %eax,-0x414(%rbp)
cmp    $0x0,%eax
jbe    1335 <Sys_ErrorDialog+0x185>
mov    -0x418(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1030 <write@plt>
cmp    -0x414(%rbp),%eax
je     1330 <Sys_ErrorDialog+0x180>
lea    0xd56(%rip),%rdi
call   10b0 <Sys_Gettext@plt>
mov    %eax,%edi
mov    -0x430(%rbp),%rsi
call   1080 <Com_Printf@plt>
jmp    1335 <Sys_ErrorDialog+0x185>
jmp    12cd <Sys_ErrorDialog+0x11d>
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    0xe3b(%rip),%rdi
call   10d0 <Cvar_VariableString@plt>
mov    %rax,%rbx
lea    0xe38(%rip),%rdi
call   10d0 <Cvar_VariableString@plt>
lea    0xe37(%rip),%rdx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <FS_BuildOSPath@plt>
mov    %rax,%rbx
lea    0xe75(%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     1232 <Sys_ErrorDialog+0x82>
lea    0xe0c(%rip),%rdi
call   10b0 <Sys_Gettext@plt>
mov    %eax,%edi
mov    %rbx,%rsi
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1080 <Com_Printf@plt>
mov    0x2dbf(%rip),%rax
mov    0x2db0(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d7d(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a0,%edx
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12c2 <Sys_ErrorDialog+0x112>
mov    %eax,%r14d
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x400,%esi
call   10c0 <CON_LogRead@plt>
test   %eax,%eax
je     12ae <Sys_ErrorDialog+0xfe>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     1270 <Sys_ErrorDialog+0xc0>
lea    0xdd3(%rip),%rdi
call   10b0 <Sys_Gettext@plt>
lea    0xd70(%rip),%rsi
mov    %eax,%edi
call   1080 <Com_Printf@plt>
mov    %r14d,%edi
call   1050 <close@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd8c(%rip),%rdi
call   10b0 <Sys_Gettext@plt>
lea    0xd42(%rip),%rsi
mov    %eax,%edi
jmp    1222 <Sys_ErrorDialog+0x72>

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 %26
22: ; preds = %1
%23 = call i32 @Sys_Gettext(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
%24 = load i8*, i8** %9
%25 = call i32 @Com_Printf(i32 %23, i8* %24)
br label %60
26: ; preds = %1
%27 = load i8*, i8** %9
%28 = load i32, i32* @O_CREAT
%29 = load i32, i32* @O_TRUNC
%30 = or i32 %28, %29
%31 = load i32, i32* @O_WRONLY
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %27, i32 %32, i32 416)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %26
%37 = call i32 @Sys_Gettext(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%38 = load i8*, i8** %8
%39 = call i32 @Com_Printf(i32 %37, i8* %38)
br label %60
40: ; preds = %26
br label %41
41: ; preds = %56, %40
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%43 = call i32 @CON_LogRead(i8* %42, i32 1024)
store i32 %43, i32* %4
%44 = icmp ugt i32 %43, 0
br i1 %44, label %45, label %57
45: ; preds = %41
%46 = load i32, i32* %5
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%48 = load i32, i32* %4
%49 = call i32 @write(i32 %46, i8* %47, i32 %48)
%50 = load i32, i32* %4
%51 = icmp ne i32 %49, %50
br i1 %51, label %52, label %56
52: ; preds = %45
%53 = call i32 @Sys_Gettext(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%54 = load i8*, i8** %8
%55 = call i32 @Com_Printf(i32 %53, i8* %54)
br label %57
56: ; preds = %45
br label %41
57: ; preds = %52, %41
%58 = load i32, i32* %5
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57, %36, %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 %14, label %11
11: ; preds = %1
%12 = tail call i32 @Sys_Gettext(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0))
%13 = tail call i32 @Com_Printf(i32 %12, i8* %6)
br label %36
14: ; preds = %1
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %16, %15
%18 = load i32, i32* @O_WRONLY
%19 = or i32 %17, %18
%20 = tail call i32 @open(i8* %6, i32 %19, i32 416)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = tail call i32 @Sys_Gettext(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%24 = tail call i32 @Com_Printf(i32 %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %36
25: ; preds = %14, %28
%26 = call i32 @CON_LogRead(i8* %3, i32 1024)
%27 = icmp eq i32 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %25
%29 = call i32 @write(i32 %20, i8* %3, i32 %26)
%30 = icmp eq i32 %29, %26
br i1 %30, label %25, label %31
31: ; preds = %28
%32 = call i32 @Sys_Gettext(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%33 = call i32 @Com_Printf(i32 %32, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %25, %31
%35 = call i32 @close(i32 %20)
br label %36
36: ; preds = %34, %22, %11
ret void
}