#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 *MAP_FAILED;
int MAP_LOCKED;
int MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpagesize();
int *mmap(int /*<<< orphan*/, int, int, int, int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(void) {
  int fd;
  int *kadr;
  int len = 1 * getpagesize();
  if ((fd = open("/dev/ppid_map", O_RDWR | O_SYNC)) < 0) {
    perror("open");
    exit(-1);
  }
  fprintf(stderr, "/dev/ppid_map: open OK\n");
  kadr = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, fd, 0);
  if (kadr == MAP_FAILED) {
    perror("Could not mmap /dev/ppid_map");
    exit(-1);
  }
  fprintf(stderr, "ppid_map: mmap OK\n");
  int cpu = 0;
  while (1) {
    for (cpu = 0; cpu < 8; cpu++) {
      int *buf = kadr + cpu * (64 / sizeof(int));
      printf("CPU%d: tgid %d pid %d\n", cpu, buf[0], buf[1]);
    }
    sleep(1);
  }
  close(fd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
shl    $0x0,%eax
mov    %eax,-0x14(%rbp)
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
lea    0xe77(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11dc <main+0x5c>
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%esi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    0x2dac(%rip),%rax
or     (%rax),%edx
mov    0x2ddb(%rip),%rax
mov    (%rax),%ecx
mov    0x2daa(%rip),%rax
or     (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d72(%rip),%rcx
cmp    (%rcx),%rax
jne    1255 <main+0xd5>
lea    0xdd7(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x8,-0x18(%rbp)
jge    12c3 <main+0x143>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
shl    $0x4,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%ecx
lea    0xd9d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1278 <main+0xf8>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1271 <main+0xf1>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1080 <getpagesize@plt>
mov    %eax,%ebx
mov    0x2e44(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe86(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     12f7 <main+0x177>
mov    %eax,%ebp
mov    0x2dfc(%rip),%r14
mov    (%r14),%edi
lea    0xe3f(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e26(%rip),%rax
mov    0x2dd7(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e04(%rip),%rax
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2da0(%rip),%rax
cmp    (%rax),%rbx
je     1300 <main+0x180>
mov    (%r14),%edi
lea    0xe1f(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe26(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    (%rbx),%edx
mov    0x4(%rbx),%ecx
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <printf@plt>
mov    0x40(%rbx),%edx
mov    0x44(%rbx),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
mov    0x80(%rbx),%edx
mov    0x84(%rbx),%ecx
mov    %r14,%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
mov    0xc0(%rbx),%edx
mov    0xc4(%rbx),%ecx
mov    %r14,%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
mov    0x100(%rbx),%edx
mov    0x104(%rbx),%ecx
mov    %r14,%rdi
mov    $0x4,%esi
call   1040 <printf@plt>
mov    0x140(%rbx),%edx
mov    0x144(%rbx),%ecx
mov    %r14,%rdi
mov    $0x5,%esi
call   1040 <printf@plt>
mov    0x180(%rbx),%edx
mov    0x184(%rbx),%ecx
mov    %r14,%rdi
mov    $0x6,%esi
call   1040 <printf@plt>
mov    0x1c0(%rbx),%edx
mov    0x1c4(%rbx),%ecx
mov    %r14,%rdi
mov    $0x7,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1230 <main+0xb0>
lea    0xd02(%rip),%rdi
jmp    1307 <main+0x187>
lea    0xd16(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/ppid_map\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [5 x i8] c"open\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"/dev/ppid_map: open OK\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_LOCKED = global i32 0
@MAP_FAILED = global i32* null
@.str.3 = [29 x i8] c"Could not mmap /dev/ppid_map\00"
@.str.4 = [19 x i8] c"ppid_map: mmap OK\0A\00"
@.str.5 = [23 x i8] c"CPU%d: tgid %d pid %d\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 (...) @getpagesize()
%8 = mul nsw i32 1, %7
store i32 %8, i32* %4
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_SYNC
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %0
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* %4
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %21, %22
%24 = load i32, i32* @MAP_SHARED
%25 = load i32, i32* @MAP_LOCKED
%26 = or i32 %24, %25
%27 = load i32, i32* %2
%28 = call i32* @mmap(i32 0, i32 %20, i32 %23, i32 %26, i32 %27, i32 0)
store i32* %28, i32** %3
%29 = load i32*, i32** %3
%30 = load i32*, i32** @MAP_FAILED
%31 = icmp eq i32* %29, %30
br i1 %31, label %32, label %35
32: ; preds = %17
%33 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %17
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %5
br label %38
38: ; preds = %35, %59
store i32 0, i32* %5
br label %39
39: ; preds = %56, %38
%40 = load i32, i32* %5
%41 = icmp slt i32 %40, 8
br i1 %41, label %42, label %59
42: ; preds = %39
%43 = load i32*, i32** %3
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = mul i64 %45, 16
%47 = getelementptr inbounds i32, i32* %43, i64 %46
store i32* %47, i32** %6
%48 = load i32, i32* %5
%49 = load i32*, i32** %6
%50 = getelementptr inbounds i32, i32* %49, i64 0
%51 = load i32, i32* %50
%52 = load i32*, i32** %6
%53 = getelementptr inbounds i32, i32* %52, i64 1
%54 = load i32, i32* %53
%55 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %48, i32 %51, i32 %54)
br label %56
56: ; preds = %42
%57 = load i32, i32* %5
%58 = add nsw i32 %57, 1
store i32 %58, i32* %5
br label %39
59: ; preds = %39
%60 = call i32 @sleep(i32 1)
br label %38
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/ppid_map\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [5 x i8] c"open\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"/dev/ppid_map: open OK\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_LOCKED = global i32 0
@MAP_FAILED = global i32* null
@.str.3 = [29 x i8] c"Could not mmap /dev/ppid_map\00"
@.str.4 = [19 x i8] c"ppid_map: mmap OK\0A\00"
@.str.5 = [23 x i8] c"CPU%d: tgid %d pid %d\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @getpagesize()
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_SYNC
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* @MAP_LOCKED
%18 = or i32 %17, %16
%19 = tail call i32* @mmap(i32 0, i32 %1, i32 %15, i32 %18, i32 %5, i32 0)
%20 = load i32*, i32** @MAP_FAILED
%21 = icmp eq i32* %19, %20
br i1 %21, label %22, label %25
22: ; preds = %10
%23 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%24 = tail call i32 @exit(i32 -1)
unreachable
25: ; preds = %10
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%28 = getelementptr inbounds i32, i32* %19, i64 1
%29 = getelementptr inbounds i32, i32* %19, i64 16
%30 = getelementptr inbounds i32, i32* %19, i64 17
%31 = getelementptr inbounds i32, i32* %19, i64 32
%32 = getelementptr inbounds i32, i32* %19, i64 33
%33 = getelementptr inbounds i32, i32* %19, i64 48
%34 = getelementptr inbounds i32, i32* %19, i64 49
%35 = getelementptr inbounds i32, i32* %19, i64 64
%36 = getelementptr inbounds i32, i32* %19, i64 65
%37 = getelementptr inbounds i32, i32* %19, i64 80
%38 = getelementptr inbounds i32, i32* %19, i64 81
%39 = getelementptr inbounds i32, i32* %19, i64 96
%40 = getelementptr inbounds i32, i32* %19, i64 97
%41 = getelementptr inbounds i32, i32* %19, i64 112
%42 = getelementptr inbounds i32, i32* %19, i64 113
br label %43
43: ; preds = %43, %25
%44 = load i32, i32* %19
%45 = load i32, i32* %28
%46 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 0, i32 %44, i32 %45)
%47 = load i32, i32* %29
%48 = load i32, i32* %30
%49 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 1, i32 %47, i32 %48)
%50 = load i32, i32* %31
%51 = load i32, i32* %32
%52 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 2, i32 %50, i32 %51)
%53 = load i32, i32* %33
%54 = load i32, i32* %34
%55 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 3, i32 %53, i32 %54)
%56 = load i32, i32* %35
%57 = load i32, i32* %36
%58 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 4, i32 %56, i32 %57)
%59 = load i32, i32* %37
%60 = load i32, i32* %38
%61 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 5, i32 %59, i32 %60)
%62 = load i32, i32* %39
%63 = load i32, i32* %40
%64 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 6, i32 %62, i32 %63)
%65 = load i32, i32* %41
%66 = load i32, i32* %42
%67 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 7, i32 %65, i32 %66)
%68 = tail call i32 @sleep(i32 1)
br label %43
}