#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*/ uid_t;
struct stat {
  int dummy;
};
typedef int mode_t;
typedef int /*<<< orphan*/ gid_t;

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ chmod(char const *, int);
scalar_t__ chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fatal(char *, ...);
int /*<<< orphan*/ fatalx(char *, char const *);
char *format(char *, char const *);
int /*<<< orphan*/ *freopen(char const *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ getegid();
int /*<<< orphan*/ geteuid();
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ to_gid(char *);
int /*<<< orphan*/ to_uid(int /*<<< orphan*/ *);
int umask(int);
void open_report(const char *tag) {
  const char *path;
  struct stat st;
  uid_t uid;
  gid_t gid;
  mode_t mask;
  path = format("/var/crash_reports"
                "/"
                "crash_report"
                "__"
                "%s",
                tag);
  if (stat(path, &st) == 0 || errno != ENOENT)
    fatalx("%s: file already exists", path);
  mask = umask(~0200);
  if (freopen(path, "w", stdout) == NULL)
    fatal("%s: failed to redirect stdout to report file", path);
  (void)umask(mask);
  if (stat(path, &st) < 0)
    fatal("%s: failed to stat", path);
  uid = NULL == NULL ? geteuid() : to_uid(NULL);
  gid = "gr_crash_reports" == NULL ? getegid() : to_gid("gr_crash_reports");
  if (chown(path, uid, gid) < 0)
    fatal("%s: failed to change owner and group to %d:%d", path, NULL,
          "gr_crash_reports");
  if (chmod(path, 0640) < 0)
    fatal("%s: failed to set permissions to %o", 0640);
}


x86 assembly (clang, -O0):
<open_report>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe49(%rip),%rdi
call   1080 <format@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     11f1 <open_report+0x51>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
je     1201 <open_report+0x61>
mov    -0x10(%rbp),%rsi
lea    0xe28(%rip),%rdi
call   1030 <fatalx@plt>
mov    $0xffffff7f,%edi
call   1060 <umask@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
lea    0xe1a(%rip),%rsi
call   1050 <freopen@plt>
cmp    $0x0,%rax
jne    1243 <open_report+0xa3>
mov    -0x10(%rbp),%rsi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   10c0 <fatal@plt>
mov    -0x24(%rbp),%edi
call   1060 <umask@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    1274 <open_report+0xd4>
mov    -0x10(%rbp),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   10c0 <fatal@plt>
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %eax,-0x1c(%rbp)
lea    0xdf9(%rip),%rdi
call   10b0 <to_gid@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1090 <chown@plt>
cmp    $0x0,%rax
jge    12c3 <open_report+0x123>
mov    -0x10(%rbp),%rsi
lea    0xdde(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    0xdc2(%rip),%rcx
mov    $0x0,%al
call   10c0 <fatal@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1a0,%esi
call   10a0 <chmod@plt>
cmp    $0x0,%rax
jge    12ee <open_report+0x14e>
lea    0xddb(%rip),%rdi
mov    $0x1a0,%esi
mov    $0x0,%al
call   10c0 <fatal@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_report>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rsi
lea    0xe53(%rip),%rdi
call   1080 <format@plt>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
call   1070 <stat@plt>
test   %rax,%rax
je     11db <open_report+0x3b>
mov    0x2e14(%rip),%rax
mov    (%rax),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
je     11ea <open_report+0x4a>
lea    0xe42(%rip),%rdi
mov    %rbx,%rsi
call   1030 <fatalx@plt>
mov    $0xffffff7f,%edi
call   1060 <umask@plt>
mov    %eax,%ebp
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    0xe36(%rip),%rsi
mov    %rbx,%rdi
call   1050 <freopen@plt>
test   %rax,%rax
jne    1224 <open_report+0x84>
lea    0xe24(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   10c0 <fatal@plt>
mov    %ebp,%edi
call   1060 <umask@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %rax,%rax
jns    124c <open_report+0xac>
lea    0xe29(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   10c0 <fatal@plt>
xor    %eax,%eax
call   1040 <geteuid@plt>
mov    %eax,%ebp
lea    0xe22(%rip),%rdi
call   10b0 <to_gid@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1090 <chown@plt>
test   %rax,%rax
jns    128c <open_report+0xec>
lea    0xe16(%rip),%rdi
lea    0xdfe(%rip),%rcx
mov    %rbx,%rsi
xor    %edx,%edx
xor    %eax,%eax
call   10c0 <fatal@plt>
mov    %rbx,%rdi
mov    $0x1a0,%esi
call   10a0 <chmod@plt>
test   %rax,%rax
jns    12b1 <open_report+0x111>
lea    0xe18(%rip),%rdi
mov    $0x1a0,%esi
xor    %eax,%eax
call   10c0 <fatal@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [36 x i8] c"/var/crash_reports/crash_report__%s\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [24 x i8] c"%s: file already exists\00"
@.str.2 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.3 = [45 x i8] c"%s: failed to redirect stdout to report file\00"
@.str.4 = [19 x i8] c"%s: failed to stat\00"
@.str.5 = [17 x i8] c"gr_crash_reports\00"
@.str.6 = [46 x i8] c"%s: failed to change owner and group to %d:%d\00"
@.str.7 = [36 x i8] c"%s: failed to set permissions to %o\00"
define void @open_report(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i8* @format(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %8)
store i8* %9, i8** %3
%10 = load i8*, i8** %3
%11 = call i64 @stat(i8* %10, %struct.stat* %4)
%12 = icmp eq i64 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %1
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp ne i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %13, %1
%18 = load i8*, i8** %3
%19 = call i32 @fatalx(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %13
%21 = call i32 @umask(i32 -129)
store i32 %21, i32* %7
%22 = load i8*, i8** %3
%23 = load i32, i32* @stdout
%24 = call i32* @freopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = load i8*, i8** %3
%28 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %26, %20
%30 = load i32, i32* %7
%31 = call i32 @umask(i32 %30)
%32 = load i8*, i8** %3
%33 = call i64 @stat(i8* %32, %struct.stat* %4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i8*, i8** %3
%37 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %35, %29
%39 = call i32 (...) @geteuid()
store i32 %39, i32* %5
%40 = call i32 @to_gid(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
store i32 %40, i32* %6
%41 = load i8*, i8** %3
%42 = load i32, i32* %5
%43 = load i32, i32* %6
%44 = call i64 @chown(i8* %41, i32 %42, i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = load i8*, i8** %3
%48 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0), i8* %47, i8* null, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %46, %38
%50 = load i8*, i8** %3
%51 = call i64 @chmod(i8* %50, i32 416)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i32 416)
br label %55
55: ; preds = %53, %49
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [36 x i8] c"/var/crash_reports/crash_report__%s\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [24 x i8] c"%s: file already exists\00"
@.str.2 = [2 x i8] c"w\00"
@stdout = global i32 0
@.str.3 = [45 x i8] c"%s: failed to redirect stdout to report file\00"
@.str.4 = [19 x i8] c"%s: failed to stat\00"
@.str.5 = [17 x i8] c"gr_crash_reports\00"
@.str.6 = [46 x i8] c"%s: failed to change owner and group to %d:%d\00"
@.str.7 = [36 x i8] c"%s: failed to set permissions to %o\00"
define void @open_report(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i8* @format(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i64 @stat(i8* %4, %struct.stat* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = load i64, i64* @errno
%9 = load i64, i64* @ENOENT
%10 = icmp eq i64 %8, %9
br i1 %10, label %13, label %11
11: ; preds = %7, %1
%12 = call i32 @fatalx(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %13
13: ; preds = %11, %7
%14 = call i32 @umask(i32 -129)
%15 = load i32, i32* @stdout
%16 = call i32* @freopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i8* %4)
br label %20
20: ; preds = %18, %13
%21 = call i32 @umask(i32 %14)
%22 = call i64 @stat(i8* %4, %struct.stat* %2)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %4)
br label %26
26: ; preds = %24, %20
%27 = call i32 (...) @geteuid()
%28 = call i32 @to_gid(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%29 = call i64 @chown(i8* %4, i32 %27, i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.6, i64 0, i64 0), i8* %4, i8* null, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = call i64 @chmod(i8* %4, i32 416)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i32 416)
br label %38
38: ; preds = %36, %33
ret void
}