#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*/ exit(int);
int /*<<< orphan*/ free_a_frame();
int get_free_frames();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setpsname(char *);
int /*<<< orphan*/ setterm(int);
int main() {
  int pid = getpid();
  if (pid != 2) {
    printf("swapper: don't start me manually.\n");
    exit(1);
  }
  setterm(9);
  setpsname("[swapper]");
  int init_frames = get_free_frames();
  int last_free_frames;
  int free_frames = init_frames;
  unsigned int counter = 0;
  for (;;) {
    last_free_frames = free_frames;
    free_frames = get_free_frames();
    if (free_frames != last_free_frames) {
      printf("[%d.%d] swapper: %d free frames. threshold = %d.", pid, counter++,
             free_frames, init_frames - 500);
      if (free_frames < init_frames - 500) {
        printf("calling free_a_frame (%d < %d)\n", free_frames,
               init_frames - 500);
        free_a_frame();
      } else {
        printf("\n");
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ab <main+0x3b>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x9,%edi
call   1090 <setterm@plt>
lea    0xe67(%rip),%rdi
call   1050 <setpsname@plt>
mov    $0x0,%al
call   1080 <get_free_frames@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1080 <get_free_frames@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     126b <main+0xfb>
mov    -0x8(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    %edx,%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0xc(%rbp),%r8d
sub    $0x1f4,%r8d
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1f4,%ecx
cmp    %ecx,%eax
jge    1258 <main+0xe8>
mov    -0x14(%rbp),%esi
mov    -0xc(%rbp),%edx
sub    $0x1f4,%edx
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <free_a_frame@plt>
jmp    1266 <main+0xf6>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    126b <main+0xfb>
jmp    11d8 <main+0x68>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <getpid@plt>
cmp    $0x2,%eax
jne    1240 <main+0xb0>
mov    $0x9,%edi
call   10b0 <setterm@plt>
lea    0xe44(%rip),%rdi
call   1070 <setpsname@plt>
xor    %eax,%eax
call   10a0 <get_free_frames@plt>
mov    %eax,%r12d
mov    %eax,%r15d
add    $0xfffffe0c,%r15d
lea    0xe2e(%rip),%r14
xor    %ebx,%ebx
jmp    1200 <main+0x70>
lea    0xe54(%rip),%rdi
mov    %r12d,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
call   1030 <free_a_frame@plt>
mov    %r13d,%ebx
xchg   %ax,%ax
mov    %r12d,%ebp
xor    %eax,%eax
call   10a0 <get_free_frames@plt>
mov    %eax,%r12d
cmp    %ebp,%eax
je     1200 <main+0x70>
lea    0x1(%rbx),%r13d
mov    %r14,%rdi
mov    $0x2,%esi
mov    %ebx,%edx
mov    %r12d,%ecx
mov    %r15d,%r8d
xor    %eax,%eax
call   1080 <printf@plt>
cmp    %r15d,%r12d
jl     11e0 <main+0x50>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    %r13d,%ebx
jmp    1200 <main+0x70>
lea    0xe14(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"swapper: don't start me manually.\0A\00"
@.str.1 = [10 x i8] c"[swapper]\00"
@.str.2 = [49 x i8] c"[%d.%d] swapper: %d free frames. threshold = %d.\00"
@.str.3 = [32 x i8] c"calling free_a_frame (%d < %d)\0A\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (...) @getpid()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 @setterm(i32 9)
%15 = call i32 @setpsname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 (...) @get_free_frames()
store i32 %16, i32* %3
%17 = load i32, i32* %3
store i32 %17, i32* %5
store i32 0, i32* %6
br label %18
18: ; preds = %45, %13
%19 = load i32, i32* %5
store i32 %19, i32* %4
%20 = call i32 (...) @get_free_frames()
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = load i32, i32* %4
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %45
24: ; preds = %18
%25 = load i32, i32* %2
%26 = load i32, i32* %6
%27 = add i32 %26, 1
store i32 %27, i32* %6
%28 = load i32, i32* %5
%29 = load i32, i32* %3
%30 = sub nsw i32 %29, 500
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %26, i32 %28, i32 %30)
%32 = load i32, i32* %5
%33 = load i32, i32* %3
%34 = sub nsw i32 %33, 500
%35 = icmp slt i32 %32, %34
br i1 %35, label %36, label %42
36: ; preds = %24
%37 = load i32, i32* %5
%38 = load i32, i32* %3
%39 = sub nsw i32 %38, 500
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %37, i32 %39)
%41 = call i32 (...) @free_a_frame()
br label %44
42: ; preds = %24
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %42, %36
br label %45
45: ; preds = %44, %18
br label %18
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"[swapper]\00"
@.str.2 = [49 x i8] c"[%d.%d] swapper: %d free frames. threshold = %d.\00"
@.str.3 = [32 x i8] c"calling free_a_frame (%d < %d)\0A\00"
@str = [34 x i8] c"swapper: don't start me manually.\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = icmp eq i32 %1, 2
br i1 %2, label %6, label %3
3: ; preds = %0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 @setterm(i32 9)
%8 = tail call i32 @setpsname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 (...) @get_free_frames()
%10 = add nsw i32 %9, -500
br label %11
11: ; preds = %25, %6
%12 = phi i32 [ %9, %6 ], [ %16, %25 ]
%13 = phi i32 [ 0, %6 ], [ %19, %25 ]
br label %14
14: ; preds = %11, %14
%15 = phi i32 [ %16, %14 ], [ %12, %11 ]
%16 = tail call i32 (...) @get_free_frames()
%17 = icmp eq i32 %16, %15
br i1 %17, label %14, label %18
18: ; preds = %14
%19 = add i32 %13, 1
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 %13, i32 %16, i32 %10)
%21 = icmp slt i32 %16, %10
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %16, i32 %10)
%24 = tail call i32 (...) @free_a_frame()
br label %25
25: ; preds = %22, %26
br label %11
26: ; preds = %18
%27 = tail call i32 @putchar(i32 10)
br label %25
}