#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ puts(char *);
scalar_t__ sendfile(int, int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
void check_leaks(void) {
  int fd;
  fd = open("/sys/kernel/debug/kmemleak", O_WRONLY);
  if (fd < 0)
    return;
  puts("\x1b[32m\x1b[1m"
       "[+] Scanning for memory leaks..."
       "\x1b[0m");
  sleep(2);
  write(fd, "scan\n", 5);
  close(fd);
  fd = open("/sys/kernel/debug/kmemleak", O_RDONLY);
  if (fd < 0)
    return;
  if (sendfile(1, fd, NULL, 0x7ffff000) > 0)
    panic("Memory leaks encountered");
  close(fd);
}


x86 assembly (clang, -O0):
<check_leaks>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    119f <check_leaks+0x2f>
jmp    122c <check_leaks+0xbc>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    -0x4(%rbp),%edi
lea    0xe8a(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe1f(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11f8 <check_leaks+0x88>
jmp    122c <check_leaks+0xbc>
mov    -0x4(%rbp),%esi
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%edx
mov    $0x7ffff000,%ecx
call   1070 <sendfile@plt>
cmp    $0x0,%rax
jle    1224 <check_leaks+0xb4>
lea    0xe30(%rip),%rdi
call   1050 <panic@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_leaks>:
push   %rbx
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe7f(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1203 <check_leaks+0x93>
mov    %eax,%ebx
lea    0xe88(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
lea    0xea0(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1203 <check_leaks+0x93>
mov    %eax,%ebx
mov    $0x1,%edi
mov    %eax,%esi
xor    %edx,%edx
mov    $0x7ffff000,%ecx
call   1070 <sendfile@plt>
test   %rax,%rax
jle    11fb <check_leaks+0x8b>
lea    0xe59(%rip),%rdi
call   1050 <panic@plt>
mov    %ebx,%edi
pop    %rbx
jmp    1060 <close@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [27 x i8] c"/sys/kernel/debug/kmemleak\00"
@O_WRONLY = global i32 0
@.str.1 = [46 x i8] c"\1B[32m\1B[1m[+] Scanning for memory leaks...\1B[0m\00"
@.str.2 = [6 x i8] c"scan\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [25 x i8] c"Memory leaks encountered\00"
define void @check_leaks() {
%1 = alloca i32
%2 = load i32, i32* @O_WRONLY
%3 = call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %2)
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %7
6: ; preds = %0
br label %28
7: ; preds = %0
%8 = call i32 @puts(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @sleep(i32 2)
%10 = load i32, i32* %1
%11 = call i32 @write(i32 %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%12 = load i32, i32* %1
%13 = call i32 @close(i32 %12)
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %1
%16 = load i32, i32* %1
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %7
br label %28
19: ; preds = %7
%20 = load i32, i32* %1
%21 = call i64 @sendfile(i32 1, i32 %20, i32* null, i32 2147479552)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @panic(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = load i32, i32* %1
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %25, %18, %6
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"/sys/kernel/debug/kmemleak\00"
@O_WRONLY = global i32 0
@.str.1 = [46 x i8] c"\1B[32m\1B[1m[+] Scanning for memory leaks...\1B[0m\00"
@.str.2 = [6 x i8] c"scan\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [25 x i8] c"Memory leaks encountered\00"
define void @check_leaks() {
%1 = load i32, i32* @O_WRONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %19, label %4
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @sleep(i32 2)
%7 = tail call i32 @write(i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 5)
%8 = tail call i32 @close(i32 %2)
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %4
%13 = tail call i64 @sendfile(i32 1, i32 %10, i32* null, i32 2147479552)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = tail call i32 @panic(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = tail call i32 @close(i32 %10)
br label %19
19: ; preds = %4, %0, %17
ret void
}