#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 */
struct timespec {
  int tv_nsec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ F_OK;
int access(char const *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *getenv(char *);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(scalar_t__);
int /*<<< orphan*/ system__exit(int);
int unlink(char const *);
void my__exit(int status) {
  const char *ready_file = getenv("LEAK_READY_FILE");
  const char *done_file = getenv("LEAK_DONE_FILE");
  if (!ready_file || !done_file)
    return;
  fprintf(stderr, "Checking for leaks\n");
  int rv = unlink(ready_file);
  if (rv != 0) {
    fprintf(stderr, "Could not delete ready file %s: %s\n", ready_file,
            strerror(errno));
    ;
    return;
  }
  rv = access(done_file, F_OK);
  while (rv == 0) {
    struct timespec ts;
    ts.tv_sec = 0;
    ts.tv_nsec = 100000000;
    nanosleep(&ts, NULL);
    rv = access(done_file, F_OK);
  }
  if (errno != ENOENT)
    fprintf(stdout, "Failed to access done file %s: %s\n", done_file,
            strerror(errno));
  fprintf(stdout, "Leak check performed\n");
  system__exit(status);
}


x86 assembly (clang, -O0):
<my__exit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe7e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
lea    0xe7e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b1 <my__exit+0x41>
cmpq   $0x0,-0x18(%rbp)
jne    11b6 <my__exit+0x46>
jmp    12ec <my__exit+0x17c>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <unlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1223 <my__exit+0xb3>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dda(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12ec <my__exit+0x17c>
mov    -0x18(%rbp),%rdi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1278 <my__exit+0x108>
movq   $0x0,-0x28(%rbp)
movl   $0x5f5e100,-0x30(%rbp)
lea    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <nanosleep@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
call   1070 <access@plt>
mov    %eax,-0x1c(%rbp)
jmp    1238 <my__exit+0xc8>
mov    0x2d59(%rip),%rax
mov    (%rax),%rax
mov    0x2d37(%rip),%rcx
cmp    (%rcx),%rax
je     12cd <my__exit+0x15d>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d2b(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %rax,%rcx
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%edi
call   1090 <system__exit@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my__exit>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
lea    0xe7c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbp
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
test   %rbp,%rbp
je     12a3 <my__exit+0x133>
mov    %rax,%rbx
test   %rax,%rax
je     12a3 <my__exit+0x133>
mov    0x2e2c(%rip),%r15
mov    (%r15),%edi
lea    0xe61(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <unlink@plt>
test   %eax,%eax
je     1203 <my__exit+0x93>
mov    (%r15),%ebx
mov    0x2dfd(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xe49(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>
mov    0x2dae(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %eax,%eax
jne    124a <my__exit+0xda>
lea    0x8(%rsp),%rbp
xchg   %ax,%ax
movq   $0x0,0x10(%rsp)
movl   $0x5f5e100,0x8(%rsp)
05
mov    %rbp,%rdi
xor    %esi,%esi
call   1050 <nanosleep@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1070 <access@plt>
test   %eax,%eax
je     1220 <my__exit+0xb0>
mov    0x2d87(%rip),%rax
mov    (%rax),%rdi
mov    0x2d65(%rip),%rax
cmp    (%rax),%rdi
je     1284 <my__exit+0x114>
mov    0x2d69(%rip),%rax
mov    (%rax),%ebp
call   1080 <strerror@plt>
lea    0xde2(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r14d,%edi
call   1090 <system__exit@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@.str = [16 x i8] c"LEAK_READY_FILE\00"
@.str.1 = [15 x i8] c"LEAK_DONE_FILE\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"Checking for leaks\0A\00"
@.str.3 = [36 x i8] c"Could not delete ready file %s: %s\0A\00"
@errno = global i64 0
@F_OK = global i32 0
@ENOENT = global i64 0
@stdout = global i32 0
@.str.4 = [35 x i8] c"Failed to access done file %s: %s\0A\00"
@.str.5 = [22 x i8] c"Leak check performed\0A\00"
define void @my__exit(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.timespec
store i32 %0, i32* %2
%7 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %3
%8 = call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i8* %8, i8** %4
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %11, %1
br label %57
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%18 = load i8*, i8** %3
%19 = call i32 @unlink(i8* %18)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %3
%25 = load i64, i64* @errno
%26 = call i8* @strerror(i64 %25)
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %24, i8* %26)
br label %57
28: ; preds = %15
%29 = load i8*, i8** %4
%30 = load i32, i32* @F_OK
%31 = call i32 @access(i8* %29, i32 %30)
store i32 %31, i32* %5
br label %32
32: ; preds = %35, %28
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %32
%36 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 1
store i64 0, i64* %36
%37 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i32 0, i32 0
store i32 100000000, i32* %37
%38 = call i32 @nanosleep(%struct.timespec* %6, i32* null)
%39 = load i8*, i8** %4
%40 = load i32, i32* @F_OK
%41 = call i32 @access(i8* %39, i32 %40)
store i32 %41, i32* %5
br label %32
42: ; preds = %32
%43 = load i64, i64* @errno
%44 = load i64, i64* @ENOENT
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %52
46: ; preds = %42
%47 = load i32, i32* @stdout
%48 = load i8*, i8** %4
%49 = load i64, i64* @errno
%50 = call i8* @strerror(i64 %49)
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %48, i8* %50)
br label %52
52: ; preds = %46, %42
%53 = load i32, i32* @stdout
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%55 = load i32, i32* %2
%56 = call i32 @system__exit(i32 %55)
br label %57
57: ; preds = %52, %22, %14
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@.str = [16 x i8] c"LEAK_READY_FILE\00"
@.str.1 = [15 x i8] c"LEAK_DONE_FILE\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"Checking for leaks\0A\00"
@.str.3 = [36 x i8] c"Could not delete ready file %s: %s\0A\00"
@errno = global i64 0
@F_OK = global i32 0
@ENOENT = global i64 0
@stdout = global i32 0
@.str.4 = [35 x i8] c"Failed to access done file %s: %s\0A\00"
@.str.5 = [22 x i8] c"Leak check performed\0A\00"
define void @my__exit(i32 %0) {
%2 = alloca %struct.timespec
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp ne i8* %3, null
%6 = icmp ne i8* %4, null
%7 = and i1 %5, %6
br i1 %7, label %8, label %43
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @unlink(i8* %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %8
%14 = load i32, i32* @stderr
%15 = load i64, i64* @errno
%16 = tail call i8* @strerror(i64 %15)
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %3, i8* %16)
br label %43
18: ; preds = %8
%19 = load i32, i32* @F_OK
%20 = tail call i32 @access(i8* %4, i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %18
%23 = bitcast %struct.timespec* %2 to i8*
%24 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%25 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
br label %26
26: ; preds = %22, %26
store i64 0, i64* %24
store i32 100000000, i32* %25
%27 = call i32 @nanosleep(%struct.timespec* %2, i32* null)
%28 = load i32, i32* @F_OK
%29 = call i32 @access(i8* %4, i32 %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %26, label %31
31: ; preds = %26, %18
%32 = load i64, i64* @errno
%33 = load i64, i64* @ENOENT
%34 = icmp eq i64 %32, %33
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i32, i32* @stdout
%37 = call i8* @strerror(i64 %32)
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %4, i8* %37)
br label %39
39: ; preds = %35, %31
%40 = load i32, i32* @stdout
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 @system__exit(i32 %0)
br label %43
43: ; preds = %13, %39, %1
ret void
}