#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 */
typedef int /*<<< orphan*/ SHA_CTX;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int O_NONBLOCK;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SHA1_Final(unsigned char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Init(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA1_Update(int /*<<< orphan*/ *, void *, int);
int SHA_DIGEST_LENGTH;
int atoi(char *);
long atol(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int getpagesize();
int ivshmem_recv(int);
int /*<<< orphan*/ ivshmem_send(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
long rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  int fd, length = 4 * 1024;
  void *memptr, *regptr;
  long *long_array;
  long num_chunks;
  int other;
  int i, j, k;
  int count;
  if (argc != 4) {
    printf("USAGE: dump_sema <filename> <num chunks> <other vm>\n");
    exit(-1);
  }
  fd = open(argv[1], O_RDWR | O_NONBLOCK);
  printf("[DUMP] opening file %s\n", argv[1]);
  num_chunks = atol(argv[2]);
  other = atoi(argv[3]);
  length = num_chunks * (4 * 1024 * 1024);
  printf("[DUMP] size is %d\n", length);
  if ((regptr = mmap(NULL, 256, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                     0 * getpagesize())) == (void *)-1) {
    printf("mmap failed (0x%p)\n", regptr);
    close(fd);
    exit(-1);
  }
  if ((memptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                     1 * getpagesize())) == (void *)-1) {
    printf("mmap failed (0x%p)\n", memptr);
    close(fd);
    exit(-1);
  }
  srand(time(NULL));
  long_array = (long *)memptr;
  count = num_chunks;
  for (k = 0; k < 2; k++) {
    int oldrv = -1;
    for (j = 0; j < num_chunks; j++) {
      int rv;
      SHA_CTX context;
      unsigned char md[20];
      long offset = j * ((4 * 1024 * 1024) / sizeof(long));
      memset(md, 0, 20);
      SHA1_Init(&context);
      do {
        rv = ivshmem_recv(fd);
        if (rv > 0) {
          if (oldrv == -1)
            oldrv = rv - 1;
          count += rv - oldrv;
          printf("rv = %d\n", rv);
        }
        oldrv = rv;
      } while (count <= 0);
      for (i = 0; i < (4 * 1024 * 1024) / sizeof(long); i++) {
        long_array[offset + i] = rand();
      }
      SHA1_Update(&context, memptr + (4 * 1024 * 1024) * j, (4 * 1024 * 1024));
      count--;
      ivshmem_send(regptr, 0, other);
      SHA1_Final(md, &context);
      printf("[CHUNK %d] ", j);
      for (i = 0; i < SHA_DIGEST_LENGTH; ++i) {
        unsigned char c = md[i];
        printf("%2.2x", c);
      }
      printf("\n");
    }
  }
  printf("[DUMP] munmap is unmapping %p\n", memptr);
  munmap(memptr, length);
  munmap(regptr, 256);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1000,-0x18(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     1262 <main+0x42>
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1120 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
mov    0x2d56(%rip),%rax
or     (%rax),%esi
call   10f0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atol@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rax
shl    $0x16,%rax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    0x2cd1(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x88(%rbp)
mov    0x2cf2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x80(%rbp)
mov    $0x0,%al
call   1110 <getpagesize@plt>
mov    -0x88(%rbp),%edx
mov    -0x84(%rbp),%ecx
mov    -0x80(%rbp),%r8d
imul   $0x0,%eax,%r9d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x100,%esi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1365 <main+0x145>
mov    -0x28(%rbp),%rsi
lea    0xd14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0xffffffff,%edi
call   1120 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x98(%rbp)
mov    0x2c83(%rip),%rax
mov    (%rax),%eax
mov    0x2c3a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x94(%rbp)
mov    0x2c5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x90(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x8c(%rbp)
mov    $0x0,%al
call   1110 <getpagesize@plt>
mov    -0x98(%rbp),%esi
mov    -0x94(%rbp),%edx
mov    -0x90(%rbp),%ecx
mov    -0x8c(%rbp),%r8d
mov    %eax,%r9d
shl    $0x0,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1406 <main+0x1e6>
mov    -0x20(%rbp),%rsi
lea    0xc73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0xffffffff,%edi
call   1120 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    %eax,-0x4c(%rbp)
movl   $0x0,-0x48(%rbp)
cmpl   $0x2,-0x48(%rbp)
jge    15d3 <main+0x3b3>
movl   $0xffffffff,-0x50(%rbp)
movl   $0x0,-0x44(%rbp)
movslq -0x44(%rbp),%rax
cmp    -0x38(%rbp),%rax
jge    15c0 <main+0x3a0>
movslq -0x44(%rbp),%rax
shl    $0x13,%rax
mov    %rax,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
lea    -0x58(%rbp),%rdi
call   10d0 <SHA1_Init@plt>
mov    -0x14(%rbp),%edi
call   1090 <ivshmem_recv@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jle    14bc <main+0x29c>
cmpl   $0xffffffff,-0x50(%rbp)
jne    149f <main+0x27f>
mov    -0x54(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x50(%rbp)
mov    -0x54(%rbp),%eax
sub    -0x50(%rbp),%eax
add    -0x4c(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x54(%rbp),%esi
lea    0xbbf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jle    1477 <main+0x257>
movl   $0x0,-0x40(%rbp)
movslq -0x40(%rbp),%rax
cmp    $0x80000,%rax
jae    150e <main+0x2ee>
mov    $0x0,%al
call   1140 <rand@plt>
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
mov    -0x78(%rbp),%rcx
movslq -0x40(%rbp),%rsi
add    %rsi,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    14d3 <main+0x2b3>
mov    -0x20(%rbp),%rsi
mov    -0x44(%rbp),%eax
shl    $0x16,%eax
cltq
add    %rax,%rsi
lea    -0x58(%rbp),%rdi
mov    $0x400000,%edx
call   10a0 <SHA1_Update@plt>
mov    -0x4c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x3c(%rbp),%edx
xor    %esi,%esi
call   10c0 <ivshmem_send@plt>
lea    -0x70(%rbp),%rdi
lea    -0x58(%rbp),%rsi
call   1130 <SHA1_Final@plt>
mov    -0x44(%rbp),%esi
lea    0xb24(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    0x2a57(%rip),%rcx
cmp    (%rcx),%eax
jge    15a4 <main+0x384>
movslq -0x40(%rbp),%rax
mov    -0x70(%rbp,%rax,1),%al
mov    %al,-0x79(%rbp)
movzbl -0x79(%rbp),%esi
lea    0xafa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1567 <main+0x347>
lea    0xac7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1444 <main+0x224>
jmp    15c5 <main+0x3a5>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    142c <main+0x20c>
mov    -0x20(%rbp),%rsi
lea    0xab1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   10b0 <munmap@plt>
mov    -0x28(%rbp),%rdi
mov    $0x100,%esi
call   10b0 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
cmp    $0x4,%edi
jne    16e0 <main+0x4a0>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2d73(%rip),%rax
mov    0x2d64(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1110 <open@plt>
mov    %eax,%ebx
mov    0x8(%r14),%rsi
lea    0xd7e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%r14),%rdi
call   1100 <atol@plt>
mov    %rax,%rbp
mov    0x18(%r14),%rdi
call   1120 <atoi@plt>
mov    %eax,0x2c(%rsp)
mov    %rbp,0x20(%rsp)
mov    %ebp,%r13d
shl    $0x16,%r13d
lea    0xd63(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d32(%rip),%rax
mov    0x2ceb(%rip),%r12
mov    (%r12),%ebp
or     (%rax),%ebp
mov    0x2d0e(%rip),%r15
mov    (%r15),%r14d
xor    %eax,%eax
call   1130 <getpagesize@plt>
xor    %edi,%edi
mov    $0x100,%esi
mov    %ebp,%edx
mov    %r14d,%ecx
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,0x30(%rsp)
cmp    $0xffffffffffffffff,%rax
je     16ba <main+0x47a>
mov    (%r12),%ebp
mov    0x2ce3(%rip),%rax
or     (%rax),%ebp
mov    (%r15),%r14d
xor    %eax,%eax
call   1130 <getpagesize@plt>
xor    %edi,%edi
mov    %r13d,%esi
mov    %ebp,%edx
mov    %r14d,%ecx
mov    %ebx,%r8d
mov    %eax,%r9d
call   1050 <mmap@plt>
mov    %rax,0x18(%rsp)
cmp    $0xffffffffffffffff,%rax
mov    %r13d,0x3c(%rsp)
je     16ba <main+0x47a>
xor    %edi,%edi
call   10a0 <time@plt>
mov    %eax,%edi
call   1090 <srand@plt>
cmpq   $0x0,0x20(%rsp)
jle    1671 <main+0x431>
mov    $0xffffffff,%ebp
lea    0xce4(%rip),%r13
mov    0x18(%rsp),%r14
xor    %eax,%eax
mov    %rax,0x8(%rsp)
mov    0x20(%rsp),%rax
mov    %eax,%r15d
mov    %ebx,0x28(%rsp)
jmp    13c5 <main+0x185>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%r15d
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x8(%rsp),%rcx
add    $0x1,%rcx
add    $0x400000,%r14
mov    %rcx,%rax
mov    %rcx,0x8(%rsp)
cmp    0x20(%rsp),%rcx
mov    0x28(%rsp),%ebx
je     14e1 <main+0x2a1>
lea    0x40(%rsp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
lea    0x14(%rsp),%rdi
call   10f0 <SHA1_Init@plt>
mov    %ebp,%r12d
jmp    13f8 <main+0x1b8>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%r12d
test   %r15d,%r15d
jg     1430 <main+0x1f0>
mov    %ebx,%edi
call   10b0 <ivshmem_recv@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    13f0 <main+0x1b0>
lea    -0x1(%rbp),%eax
cmp    $0xffffffff,%r12d
cmovne %r12d,%eax
add    %ebp,%r15d
sub    %eax,%r15d
lea    0xc22(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    13f0 <main+0x1b0>
nopl   0x0(%rax,%rax,1)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1160 <rand@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
cmp    $0x80000,%rbx
jne    1440 <main+0x200>
mov    0x8(%rsp),%r12
mov    %r12d,%esi
shl    $0x16,%esi
add    0x18(%rsp),%rsi
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400000,%edx
call   10c0 <SHA1_Update@plt>
mov    0x30(%rsp),%rdi
xor    %esi,%esi
mov    0x2c(%rsp),%edx
call   10e0 <ivshmem_send@plt>
lea    0x40(%rsp),%rdi
mov    %rbx,%rsi
call   1150 <SHA1_Final@plt>
lea    0xbaa(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2b19(%rip),%r12
cmpl   $0x0,(%r12)
jle    1390 <main+0x150>
xor    %ebx,%ebx
nopl   0x0(%rax)
movzbl 0x40(%rsp,%rbx,1),%esi
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
movslq (%r12),%rax
cmp    %rax,%rbx
jl     14c0 <main+0x280>
jmp    1390 <main+0x150>
cmpq   $0x0,0x20(%rsp)
jle    1671 <main+0x431>
mov    $0xffffffff,%r12d
mov    0x18(%rsp),%r14
xor    %eax,%eax
mov    %rax,0x8(%rsp)
jmp    1545 <main+0x305>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%r15d
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x8(%rsp),%rcx
add    $0x1,%rcx
add    $0x400000,%r14
mov    %rcx,%rax
mov    %rcx,0x8(%rsp)
cmp    0x20(%rsp),%rcx
mov    0x28(%rsp),%ebx
je     1671 <main+0x431>
lea    0x40(%rsp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
lea    0x14(%rsp),%rdi
call   10f0 <SHA1_Init@plt>
mov    %r12d,%r13d
jmp    1578 <main+0x338>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12d,%r13d
test   %r15d,%r15d
jg     15b0 <main+0x370>
mov    %ebx,%edi
call   10b0 <ivshmem_recv@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    1570 <main+0x330>
lea    -0x1(%r12),%eax
cmp    $0xffffffff,%r13d
cmovne %r13d,%eax
add    %r12d,%r15d
sub    %eax,%r15d
lea    0xa9f(%rip),%rdi
mov    %r12d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1570 <main+0x330>
nopl   0x0(%rax)
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1160 <rand@plt>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
cmp    $0x80000,%rbx
jne    15c0 <main+0x380>
mov    0x8(%rsp),%rbp
mov    %ebp,%esi
shl    $0x16,%esi
add    0x18(%rsp),%rsi
lea    0x14(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400000,%edx
call   10c0 <SHA1_Update@plt>
mov    0x30(%rsp),%rdi
xor    %esi,%esi
mov    0x2c(%rsp),%edx
call   10e0 <ivshmem_send@plt>
lea    0x40(%rsp),%rdi
mov    %rbx,%rsi
call   1150 <SHA1_Final@plt>
lea    0xa2b(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x299b(%rip),%r13
cmpl   $0x0,0x0(%r13)
lea    0xa1b(%rip),%rbp
jle    1510 <main+0x2d0>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl 0x40(%rsp,%rbx,1),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
movslq 0x0(%r13),%rax
cmp    %rax,%rbx
jl     1650 <main+0x410>
jmp    1510 <main+0x2d0>
lea    0x9e2(%rip),%rdi
mov    0x18(%rsp),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
mov    0x3c(%rsp),%esi
call   10d0 <munmap@plt>
mov    0x30(%rsp),%rdi
mov    $0x100,%esi
call   10d0 <munmap@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x96a(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0xffffffff,%edi
call   1140 <exit@plt>
lea    0x992(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1140 <exit@plt>

LLVM IR (-O0):
@.str = [53 x i8] c"USAGE: dump_sema <filename> <num chunks> <other vm>\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [24 x i8] c"[DUMP] opening file %s\0A\00"
@.str.2 = [19 x i8] c"[DUMP] size is %d\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [20 x i8] c"mmap failed (0x%p)\0A\00"
@.str.4 = [9 x i8] c"rv = %d\0A\00"
@.str.5 = [12 x i8] c"[CHUNK %d] \00"
@SHA_DIGEST_LENGTH = global i32 0
@.str.6 = [6 x i8] c"%2.2x\00"
@.str.7 = [2 x i8] c"\0A\00"
@.str.8 = [31 x i8] c"[DUMP] munmap is unmapping %p\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca [20 x i8]
%21 = alloca i64
%22 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4096, i32* %7
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 4
br i1 %24, label %25, label %28
25: ; preds = %2
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = load i32, i32* @O_RDWR
%33 = load i32, i32* @O_NONBLOCK
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %31, i32 %34)
store i32 %35, i32* %6
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = call i64 @atol(i8* %42)
store i64 %43, i64* %11
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 3
%46 = load i8*, i8** %45
%47 = call i32 @atoi(i8* %46)
store i32 %47, i32* %12
%48 = load i64, i64* %11
%49 = mul nsw i64 %48, 4194304
%50 = trunc i64 %49 to i32
store i32 %50, i32* %7
%51 = load i32, i32* %7
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %51)
%53 = load i32, i32* @PROT_READ
%54 = load i32, i32* @PROT_WRITE
%55 = or i32 %53, %54
%56 = load i32, i32* @MAP_SHARED
%57 = load i32, i32* %6
%58 = call i32 (...) @getpagesize()
%59 = mul nsw i32 0, %58
%60 = call i8* @mmap(i32* null, i32 256, i32 %55, i32 %56, i32 %57, i32 %59)
store i8* %60, i8** %9
%61 = icmp eq i8* %60, inttoptr (i64 -1 to i8*)
br i1 %61, label %62, label %68
62: ; preds = %28
%63 = load i8*, i8** %9
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %63)
%65 = load i32, i32* %6
%66 = call i32 @close(i32 %65)
%67 = call i32 @exit(i32 -1)
unreachable
68: ; preds = %28
%69 = load i32, i32* %7
%70 = load i32, i32* @PROT_READ
%71 = load i32, i32* @PROT_WRITE
%72 = or i32 %70, %71
%73 = load i32, i32* @MAP_SHARED
%74 = load i32, i32* %6
%75 = call i32 (...) @getpagesize()
%76 = mul nsw i32 1, %75
%77 = call i8* @mmap(i32* null, i32 %69, i32 %72, i32 %73, i32 %74, i32 %76)
store i8* %77, i8** %8
%78 = icmp eq i8* %77, inttoptr (i64 -1 to i8*)
br i1 %78, label %79, label %85
79: ; preds = %68
%80 = load i8*, i8** %8
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %80)
%82 = load i32, i32* %6
%83 = call i32 @close(i32 %82)
%84 = call i32 @exit(i32 -1)
unreachable
85: ; preds = %68
%86 = call i32 @time(i32* null)
%87 = call i32 @srand(i32 %86)
%88 = load i8*, i8** %8
%89 = bitcast i8* %88 to i64*
store i64* %89, i64** %10
%90 = load i64, i64* %11
%91 = trunc i64 %90 to i32
store i32 %91, i32* %16
store i32 0, i32* %15
br label %92
92: ; preds = %185, %85
%93 = load i32, i32* %15
%94 = icmp slt i32 %93, 2
br i1 %94, label %95, label %188
95: ; preds = %92
store i32 -1, i32* %17
store i32 0, i32* %14
br label %96
96: ; preds = %181, %95
%97 = load i32, i32* %14
%98 = sext i32 %97 to i64
%99 = load i64, i64* %11
%100 = icmp slt i64 %98, %99
br i1 %100, label %101, label %184
101: ; preds = %96
%102 = load i32, i32* %14
%103 = sext i32 %102 to i64
%104 = mul i64 %103, 524288
store i64 %104, i64* %21
%105 = getelementptr inbounds [20 x i8], [20 x i8]* %20, i64 0, i64 0
%106 = call i32 @memset(i8* %105, i32 0, i32 20)
%107 = call i32 @SHA1_Init(i32* %19)
br label %108
108: ; preds = %129, %101
%109 = load i32, i32* %6
%110 = call i32 @ivshmem_recv(i32 %109)
store i32 %110, i32* %18
%111 = load i32, i32* %18
%112 = icmp sgt i32 %111, 0
br i1 %112, label %113, label %127
113: ; preds = %108
%114 = load i32, i32* %17
%115 = icmp eq i32 %114, -1
br i1 %115, label %116, label %119
116: ; preds = %113
%117 = load i32, i32* %18
%118 = sub nsw i32 %117, 1
store i32 %118, i32* %17
br label %119
119: ; preds = %116, %113
%120 = load i32, i32* %18
%121 = load i32, i32* %17
%122 = sub nsw i32 %120, %121
%123 = load i32, i32* %16
%124 = add nsw i32 %123, %122
store i32 %124, i32* %16
%125 = load i32, i32* %18
%126 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %125)
br label %127
127: ; preds = %119, %108
%128 = load i32, i32* %18
store i32 %128, i32* %17
br label %129
129: ; preds = %127
%130 = load i32, i32* %16
%131 = icmp sle i32 %130, 0
br i1 %131, label %108, label %132
132: ; preds = %129
store i32 0, i32* %13
br label %133
133: ; preds = %145, %132
%134 = load i32, i32* %13
%135 = sext i32 %134 to i64
%136 = icmp ult i64 %135, 524288
br i1 %136, label %137, label %148
137: ; preds = %133
%138 = call i64 (...) @rand()
%139 = load i64*, i64** %10
%140 = load i64, i64* %21
%141 = load i32, i32* %13
%142 = sext i32 %141 to i64
%143 = add nsw i64 %140, %142
%144 = getelementptr inbounds i64, i64* %139, i64 %143
store i64 %138, i64* %144
br label %145
145: ; preds = %137
%146 = load i32, i32* %13
%147 = add nsw i32 %146, 1
store i32 %147, i32* %13
br label %133
148: ; preds = %133
%149 = load i8*, i8** %8
%150 = load i32, i32* %14
%151 = mul nsw i32 4194304, %150
%152 = sext i32 %151 to i64
%153 = getelementptr i8, i8* %149, i64 %152
%154 = call i32 @SHA1_Update(i32* %19, i8* %153, i32 4194304)
%155 = load i32, i32* %16
%156 = add nsw i32 %155, -1
store i32 %156, i32* %16
%157 = load i8*, i8** %9
%158 = load i32, i32* %12
%159 = call i32 @ivshmem_send(i8* %157, i32 0, i32 %158)
%160 = getelementptr inbounds [20 x i8], [20 x i8]* %20, i64 0, i64 0
%161 = call i32 @SHA1_Final(i8* %160, i32* %19)
%162 = load i32, i32* %14
%163 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %162)
store i32 0, i32* %13
br label %164
164: ; preds = %176, %148
%165 = load i32, i32* %13
%166 = load i32, i32* @SHA_DIGEST_LENGTH
%167 = icmp slt i32 %165, %166
br i1 %167, label %168, label %179
168: ; preds = %164
%169 = load i32, i32* %13
%170 = sext i32 %169 to i64
%171 = getelementptr inbounds [20 x i8], [20 x i8]* %20, i64 0, i64 %170
%172 = load i8, i8* %171
store i8 %172, i8* %22
%173 = load i8, i8* %22
%174 = zext i8 %173 to i32
%175 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %174)
br label %176
176: ; preds = %168
%177 = load i32, i32* %13
%178 = add nsw i32 %177, 1
store i32 %178, i32* %13
br label %164
179: ; preds = %164
%180 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %181
181: ; preds = %179
%182 = load i32, i32* %14
%183 = add nsw i32 %182, 1
store i32 %183, i32* %14
br label %96
184: ; preds = %96
br label %185
185: ; preds = %184
%186 = load i32, i32* %15
%187 = add nsw i32 %186, 1
store i32 %187, i32* %15
br label %92
188: ; preds = %92
%189 = load i8*, i8** %8
%190 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %189)
%191 = load i8*, i8** %8
%192 = load i32, i32* %7
%193 = call i32 @munmap(i8* %191, i32 %192)
%194 = load i8*, i8** %9
%195 = call i32 @munmap(i8* %194, i32 256)
%196 = load i32, i32* %6
%197 = call i32 @close(i32 %196)
%198 = load i32, i32* %3
ret i32 %198
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [24 x i8] c"[DUMP] opening file %s\0A\00"
@.str.2 = [19 x i8] c"[DUMP] size is %d\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [20 x i8] c"mmap failed (0x%p)\0A\00"
@.str.4 = [9 x i8] c"rv = %d\0A\00"
@.str.5 = [12 x i8] c"[CHUNK %d] \00"
@SHA_DIGEST_LENGTH = global i32 0
@.str.6 = [6 x i8] c"%2.2x\00"
@.str.8 = [31 x i8] c"[DUMP] munmap is unmapping %p\0A\00"
@str = [52 x i8] c"USAGE: dump_sema <filename> <num chunks> <other vm>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = icmp eq i32 %0, 4
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* %11, i32 %14)
%16 = load i8*, i8** %10
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds i8*, i8** %1, i64 2
%19 = load i8*, i8** %18
%20 = tail call i64 @atol(i8* %19)
%21 = getelementptr inbounds i8*, i8** %1, i64 3
%22 = load i8*, i8** %21
%23 = tail call i32 @atoi(i8* %22)
%24 = trunc i64 %20 to i32
%25 = shl i32 %24, 22
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %28, %27
%30 = load i32, i32* @MAP_SHARED
%31 = tail call i32 (...) @getpagesize()
%32 = tail call i8* @mmap(i32* null, i32 256, i32 %29, i32 %30, i32 %15, i32 0)
%33 = icmp eq i8* %32, inttoptr (i64 -1 to i8*)
br i1 %33, label %34, label %38
34: ; preds = %9
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
%36 = tail call i32 @close(i32 %15)
%37 = tail call i32 @exit(i32 -1)
unreachable
38: ; preds = %9
%39 = load i32, i32* @PROT_READ
%40 = load i32, i32* @PROT_WRITE
%41 = or i32 %40, %39
%42 = load i32, i32* @MAP_SHARED
%43 = tail call i32 (...) @getpagesize()
%44 = tail call i8* @mmap(i32* null, i32 %25, i32 %41, i32 %42, i32 %15, i32 %43)
%45 = icmp eq i8* %44, inttoptr (i64 -1 to i8*)
br i1 %45, label %46, label %50
46: ; preds = %38
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* inttoptr (i64 -1 to i8*))
%48 = tail call i32 @close(i32 %15)
%49 = tail call i32 @exit(i32 -1)
unreachable
50: ; preds = %38
%51 = tail call i32 @time(i32* null)
%52 = tail call i32 @srand(i32 %51)
%53 = bitcast i8* %44 to i64*
%54 = icmp sgt i64 %20, 0
%55 = bitcast i32* %3 to i8*
%56 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
br i1 %54, label %57, label %170
57: ; preds = %50, %109
%58 = phi i64 [ %111, %109 ], [ 0, %50 ]
%59 = phi i32 [ %66, %109 ], [ -1, %50 ]
%60 = phi i32 [ %93, %109 ], [ %24, %50 ]
%61 = call i32 @memset(i8* %56, i32 0, i32 20)
%62 = call i32 @SHA1_Init(i32* %3)
br label %63
63: ; preds = %75, %57
%64 = phi i32 [ %60, %57 ], [ %76, %75 ]
%65 = phi i32 [ %59, %57 ], [ %66, %75 ]
%66 = call i32 @ivshmem_recv(i32 %15)
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %75
68: ; preds = %63
%69 = icmp eq i32 %65, -1
%70 = add nsw i32 %66, -1
%71 = select i1 %69, i32 %70, i32 %65
%72 = add i32 %66, %64
%73 = sub i32 %72, %71
%74 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %66)
br label %75
75: ; preds = %68, %63
%76 = phi i32 [ %73, %68 ], [ %64, %63 ]
%77 = icmp slt i32 %76, 1
br i1 %77, label %63, label %78
78: ; preds = %75
%79 = shl nuw nsw i64 %58, 19
br label %80
80: ; preds = %78, %80
%81 = phi i64 [ 0, %78 ], [ %85, %80 ]
%82 = call i64 (...) @rand()
%83 = add nuw nsw i64 %79, %81
%84 = getelementptr inbounds i64, i64* %53, i64 %83
store i64 %82, i64* %84
%85 = add nuw nsw i64 %81, 1
%86 = icmp eq i64 %85, 524288
br i1 %86, label %87, label %80
87: ; preds = %80
%88 = trunc i64 %58 to i32
%89 = shl i64 %58, 22
%90 = and i64 %89, 4290772992
%91 = getelementptr i8, i8* %44, i64 %90
%92 = call i32 @SHA1_Update(i32* %3, i8* %91, i32 4194304)
%93 = add nsw i32 %76, -1
%94 = call i32 @ivshmem_send(i8* %32, i32 0, i32 %23)
%95 = call i32 @SHA1_Final(i8* %56, i32* %3)
%96 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %88)
%97 = load i32, i32* @SHA_DIGEST_LENGTH
%98 = icmp sgt i32 %97, 0
br i1 %98, label %99, label %109
99: ; preds = %87, %99
%100 = phi i64 [ %105, %99 ], [ 0, %87 ]
%101 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %100
%102 = load i8, i8* %101
%103 = zext i8 %102 to i32
%104 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %103)
%105 = add nuw nsw i64 %100, 1
%106 = load i32, i32* @SHA_DIGEST_LENGTH
%107 = sext i32 %106 to i64
%108 = icmp slt i64 %105, %107
br i1 %108, label %99, label %109
109: ; preds = %99, %87
%110 = call i32 @putchar(i32 10)
%111 = add nuw nsw i64 %58, 1
%112 = icmp eq i64 %111, %20
br i1 %112, label %113, label %57
113: ; preds = %109
br i1 %54, label %114, label %170
114: ; preds = %113, %166
%115 = phi i64 [ %168, %166 ], [ 0, %113 ]
%116 = phi i32 [ %123, %166 ], [ -1, %113 ]
%117 = phi i32 [ %150, %166 ], [ %93, %113 ]
%118 = call i32 @memset(i8* %56, i32 0, i32 20)
%119 = call i32 @SHA1_Init(i32* %3)
br label %120
120: ; preds = %132, %114
%121 = phi i32 [ %117, %114 ], [ %133, %132 ]
%122 = phi i32 [ %116, %114 ], [ %123, %132 ]
%123 = call i32 @ivshmem_recv(i32 %15)
%124 = icmp sgt i32 %123, 0
br i1 %124, label %125, label %132
125: ; preds = %120
%126 = icmp eq i32 %122, -1
%127 = add nsw i32 %123, -1
%128 = select i1 %126, i32 %127, i32 %122
%129 = add i32 %123, %121
%130 = sub i32 %129, %128
%131 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 %123)
br label %132
132: ; preds = %125, %120
%133 = phi i32 [ %130, %125 ], [ %121, %120 ]
%134 = icmp slt i32 %133, 1
br i1 %134, label %120, label %135
135: ; preds = %132
%136 = shl nuw nsw i64 %115, 19
br label %137
137: ; preds = %137, %135
%138 = phi i64 [ 0, %135 ], [ %142, %137 ]
%139 = call i64 (...) @rand()
%140 = add nuw nsw i64 %136, %138
%141 = getelementptr inbounds i64, i64* %53, i64 %140
store i64 %139, i64* %141
%142 = add nuw nsw i64 %138, 1
%143 = icmp eq i64 %142, 524288
br i1 %143, label %144, label %137
144: ; preds = %137
%145 = trunc i64 %115 to i32
%146 = shl i64 %115, 22
%147 = and i64 %146, 4290772992
%148 = getelementptr i8, i8* %44, i64 %147
%149 = call i32 @SHA1_Update(i32* %3, i8* %148, i32 4194304)
%150 = add nsw i32 %133, -1
%151 = call i32 @ivshmem_send(i8* %32, i32 0, i32 %23)
%152 = call i32 @SHA1_Final(i8* %56, i32* %3)
%153 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %145)
%154 = load i32, i32* @SHA_DIGEST_LENGTH
%155 = icmp sgt i32 %154, 0
br i1 %155, label %156, label %166
156: ; preds = %144, %156
%157 = phi i64 [ %162, %156 ], [ 0, %144 ]
%158 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %157
%159 = load i8, i8* %158
%160 = zext i8 %159 to i32
%161 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %160)
%162 = add nuw nsw i64 %157, 1
%163 = load i32, i32* @SHA_DIGEST_LENGTH
%164 = sext i32 %163 to i64
%165 = icmp slt i64 %162, %164
br i1 %165, label %156, label %166
166: ; preds = %156, %144
%167 = call i32 @putchar(i32 10)
%168 = add nuw nsw i64 %115, 1
%169 = icmp eq i64 %168, %20
br i1 %169, label %170, label %114
170: ; preds = %166, %50, %113
%171 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %44)
%172 = call i32 @munmap(i8* %44, i32 %25)
%173 = call i32 @munmap(i8* %32, i32 256)
%174 = call i32 @close(i32 %15)
ret i32 0
}