#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*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int atoi(char *);
long atol(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int getpagesize();
int ivshmem_recv(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 fd, rv;
  int i, j, k;
  long param;
  int other;
  if (argc != 4) {
    printf("USAGE: sum <filename> <param> <other vm>\n");
    exit(-1);
  }
  fd = open(argv[1], O_RDWR);
  printf("[SUM] opening file %s\n", argv[1]);
  param = atol(argv[2]);
  other = atoi(argv[3]);
  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);
  }
  length = (1024l * 1024l * 4l);
  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("waiting\n");
  rv = ivshmem_recv(fd);
  printf("rv = %d\n", rv);
  munmap(memptr, length);
  munmap(regptr, 256);
  close(fd);
  printf("[SUM] Exiting...\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x4(%rbp)
je     11d4 <main+0x34>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atol@plt>
mov    %rax,-0x50(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x54(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    0x2d82(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x60(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    $0x0,%al
call   10b0 <getpagesize@plt>
mov    -0x60(%rbp),%edx
mov    -0x5c(%rbp),%ecx
mov    -0x58(%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    12b8 <main+0x118>
mov    -0x30(%rbp),%rsi
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
movq   $0x400000,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2d29(%rip),%rax
mov    (%rax),%eax
mov    0x2cf0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2d04(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    $0x0,%al
call   10b0 <getpagesize@plt>
mov    -0x78(%rbp),%rsi
mov    -0x6c(%rbp),%edx
mov    -0x68(%rbp),%ecx
mov    -0x64(%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    134c <main+0x1ac>
mov    -0x28(%rbp),%rsi
lea    0xd23(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    0xd17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%edi
call   1060 <ivshmem_recv@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %eax,%esi
call   1070 <munmap@plt>
mov    -0x30(%rbp),%rdi
mov    $0x100,%esi
call   1070 <munmap@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x4,%edi
jne    12eb <main+0x15b>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r14d
mov    0x8(%rbx),%rsi
lea    0xe39(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe42(%rip),%rdi
mov    $0x400000,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e10(%rip),%rax
mov    0x2dd9(%rip),%r13
mov    0x0(%r13),%ebx
or     (%rax),%ebx
mov    0x2dec(%rip),%r12
mov    (%r12),%ebp
xor    %eax,%eax
call   10a0 <getpagesize@plt>
mov    $0x100,%esi
xor    %edi,%edi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     12c4 <main+0x134>
mov    %rax,%r15
mov    0x0(%r13),%ebp
mov    0x2dc3(%rip),%rax
or     (%rax),%ebp
mov    (%r12),%ebx
xor    %eax,%eax
call   10a0 <getpagesize@plt>
mov    $0x400000,%esi
xor    %edi,%edi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %r14d,%r8d
mov    %eax,%r9d
call   1040 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     12c4 <main+0x134>
mov    %rax,%rbp
lea    0xde1(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1070 <ivshmem_recv@plt>
lea    0xdc4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
mov    $0x400000,%esi
call   1080 <munmap@plt>
mov    %r15,%rdi
mov    $0x100,%esi
call   1080 <munmap@plt>
mov    %r14d,%edi
call   1060 <close@plt>
lea    0xda3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd61(%rip),%rdi
mov    $0xffffffffffffffff,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xd70(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [42 x i8] c"USAGE: sum <filename> <param> <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 = [9 x i8] c"waiting\0A\00"
@.str.5 = [9 x i8] c"rv = %d\0A\00"
@.str.6 = [18 x i8] c"[SUM] Exiting...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%16 = load i32, i32* %3
%17 = icmp ne i32 %16, 4
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %4
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDWR
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %9
%27 = load i8**, i8*** %4
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i8**, i8*** %4
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i64 @atol(i8* %33)
store i64 %34, i64* %14
%35 = load i8**, i8*** %4
%36 = getelementptr inbounds i8*, i8** %35, i64 3
%37 = load i8*, i8** %36
%38 = call i32 @atoi(i8* %37)
store i32 %38, i32* %15
%39 = load i64, i64* %6
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %39)
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @PROT_WRITE
%43 = or i32 %41, %42
%44 = load i32, i32* @MAP_SHARED
%45 = load i32, i32* %9
%46 = call i32 (...) @getpagesize()
%47 = mul nsw i32 0, %46
%48 = call i8* @mmap(i32* null, i64 256, i32 %43, i32 %44, i32 %45, i32 %47)
store i8* %48, i8** %8
%49 = icmp eq i8* %48, inttoptr (i64 -1 to i8*)
br i1 %49, label %50, label %56
50: ; preds = %21
%51 = load i8*, i8** %8
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%53 = load i32, i32* %9
%54 = call i32 @close(i32 %53)
%55 = call i32 @exit(i32 -1)
unreachable
56: ; preds = %21
store i64 4194304, i64* %6
%57 = load i64, i64* %6
%58 = load i32, i32* @PROT_READ
%59 = load i32, i32* @PROT_WRITE
%60 = or i32 %58, %59
%61 = load i32, i32* @MAP_SHARED
%62 = load i32, i32* %9
%63 = call i32 (...) @getpagesize()
%64 = mul nsw i32 1, %63
%65 = call i8* @mmap(i32* null, i64 %57, i32 %60, i32 %61, i32 %62, i32 %64)
store i8* %65, i8** %7
%66 = icmp eq i8* %65, inttoptr (i64 -1 to i8*)
br i1 %66, label %67, label %73
67: ; preds = %56
%68 = load i8*, i8** %7
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %68)
%70 = load i32, i32* %9
%71 = call i32 @close(i32 %70)
%72 = call i32 @exit(i32 -1)
unreachable
73: ; preds = %56
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%75 = load i32, i32* %9
%76 = call i32 @ivshmem_recv(i32 %75)
store i32 %76, i32* %10
%77 = load i32, i32* %10
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %77)
%79 = load i8*, i8** %7
%80 = load i64, i64* %6
%81 = trunc i64 %80 to i32
%82 = call i32 @munmap(i8* %79, i32 %81)
%83 = load i8*, i8** %8
%84 = call i32 @munmap(i8* %83, i32 256)
%85 = load i32, i32* %9
%86 = call i32 @close(i32 %85)
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

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.5 = [9 x i8] c"rv = %d\0A\00"
@str = [8 x i8] c"waiting\00"
@str.7 = [17 x i8] c"[SUM] Exiting...\00"
@str.8 = [41 x i8] c"USAGE: sum <filename> <param> <other vm>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.8, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDWR
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = load i8*, i8** %8
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 4194304)
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_SHARED
%19 = tail call i32 (...) @getpagesize()
%20 = tail call i8* @mmap(i32* null, i64 256, i32 %17, i32 %18, i32 %11, i32 0)
%21 = icmp eq i8* %20, inttoptr (i64 -1 to i8*)
br i1 %21, label %22, label %26
22: ; preds = %7
%23 = 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*))
%24 = tail call i32 @close(i32 %11)
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %7
%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, i64 4194304, i32 %29, i32 %30, i32 %11, i32 %31)
%33 = icmp eq i8* %32, inttoptr (i64 -1 to i8*)
br i1 %33, label %34, label %38
34: ; preds = %26
%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 %11)
%37 = tail call i32 @exit(i32 -1)
unreachable
38: ; preds = %26
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%40 = tail call i32 @ivshmem_recv(i32 %11)
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%42 = tail call i32 @munmap(i8* %32, i32 4194304)
%43 = tail call i32 @munmap(i8* %20, i32 256)
%44 = tail call i32 @close(i32 %11)
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.7, i64 0, i64 0))
ret i32 0
}