#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 EINVAL;
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ size_allocated_malloc(size_t *);
int run_single_unittest(const char *name, int (*unittest_f)(void)) {
  size_t allocated_bytes = 0;
  int err = unittest_f();
  if (size_allocated_malloc(&allocated_bytes)) {
    printf("ERROR in size_allocated_malloc\n");
  }
  printf("%s: ", name);
  if (err) {
    printf("FAILED\n");
  } else {
    printf("OK\n");
  }
  {
    size_t allocated_bytes2 = 0;
    if (size_allocated_malloc(&allocated_bytes2)) {
      printf("ERROR in size_allocated_malloc\n");
    }
    allocated_bytes = allocated_bytes2 - allocated_bytes;
  }
  if (!err && 0 != allocated_bytes) {
    printf("%s: MEMORY LEAK\n", name);
    err = EINVAL;
  }
  return err;
}


x86 assembly (clang, -O0):
<run_single_unittest>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
call   *-0x10(%rbp)
mov    %eax,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <size_allocated_malloc@plt>
cmp    $0x0,%rax
je     115f <run_single_unittest+0x3f>
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rsi
lea    0xeb6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
je     118e <run_single_unittest+0x6e>
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    119c <run_single_unittest+0x7c>
lea    0xe98(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
call   1040 <size_allocated_malloc@plt>
cmp    $0x0,%rax
je     11c5 <run_single_unittest+0xa5>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
sub    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1205 <run_single_unittest+0xe5>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     1205 <run_single_unittest+0xe5>
mov    -0x8(%rbp),%rsi
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_single_unittest>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
call   *%rsi
mov    %eax,%ebp
lea    0x8(%rsp),%rdi
call   1050 <size_allocated_malloc@plt>
test   %rax,%rax
je     1161 <run_single_unittest+0x31>
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
lea    0xe98(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
lea    0xe9b(%rip),%rax
lea    0xe97(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
movq   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rdi
call   1050 <size_allocated_malloc@plt>
test   %rax,%rax
je     11af <run_single_unittest+0x7f>
lea    0xe76(%rip),%rdi
call   1030 <puts@plt>
mov    0x10(%rsp),%rax
sub    0x8(%rsp),%rax
mov    %rax,0x8(%rsp)
test   %ebp,%ebp
jne    11e1 <run_single_unittest+0xb1>
test   %rax,%rax
je     11e1 <run_single_unittest+0xb1>
lea    0xe37(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [32 x i8] c"ERROR in size_allocated_malloc\0A\00"
@.str.1 = [5 x i8] c"%s: \00"
@.str.2 = [8 x i8] c"FAILED\0A\00"
@.str.3 = [4 x i8] c"OK\0A\00"
@.str.4 = [17 x i8] c"%s: MEMORY LEAK\0A\00"
@EINVAL = global i32 0
define i32 @run_single_unittest(i8* %0, i32 ()* %1) {
%3 = alloca i8*
%4 = alloca i32 ()*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
store i8* %0, i8** %3
store i32 ()* %1, i32 ()** %4
store i64 0, i64* %5
%8 = load i32 ()*, i32 ()** %4
%9 = call i32 %8()
store i32 %9, i32* %6
%10 = call i64 @size_allocated_malloc(i64* %5)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i8*, i8** %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %23
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %19
store i64 0, i64* %7
%24 = call i64 @size_allocated_malloc(i64* %7)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = load i64, i64* %7
%30 = load i64, i64* %5
%31 = sub i64 %29, %30
store i64 %31, i64* %5
%32 = load i32, i32* %6
%33 = icmp ne i32 %32, 0
br i1 %33, label %41, label %34
34: ; preds = %28
%35 = load i64, i64* %5
%36 = icmp ne i64 0, %35
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i8*, i8** %3
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @EINVAL
store i32 %40, i32* %6
br label %41
41: ; preds = %37, %34, %28
%42 = load i32, i32* %6
ret i32 %42
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"%s: \00"
@.str.4 = [17 x i8] c"%s: MEMORY LEAK\0A\00"
@EINVAL = global i32 0
@str = [3 x i8] c"OK\00"
@str.6 = [7 x i8] c"FAILED\00"
@str.7 = [31 x i8] c"ERROR in size_allocated_malloc\00"
define i32 @run_single_unittest(i8* %0, i32 ()* nocapture %1) {
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%6 = tail call i32 %1()
%7 = call i64 @size_allocated_malloc(i64* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.7, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = icmp eq i32 %6, 0
%14 = select i1 %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @str.6, i64 0, i64 0)
%15 = call i32 @puts(i8* dereferenceable(1) %14)
%16 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%17 = call i64 @size_allocated_malloc(i64* %4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %11
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.7, i64 0, i64 0))
br label %21
21: ; preds = %19, %11
%22 = load i64, i64* %4
%23 = load i64, i64* %3
%24 = sub i64 %22, %23
store i64 %24, i64* %3
%25 = icmp ne i64 %24, 0
%26 = select i1 %13, i1 %25, i1 false
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%29 = load i32, i32* @EINVAL
br label %30
30: ; preds = %27, %21
%31 = phi i32 [ %29, %27 ], [ %6, %21 ]
ret i32 %31
}