#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*/ flag;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(unsigned int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void testlib_cleanup(void) {
  FILE *fd = NULL;
  unsigned int flag = 0xAAAAAAAA;
  fd = fopen("/proc/sdei_msg", "w");
  if (fd == NULL) {
    printf("Unable to open /proc/sdei_msg\n");
    return;
  }
  fwrite(&flag, 1, sizeof(flag), fd);
  fclose(fd);
}


x86 assembly (clang, -O0):
<testlib_cleanup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movq   $0x0,-0x8(%rbp)
movl   $0xaaaaaaaa,-0xc(%rbp)
lea    0xea2(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    118a <testlib_cleanup+0x4a>
lea    0xe91(%rip),%rdi
call   1040 <printf@plt>
jmp    11aa <testlib_cleanup+0x6a>
mov    -0x8(%rbp),%rcx
lea    -0xc(%rbp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
call   1060 <fwrite@plt>
mov    -0x8(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testlib_cleanup>:
push   %rbx
sub    $0x10,%rsp
movl   $0xaaaaaaaa,0xc(%rsp)
aa
lea    0xebd(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     118c <testlib_cleanup+0x4c>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0x4,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1040 <fclose@plt>
lea    0xe6f(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"/proc/sdei_msg\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [31 x i8] c"Unable to open /proc/sdei_msg\0A\00"
define void @testlib_cleanup() {
%1 = alloca i32*
%2 = alloca i32
store i32* null, i32** %1
store i32 -1431655766, i32* %2
%3 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %3, i32** %1
%4 = load i32*, i32** %1
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %13
8: ; preds = %0
%9 = load i32*, i32** %1
%10 = call i32 @fwrite(i32* %2, i32 1, i32 4, i32* %9)
%11 = load i32*, i32** %1
%12 = call i32 @fclose(i32* %11)
br label %13
13: ; preds = %8, %6
ret void
}

LLVM IR (-O2):
@.str = [15 x i8] c"/proc/sdei_msg\00"
@.str.1 = [2 x i8] c"w\00"
@str = [30 x i8] c"Unable to open /proc/sdei_msg\00"
define void @testlib_cleanup() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 -1431655766, i32* %1
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 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 %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %10
7: ; preds = %0
%8 = call i32 @fwrite(i32* %1, i32 1, i32 4, i32* %3)
%9 = tail call i32 @fclose(i32* %3)
br label %10
10: ; preds = %7, %5
ret void
}