#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 */
int /*<<< orphan*/ do_exit(int);
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char const *,
                           ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void msg_and_exit(int code, char const *msg) {
  FILE *f = NULL;
  if (!(f = fopen("./error", "w"))) {
    fprintf(stderr, "%s: cannot open %s for writing: %s\n", "dosrun2",
            "./error", strerror(errno));
    do_exit(100);
  }
  fprintf(f, "%s: %s\r\n", "dosrun2", msg);
  if (ferror(f)) {
    fprintf(stderr, "%s: i/o error on %s: %s\n", "dosrun2", "./error",
            strerror(errno));
    do_exit(100);
  }
  if (fclose(f) < 0) {
    fprintf(stderr, "%s: i/o error on %s: %s\n", "dosrun2", "./error",
            strerror(errno));
    do_exit(100);
  }
  do_exit(code);
}


x86 assembly (clang, -O0):
<msg_and_exit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xe82(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11de <msg_and_exit+0x7e>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%r8
lea    0xe4b(%rip),%rsi
lea    0xe68(%rip),%rdx
lea    0xe33(%rip),%rcx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x64,%edi
call   1040 <do_exit@plt>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    -0x10(%rbp),%rcx
lea    0xe47(%rip),%rsi
lea    0xe38(%rip),%rdx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     1256 <msg_and_exit+0xf6>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%r8
lea    0xe08(%rip),%rsi
lea    0xdf0(%rip),%rdx
lea    0xdbb(%rip),%rcx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x64,%edi
call   1040 <do_exit@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%rax
jge    12af <msg_and_exit+0x14f>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%r8
lea    0xdaf(%rip),%rsi
lea    0xd97(%rip),%rdx
lea    0xd62(%rip),%rcx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x64,%edi
call   1040 <do_exit@plt>
mov    -0x4(%rbp),%edi
call   1040 <do_exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msg_and_exit>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
mov    %edi,%r14d
lea    0xe8c(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11cc <msg_and_exit+0x6c>
mov    0x2e51(%rip),%rax
mov    (%rax),%r15d
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe63(%rip),%rsi
lea    0xe80(%rip),%rdx
lea    0xe4b(%rip),%rcx
mov    %r15d,%edi
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x64,%edi
call   1040 <do_exit@plt>
lea    0xe63(%rip),%rsi
lea    0xe54(%rip),%rdx
mov    %ebx,%edi
mov    %rbp,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
je     1235 <msg_and_exit+0xd5>
mov    0x2de6(%rip),%rax
mov    (%rax),%ebp
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe2e(%rip),%rsi
lea    0xe16(%rip),%rdx
lea    0xde1(%rip),%rcx
mov    %ebp,%edi
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x64,%edi
call   1040 <do_exit@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
test   %rax,%rax
jns    1284 <msg_and_exit+0x124>
mov    0x2d97(%rip),%rax
mov    (%rax),%ebx
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xddf(%rip),%rsi
lea    0xdc7(%rip),%rdx
lea    0xd92(%rip),%rcx
mov    %ebx,%edi
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x64,%edi
call   1040 <do_exit@plt>
mov    %r14d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <do_exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"./error\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"%s: cannot open %s for writing: %s\0A\00"
@.str.3 = [8 x i8] c"dosrun2\00"
@errno = global i32 0
@.str.4 = [9 x i8] c"%s: %s\0D\0A\00"
@.str.5 = [25 x i8] c"%s: i/o error on %s: %s\0A\00"
define void @msg_and_exit(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32* null, i32** %5
%6 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %5
%7 = icmp ne i32* %6, null
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = call i32 @do_exit(i32 100)
br label %14
14: ; preds = %8, %2
%15 = load i32*, i32** %5
%16 = ptrtoint i32* %15 to i32
%17 = load i8*, i8** %4
%18 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%19 = load i32*, i32** %5
%20 = call i64 @ferror(i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %14
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = call i32 @do_exit(i32 100)
br label %28
28: ; preds = %22, %14
%29 = load i32*, i32** %5
%30 = call i64 @fclose(i32* %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = call i32 @do_exit(i32 100)
br label %38
38: ; preds = %32, %28
%39 = load i32, i32* %3
%40 = call i32 @do_exit(i32 %39)
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"./error\00"
@.str.1 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.2 = [36 x i8] c"%s: cannot open %s for writing: %s\0A\00"
@.str.3 = [8 x i8] c"dosrun2\00"
@errno = global i32 0
@.str.4 = [9 x i8] c"%s: %s\0D\0A\00"
@.str.5 = [25 x i8] c"%s: i/o error on %s: %s\0A\00"
define void @msg_and_exit(i32 %0, i8* %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %11
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @do_exit(i32 100)
br label %11
11: ; preds = %5, %2
%12 = ptrtoint i32* %3 to i64
%13 = trunc i64 %12 to i32
%14 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%15 = tail call i64 @ferror(i32* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %11
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = tail call i32 @do_exit(i32 100)
br label %23
23: ; preds = %17, %11
%24 = tail call i64 @fclose(i32* %3)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %23
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = tail call i8* @strerror(i32 %28)
%30 = tail call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = tail call i32 @do_exit(i32 100)
br label %32
32: ; preds = %26, %23
%33 = tail call i32 @do_exit(i32 %0)
ret void
}