#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 ssize_t;

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
char *mmap(int /*<<< orphan*/ *, unsigned int, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, unsigned int);
int /*<<< orphan*/ perror(char *);
int write(int, char *, size_t);
int do_mmap(int infd, int outfd, unsigned int size) {
  char *inbuf = mmap(NULL, size, PROT_READ, MAP_SHARED, infd, 0);
  ssize_t ret = 0, off = 0;
  size_t rem;
  if (inbuf == MAP_FAILED) {
    perror("mmap");
    return 1;
  }
  rem = size;
  while (rem > 0) {
    ret = write(outfd, inbuf + off, rem);
    if (ret < 0) {
      perror("write");
      break;
    }
    off += ret;
    rem -= ret;
  }
  munmap(inbuf, size);
  return rem;
}


x86 assembly (clang, -O0):
<do_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%esi
mov    0x2e9d(%rip),%rax
mov    (%rax),%edx
mov    0x2e84(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
jne    11b4 <do_mmap+0x74>
lea    0xe5d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    122b <do_mmap+0xeb>
mov    -0x10(%rbp),%eax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    1218 <do_mmap+0xd8>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11fb <do_mmap+0xbb>
lea    0xe14(%rip),%rdi
call   1060 <perror@plt>
jmp    1218 <do_mmap+0xd8>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    11bb <do_mmap+0x7b>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1050 <munmap@plt>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_mmap>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%r8d
mov    0x2ea0(%rip),%rax
mov    (%rax),%edx
mov    0x2e87(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r12
mov    0x2e56(%rip),%rax
cmp    (%rax),%r12
je     11c6 <do_mmap+0x86>
test   %r14d,%r14d
je     11ae <do_mmap+0x6e>
mov    %r14d,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
movslq %ebx,%rsi
add    %r12,%rsi
mov    %r15d,%edi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %eax,%eax
js     11d9 <do_mmap+0x99>
add    %eax,%ebx
mov    %eax,%eax
sub    %rax,%rbp
jne    1190 <do_mmap+0x50>
xor    %ebp,%ebp
mov    %r12,%rdi
mov    %r14d,%esi
call   1050 <munmap@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe33(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebp
jmp    11bb <do_mmap+0x7b>
lea    0xe25(%rip),%rdi
call   1060 <perror@plt>
jmp    11b0 <do_mmap+0x70>

LLVM IR (-O0):
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @do_mmap(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = load i32, i32* %7
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @MAP_SHARED
%15 = load i32, i32* %5
%16 = call i8* @mmap(i32* null, i32 %12, i32 %13, i32 %14, i32 %15, i32 0)
store i8* %16, i8** %8
store i32 0, i32* %9
store i32 0, i32* %10
%17 = load i8*, i8** %8
%18 = load i8*, i8** @MAP_FAILED
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %54
22: ; preds = %3
%23 = load i32, i32* %7
%24 = zext i32 %23 to i64
store i64 %24, i64* %11
br label %25
25: ; preds = %40, %22
%26 = load i64, i64* %11
%27 = icmp ugt i64 %26, 0
br i1 %27, label %28, label %48
28: ; preds = %25
%29 = load i32, i32* %6
%30 = load i8*, i8** %8
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i64, i64* %11
%35 = call i32 @write(i32 %29, i8* %33, i64 %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %28
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %48
40: ; preds = %28
%41 = load i32, i32* %9
%42 = load i32, i32* %10
%43 = add nsw i32 %42, %41
store i32 %43, i32* %10
%44 = load i32, i32* %9
%45 = sext i32 %44 to i64
%46 = load i64, i64* %11
%47 = sub i64 %46, %45
store i64 %47, i64* %11
br label %25
48: ; preds = %38, %25
%49 = load i8*, i8** %8
%50 = load i32, i32* %7
%51 = call i32 @munmap(i8* %49, i32 %50)
%52 = load i64, i64* %11
%53 = trunc i64 %52 to i32
store i32 %53, i32* %4
br label %54
54: ; preds = %48, %20
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [6 x i8] c"write\00"
define i32 @do_mmap(i32 %0, i32 %1, i32 %2) {
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @MAP_SHARED
%6 = tail call i8* @mmap(i32* null, i32 %2, i32 %4, i32 %5, i32 %0, i32 0)
%7 = load i8*, i8** @MAP_FAILED
%8 = icmp eq i8* %6, %7
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %33
11: ; preds = %3
%12 = icmp eq i32 %2, 0
br i1 %12, label %29, label %13
13: ; preds = %11
%14 = zext i32 %2 to i64
br label %15
15: ; preds = %13, %24
%16 = phi i64 [ %27, %24 ], [ %14, %13 ]
%17 = phi i32 [ %25, %24 ], [ 0, %13 ]
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %6, i64 %18
%20 = tail call i32 @write(i32 %1, i8* %19, i64 %16)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %29
24: ; preds = %15
%25 = add nsw i32 %20, %17
%26 = zext i32 %20 to i64
%27 = sub i64 %16, %26
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %15
29: ; preds = %24, %11, %22
%30 = phi i64 [ %16, %22 ], [ 0, %11 ], [ 0, %24 ]
%31 = tail call i32 @munmap(i8* %6, i32 %2)
%32 = trunc i64 %30 to i32
br label %33
33: ; preds = %29, %9
%34 = phi i32 [ 1, %9 ], [ %32, %29 ]
ret i32 %34
}