#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 /*<<< orphan*/ 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 *, long);
int SHA_DIGEST_LENGTH;
int atoi(char *);
long atol(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ do_select(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*/ *, long, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(void *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  long num_chunks, length;
  void *memptr, *regptr;
  int i, fd, j, k;
  int other, count;
  if (argc != 4) {
    printf("USAGE: sum <filename> <num chunks> <other vm>\n");
    exit(-1);
  }
  fd = open(argv[1], O_RDWR);
  printf("[SUM] opening file %s\n", argv[1]);
  num_chunks = atol(argv[2]);
  other = atoi(argv[3]);
  length = num_chunks * (1024l * 1024l * 4l);
  printf("[SUM] length is %ld\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);
  }
  printf("[SUM] reading %ld chunks\n", num_chunks);
  count = 0;
  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];
      memset(md, 0, 20);
      SHA1_Init(&context);
      if (count <= 0) {
        do_select(fd);
        rv = ivshmem_recv(fd);
        if (rv > 0) {
          if (oldrv == -1)
            oldrv = rv - 1;
          count += rv - oldrv;
          printf("rv = %d\n", rv);
          oldrv = rv;
        }
      }
      SHA1_Update(&context, memptr + (1024l * 1024l * 4l) * j,
                  (1024l * 1024l * 4l));
      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");
    }
  }
  munmap(memptr, length);
  munmap(regptr, 256);
  close(fd);
  printf("[SUM] Exiting...\n");
}


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)
cmpl   $0x4,-0x8(%rbp)
je     123b <main+0x3b>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
call   10e0 <open@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atol@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x16,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    0x2d07(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x80(%rbp)
mov    0x2d23(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    $0x0,%al
call   1100 <getpagesize@plt>
mov    -0x80(%rbp),%edx
mov    -0x7c(%rbp),%ecx
mov    -0x78(%rbp),%r8d
imul   $0x0,%eax,%r9d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x100,%esi
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    132b <main+0x12b>
mov    -0x30(%rbp),%rsi
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    0x2cbb(%rip),%rax
mov    (%rax),%eax
mov    0x2c7a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x8c(%rbp)
mov    0x2c93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x88(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x84(%rbp)
mov    $0x0,%al
call   1100 <getpagesize@plt>
mov    -0x98(%rbp),%rsi
mov    -0x8c(%rbp),%edx
mov    -0x88(%rbp),%ecx
mov    -0x84(%rbp),%r8d
mov    %eax,%r9d
shl    $0x0,%r9d
xor    %eax,%eax
mov    %eax,%edi
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    13cf <main+0x1cf>
mov    -0x28(%rbp),%rsi
lea    0xca5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
mov    $0xffffffff,%edi
call   1110 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xc95(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x2,-0x40(%rbp)
jge    1555 <main+0x355>
movl   $0xffffffff,-0x4c(%rbp)
movl   $0x0,-0x3c(%rbp)
movslq -0x3c(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1542 <main+0x342>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
lea    -0x54(%rbp),%rdi
call   10c0 <SHA1_Init@plt>
cmpl   $0x0,-0x48(%rbp)
jg     1490 <main+0x290>
mov    -0x38(%rbp),%edi
call   1070 <do_select@plt>
mov    -0x38(%rbp),%edi
call   1080 <ivshmem_recv@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jle    148b <main+0x28b>
cmpl   $0xffffffff,-0x4c(%rbp)
jne    1468 <main+0x268>
mov    -0x50(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x50(%rbp),%eax
sub    -0x4c(%rbp),%eax
add    -0x48(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x50(%rbp),%esi
lea    0xc0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    1490 <main+0x290>
mov    -0x28(%rbp),%rsi
movslq -0x3c(%rbp),%rax
shl    $0x16,%rax
add    %rax,%rsi
lea    -0x54(%rbp),%rdi
mov    $0x400000,%edx
call   1090 <SHA1_Update@plt>
mov    -0x48(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x44(%rbp),%edx
xor    %esi,%esi
call   10b0 <ivshmem_send@plt>
lea    -0x70(%rbp),%rdi
lea    -0x54(%rbp),%rsi
call   1120 <SHA1_Final@plt>
mov    -0x3c(%rbp),%esi
lea    0xbb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    0x2add(%rip),%rcx
cmp    (%rcx),%eax
jge    1526 <main+0x326>
movslq -0x34(%rbp),%rax
mov    -0x70(%rbp,%rax,1),%al
mov    %al,-0x71(%rbp)
movzbl -0x71(%rbp),%esi
lea    0xb8d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    14e9 <main+0x2e9>
lea    0xb40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1407 <main+0x207>
jmp    1547 <main+0x347>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    13ef <main+0x1ef>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%esi
call   10a0 <munmap@plt>
mov    -0x30(%rbp),%rdi
mov    $0x100,%esi
call   10a0 <munmap@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
lea    0xb23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@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    15c5 <main+0x3a5>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
call   1100 <open@plt>
mov    %eax,%r13d
mov    0x8(%rbx),%rsi
lea    0xda6(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%rbx),%rdi
call   10f0 <atol@plt>
mov    %rax,%rbp
mov    0x18(%rbx),%rdi
call   1110 <atoi@plt>
mov    %eax,0xc(%rsp)
mov    %rbp,0x10(%rsp)
shl    $0x16,%rbp
lea    0xd8d(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d5d(%rip),%rax
mov    0x2d1e(%rip),%r15
mov    (%r15),%ebx
or     (%rax),%ebx
mov    0x2d3a(%rip),%r12
mov    (%r12),%r14d
xor    %eax,%eax
call   1120 <getpagesize@plt>
mov    $0x100,%esi
xor    %edi,%edi
mov    %ebx,%edx
mov    %r14d,%ecx
mov    %r13d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,0x20(%rsp)
cmp    $0xffffffffffffffff,%rax
je     15db <main+0x3bb>
mov    (%r15),%ebx
mov    0x2d0f(%rip),%rax
or     (%rax),%ebx
mov    (%r12),%r14d
xor    %eax,%eax
call   1120 <getpagesize@plt>
xor    %edi,%edi
mov    %rbp,0x50(%rsp)
mov    %rbp,%rsi
mov    %ebx,%edx
mov    %r14d,%ecx
mov    %r13d,0x8(%rsp)
mov    %r13d,%r8d
mov    %eax,%r9d
call   1050 <mmap@plt>
mov    %rax,0x18(%rsp)
cmp    $0xffffffffffffffff,%rax
je     15f5 <main+0x3d5>
lea    0xd15(%rip),%rdi
mov    0x10(%rsp),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
test   %rbx,%rbx
jle    1581 <main+0x361>
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x2c(%rsp),%r15
mov    0x2c79(%rip),%r12
lea    0xd11(%rip),%rbp
xor    %r13d,%r13d
xor    %r14d,%r14d
jmp    138d <main+0x16d>
cs nopw 0x0(%rax,%rax,1)
add    $0xffffffff,%r14d
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r13
cmp    0x10(%rsp),%r13
je     1461 <main+0x241>
lea    0x30(%rsp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
mov    %r15,%rdi
call   10e0 <SHA1_Init@plt>
test   %r14d,%r14d
jg     13ec <main+0x1cc>
mov    0x8(%rsp),%ebx
mov    %ebx,%edi
call   1090 <do_select@plt>
mov    %ebx,%edi
call   10a0 <ivshmem_recv@plt>
test   %eax,%eax
jle    13ec <main+0x1cc>
mov    %eax,%ebx
lea    -0x1(%rbx),%eax
mov    0x4(%rsp),%ecx
cmp    $0xffffffff,%ecx
cmovne %ecx,%eax
add    %ebx,%r14d
sub    %eax,%r14d
lea    0xc7d(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
mov    %ebx,0x4(%rsp)
mov    %r13,%rsi
shl    $0x16,%rsi
add    0x18(%rsp),%rsi
mov    $0x400000,%edx
mov    %r15,%rdi
call   10b0 <SHA1_Update@plt>
mov    0x20(%rsp),%rdi
xor    %esi,%esi
mov    0xc(%rsp),%edx
call   10d0 <ivshmem_send@plt>
lea    0x30(%rsp),%rdi
mov    %r15,%rsi
call   1140 <SHA1_Final@plt>
lea    0xc3a(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmpl   $0x0,(%r12)
jle    1370 <main+0x150>
xor    %ebx,%ebx
movzbl 0x30(%rsp,%rbx,1),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
movslq (%r12),%rax
cmp    %rax,%rbx
jl     1440 <main+0x220>
jmp    1370 <main+0x150>
cmpq   $0x0,0x10(%rsp)
jle    1581 <main+0x361>
movl   $0xffffffff,0x4(%rsp)
ff
lea    0x2c(%rsp),%r13
lea    0xbee(%rip),%r15
xor    %ebx,%ebx
jmp    14ad <main+0x28d>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffffff,%r14d
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    0x10(%rsp),%rbx
je     1581 <main+0x361>
lea    0x30(%rsp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1070 <memset@plt>
mov    %r13,%rdi
call   10e0 <SHA1_Init@plt>
test   %r14d,%r14d
jg     150c <main+0x2ec>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1090 <do_select@plt>
mov    %ebp,%edi
call   10a0 <ivshmem_recv@plt>
test   %eax,%eax
jle    150c <main+0x2ec>
mov    %eax,%ebp
lea    -0x1(%rbp),%eax
mov    0x4(%rsp),%ecx
cmp    $0xffffffff,%ecx
cmovne %ecx,%eax
add    %ebp,%r14d
sub    %eax,%r14d
lea    0xb5d(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%eax
mov    %ebp,0x4(%rsp)
mov    %rbx,%rsi
shl    $0x16,%rsi
add    0x18(%rsp),%rsi
mov    $0x400000,%edx
mov    %r13,%rdi
call   10b0 <SHA1_Update@plt>
mov    0x20(%rsp),%rdi
xor    %esi,%esi
mov    0xc(%rsp),%edx
call   10d0 <ivshmem_send@plt>
lea    0x30(%rsp),%rdi
mov    %r13,%rsi
call   1140 <SHA1_Final@plt>
lea    0xb1a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
cmpl   $0x0,(%r12)
jle    1490 <main+0x270>
xor    %ebp,%ebp
nop
movzbl 0x30(%rsp,%rbp,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbp
movslq (%r12),%rax
cmp    %rax,%rbp
jl     1560 <main+0x340>
jmp    1490 <main+0x270>
mov    0x18(%rsp),%rdi
mov    0x50(%rsp),%rsi
call   10c0 <munmap@plt>
mov    0x20(%rsp),%rdi
mov    $0x100,%esi
call   10c0 <munmap@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
lea    0xac6(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xaba(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   1130 <exit@plt>
lea    0xa4a(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13d,%edi
jmp    160e <main+0x3ee>
lea    0xa30(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    $0xffffffff,%edi
call   1130 <exit@plt>

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

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"[SUM] opening file %s\0A\00"
@.str.2 = [21 x i8] c"[SUM] length is %ld\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 = [26 x i8] c"[SUM] reading %ld chunks\0A\00"
@.str.5 = [9 x i8] c"rv = %d\0A\00"
@.str.6 = [12 x i8] c"[CHUNK %d] \00"
@SHA_DIGEST_LENGTH = global i32 0
@.str.7 = [6 x i8] c"%2.2x\00"
@str = [17 x i8] c"[SUM] Exiting...\00"
@str.10 = [46 x i8] c"USAGE: sum <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 ([46 x i8], [46 x i8]* @str.10, 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 = tail call i32 @open(i8* %11, i32 %12)
%14 = load i8*, i8** %10
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i64 @atol(i8* %17)
%19 = getelementptr inbounds i8*, i8** %1, i64 3
%20 = load i8*, i8** %19
%21 = tail call i32 @atoi(i8* %20)
%22 = shl nsw i64 %18, 22
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %22)
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %25, %24
%27 = load i32, i32* @MAP_SHARED
%28 = tail call i32 (...) @getpagesize()
%29 = tail call i8* @mmap(i32* null, i64 256, i32 %26, i32 %27, i32 %13, i32 0)
%30 = icmp eq i8* %29, inttoptr (i64 -1 to i8*)
br i1 %30, label %31, label %35
31: ; preds = %9
%32 = 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*))
%33 = tail call i32 @close(i32 %13)
%34 = tail call i32 @exit(i32 -1)
unreachable
35: ; preds = %9
%36 = load i32, i32* @PROT_READ
%37 = load i32, i32* @PROT_WRITE
%38 = or i32 %37, %36
%39 = load i32, i32* @MAP_SHARED
%40 = tail call i32 (...) @getpagesize()
%41 = tail call i8* @mmap(i32* null, i64 %22, i32 %38, i32 %39, i32 %13, i32 %40)
%42 = icmp eq i8* %41, inttoptr (i64 -1 to i8*)
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = 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*))
%45 = tail call i32 @close(i32 %13)
%46 = tail call i32 @exit(i32 -1)
unreachable
47: ; preds = %35
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i64 %18)
%49 = icmp sgt i64 %18, 0
%50 = bitcast i32* %3 to i8*
%51 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
br i1 %49, label %52, label %143
52: ; preds = %47, %93
%53 = phi i64 [ %95, %93 ], [ 0, %47 ]
%54 = phi i32 [ %72, %93 ], [ -1, %47 ]
%55 = phi i32 [ %76, %93 ], [ 0, %47 ]
%56 = call i32 @memset(i8* %51, i32 0, i32 20)
%57 = call i32 @SHA1_Init(i32* %3)
%58 = icmp slt i32 %55, 1
br i1 %58, label %59, label %70
59: ; preds = %52
%60 = call i32 @do_select(i32 %13)
%61 = call i32 @ivshmem_recv(i32 %13)
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %70
63: ; preds = %59
%64 = icmp eq i32 %54, -1
%65 = add nsw i32 %61, -1
%66 = select i1 %64, i32 %65, i32 %54
%67 = add nsw i32 %61, %55
%68 = sub i32 %67, %66
%69 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %61)
br label %70
70: ; preds = %59, %63, %52
%71 = phi i32 [ %68, %63 ], [ %55, %59 ], [ %55, %52 ]
%72 = phi i32 [ %61, %63 ], [ %54, %59 ], [ %54, %52 ]
%73 = shl nuw nsw i64 %53, 22
%74 = getelementptr i8, i8* %41, i64 %73
%75 = call i32 @SHA1_Update(i32* %3, i8* %74, i64 4194304)
%76 = add nsw i32 %71, -1
%77 = call i32 @ivshmem_send(i8* %29, i32 0, i32 %21)
%78 = call i32 @SHA1_Final(i8* %51, i32* %3)
%79 = trunc i64 %53 to i32
%80 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %79)
%81 = load i32, i32* @SHA_DIGEST_LENGTH
%82 = icmp sgt i32 %81, 0
br i1 %82, label %83, label %93
83: ; preds = %70, %83
%84 = phi i64 [ %89, %83 ], [ 0, %70 ]
%85 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %84
%86 = load i8, i8* %85
%87 = zext i8 %86 to i32
%88 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 %87)
%89 = add nuw nsw i64 %84, 1
%90 = load i32, i32* @SHA_DIGEST_LENGTH
%91 = sext i32 %90 to i64
%92 = icmp slt i64 %89, %91
br i1 %92, label %83, label %93
93: ; preds = %83, %70
%94 = call i32 @putchar(i32 10)
%95 = add nuw nsw i64 %53, 1
%96 = icmp eq i64 %95, %18
br i1 %96, label %97, label %52
97: ; preds = %93
br i1 %49, label %98, label %143
98: ; preds = %97, %139
%99 = phi i64 [ %141, %139 ], [ 0, %97 ]
%100 = phi i32 [ %118, %139 ], [ -1, %97 ]
%101 = phi i32 [ %122, %139 ], [ %76, %97 ]
%102 = call i32 @memset(i8* %51, i32 0, i32 20)
%103 = call i32 @SHA1_Init(i32* %3)
%104 = icmp slt i32 %101, 1
br i1 %104, label %105, label %116
105: ; preds = %98
%106 = call i32 @do_select(i32 %13)
%107 = call i32 @ivshmem_recv(i32 %13)
%108 = icmp sgt i32 %107, 0
br i1 %108, label %109, label %116
109: ; preds = %105
%110 = icmp eq i32 %100, -1
%111 = add nsw i32 %107, -1
%112 = select i1 %110, i32 %111, i32 %100
%113 = add nsw i32 %107, %101
%114 = sub i32 %113, %112
%115 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %107)
br label %116
116: ; preds = %109, %105, %98
%117 = phi i32 [ %114, %109 ], [ %101, %105 ], [ %101, %98 ]
%118 = phi i32 [ %107, %109 ], [ %100, %105 ], [ %100, %98 ]
%119 = shl nuw nsw i64 %99, 22
%120 = getelementptr i8, i8* %41, i64 %119
%121 = call i32 @SHA1_Update(i32* %3, i8* %120, i64 4194304)
%122 = add nsw i32 %117, -1
%123 = call i32 @ivshmem_send(i8* %29, i32 0, i32 %21)
%124 = call i32 @SHA1_Final(i8* %51, i32* %3)
%125 = trunc i64 %99 to i32
%126 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %125)
%127 = load i32, i32* @SHA_DIGEST_LENGTH
%128 = icmp sgt i32 %127, 0
br i1 %128, label %129, label %139
129: ; preds = %116, %129
%130 = phi i64 [ %135, %129 ], [ 0, %116 ]
%131 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 %130
%132 = load i8, i8* %131
%133 = zext i8 %132 to i32
%134 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 %133)
%135 = add nuw nsw i64 %130, 1
%136 = load i32, i32* @SHA_DIGEST_LENGTH
%137 = sext i32 %136 to i64
%138 = icmp slt i64 %135, %137
br i1 %138, label %129, label %139
139: ; preds = %129, %116
%140 = call i32 @putchar(i32 10)
%141 = add nuw nsw i64 %99, 1
%142 = icmp eq i64 %141, %18
br i1 %142, label %143, label %98
143: ; preds = %139, %47, %97
%144 = trunc i64 %22 to i32
%145 = call i32 @munmap(i8* %41, i32 %144)
%146 = call i32 @munmap(i8* %29, i32 256)
%147 = call i32 @close(i32 %13)
%148 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
ret i32 0
}