#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*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDONLY;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int lseek(int, size_t, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int write(int, char *, int);
void *mmap_fortran(char *filename, size_t bytes, int *file_descr,
                   int read_only) {
  int i;
  int fd;
  int result;
  void *map;
  if (read_only == 1) {
    fd = open(filename, O_RDONLY, (mode_t)0600);
    if (fd == -1) {
      printf("%s:\n", filename);
      perror("Error opening mmap file for reading");
      exit(EXIT_FAILURE);
    }
    map = mmap(NULL, bytes, PROT_READ, MAP_SHARED, fd, 0);
  } else {
    fd = open(filename, O_RDWR | O_CREAT, (mode_t)0600);
    if (fd == -1) {
      printf("%s:\n", filename);
      perror("Error opening mmap file for writing");
      exit(EXIT_FAILURE);
    }
    result = lseek(fd, bytes + 1, SEEK_SET);
    if (result == -1) {
      close(fd);
      printf("%s:\n", filename);
      perror("Error calling lseek() to stretch the file");
      exit(EXIT_FAILURE);
    }
    result = write(fd, "", 1);
    if (result != 1) {
      close(fd);
      printf("%s:\n", filename);
      perror("Error writing last byte of the file");
      exit(EXIT_FAILURE);
    }
    map = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  }
  if (map == MAP_FAILED) {
    close(fd);
    printf("%s:\n", filename);
    perror("Error mmapping the file");
    exit(EXIT_FAILURE);
  }
  *file_descr = fd;
  return map;
}


x86 assembly (clang, -O0):
<mmap_fortran>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    121e <mmap_fortran+0x9e>
mov    -0x8(%rbp),%rdi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ef <mmap_fortran+0x6f>
mov    -0x8(%rbp),%rsi
lea    0xe30(%rip),%rdi
call   1050 <printf@plt>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rsi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
mov    0x2ddd(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
jmp    1353 <mmap_fortran+0x1d3>
mov    -0x8(%rbp),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1275 <mmap_fortran+0xf5>
mov    -0x8(%rbp),%rsi
lea    0xdaa(%rip),%rdi
call   1050 <printf@plt>
lea    0xdc7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    0x2d31(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    12cd <mmap_fortran+0x14d>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%rsi
lea    0xd52(%rip),%rdi
call   1050 <printf@plt>
lea    0xd93(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%edi
lea    0xd2d(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
je     1320 <mmap_fortran+0x1a0>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%rsi
lea    0xcff(%rip),%rdi
call   1050 <printf@plt>
lea    0xd6a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c97(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rsi
mov    0x2cc5(%rip),%rax
mov    (%rax),%edx
mov    0x2c74(%rip),%rax
or     (%rax),%edx
mov    0x2ca3(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2c42(%rip),%rcx
cmp    (%rcx),%rax
jne    1399 <mmap_fortran+0x219>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%rsi
lea    0xc86(%rip),%rdi
call   1050 <printf@plt>
lea    0xd15(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c1e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_fortran>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
cmp    $0x1,%ecx
jne    11c1 <mmap_fortran+0x41>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
mov    $0x180,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1288 <mmap_fortran+0x108>
mov    %eax,%ebp
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
jmp    1236 <mmap_fortran+0xb6>
mov    0x2e08(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x180,%edx
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     12a0 <mmap_fortran+0x120>
mov    %eax,%ebp
lea    0x1(%rbx),%rsi
mov    0x2dc2(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
je     12b8 <mmap_fortran+0x138>
lea    0xdf5(%rip),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    12d7 <mmap_fortran+0x157>
mov    0x2dc5(%rip),%rax
mov    0x2d76(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da3(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2d4a(%rip),%rcx
cmp    (%rcx),%rax
je     1269 <mmap_fortran+0xe9>
mov    %ebp,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xd89(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
lea    0xe15(%rip),%rdi
jmp    12f4 <mmap_fortran+0x174>
lea    0xd71(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
lea    0xd67(%rip),%rdi
jmp    12f4 <mmap_fortran+0x174>
lea    0xd59(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
lea    0xd73(%rip),%rdi
jmp    12f4 <mmap_fortran+0x174>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xd3a(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
lea    0xd78(%rip),%rdi
jmp    12f4 <mmap_fortran+0x174>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xd1b(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
lea    0xd83(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"%s:\0A\00"
@.str.1 = [36 x i8] c"Error opening mmap file for reading\00"
@EXIT_FAILURE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [36 x i8] c"Error opening mmap file for writing\00"
@SEEK_SET = global i32 0
@.str.3 = [42 x i8] c"Error calling lseek() to stretch the file\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [36 x i8] c"Error writing last byte of the file\00"
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.6 = [24 x i8] c"Error mmapping the file\00"
define i8* @mmap_fortran(i8* %0, i64 %1, i32* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %33
15: ; preds = %4
%16 = load i8*, i8** %5
%17 = load i32, i32* @O_RDONLY
%18 = call i32 @open(i8* %16, i32 %17, i32 384)
store i32 %18, i32* %10
%19 = load i32, i32* %10
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %15
%22 = load i8*, i8** %5
%23 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %15
%28 = load i64, i64* %6
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_SHARED
%31 = load i32, i32* %10
%32 = call i8* @mmap(i32* null, i64 %28, i32 %29, i32 %30, i32 %31, i32 0)
store i8* %32, i8** %12
br label %84
33: ; preds = %4
%34 = load i8*, i8** %5
%35 = load i32, i32* @O_RDWR
%36 = load i32, i32* @O_CREAT
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %34, i32 %37, i32 384)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %47
41: ; preds = %33
%42 = load i8*, i8** %5
%43 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %42)
%44 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %33
%48 = load i32, i32* %10
%49 = load i64, i64* %6
%50 = add i64 %49, 1
%51 = load i32, i32* @SEEK_SET
%52 = call i32 @lseek(i32 %48, i64 %50, i32 %51)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %63
55: ; preds = %47
%56 = load i32, i32* %10
%57 = call i32 @close(i32 %56)
%58 = load i8*, i8** %5
%59 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %58)
%60 = call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %47
%64 = load i32, i32* %10
%65 = call i32 @write(i32 %64, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i32 1)
store i32 %65, i32* %11
%66 = load i32, i32* %11
%67 = icmp ne i32 %66, 1
br i1 %67, label %68, label %76
68: ; preds = %63
%69 = load i32, i32* %10
%70 = call i32 @close(i32 %69)
%71 = load i8*, i8** %5
%72 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %71)
%73 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %63
%77 = load i64, i64* %6
%78 = load i32, i32* @PROT_READ
%79 = load i32, i32* @PROT_WRITE
%80 = or i32 %78, %79
%81 = load i32, i32* @MAP_SHARED
%82 = load i32, i32* %10
%83 = call i8* @mmap(i32* null, i64 %77, i32 %80, i32 %81, i32 %82, i32 0)
store i8* %83, i8** %12
br label %84
84: ; preds = %76, %27
%85 = load i8*, i8** %12
%86 = load i8*, i8** @MAP_FAILED
%87 = icmp eq i8* %85, %86
br i1 %87, label %88, label %96
88: ; preds = %84
%89 = load i32, i32* %10
%90 = call i32 @close(i32 %89)
%91 = load i8*, i8** %5
%92 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %91)
%93 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%94 = load i32, i32* @EXIT_FAILURE
%95 = call i32 @exit(i32 %94)
unreachable
96: ; preds = %84
%97 = load i32, i32* %10
%98 = load i32*, i32** %7
store i32 %97, i32* %98
%99 = load i8*, i8** %12
ret i8* %99
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [5 x i8] c"%s:\0A\00"
@.str.1 = [36 x i8] c"Error opening mmap file for reading\00"
@EXIT_FAILURE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.2 = [36 x i8] c"Error opening mmap file for writing\00"
@SEEK_SET = global i32 0
@.str.3 = [42 x i8] c"Error calling lseek() to stretch the file\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [36 x i8] c"Error writing last byte of the file\00"
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.6 = [24 x i8] c"Error mmapping the file\00"
define i8* @mmap_fortran(i8* %0, i64 %1, i32* nocapture writeonly %2, i32 %3) {
%5 = icmp eq i32 %3, 1
br i1 %5, label %6, label %19
6: ; preds = %4
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %0, i32 %7, i32 384)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %6
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @MAP_SHARED
%18 = tail call i8* @mmap(i32* null, i64 %1, i32 %16, i32 %17, i32 %8, i32 0)
br label %56
19: ; preds = %4
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_CREAT
%22 = or i32 %21, %20
%23 = tail call i32 @open(i8* %0, i32 %22, i32 384)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
%27 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %19
%31 = add i64 %1, 1
%32 = load i32, i32* @SEEK_SET
%33 = tail call i32 @lseek(i32 %23, i64 %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = tail call i32 @close(i32 %23)
%37 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
%38 = tail call i32 @perror(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = tail call i32 @exit(i32 %39)
unreachable
41: ; preds = %30
%42 = tail call i32 @write(i32 %23, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i32 1)
%43 = icmp eq i32 %42, 1
br i1 %43, label %50, label %44
44: ; preds = %41
%45 = tail call i32 @close(i32 %23)
%46 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
%47 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = tail call i32 @exit(i32 %48)
unreachable
50: ; preds = %41
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @PROT_WRITE
%53 = or i32 %52, %51
%54 = load i32, i32* @MAP_SHARED
%55 = tail call i8* @mmap(i32* null, i64 %1, i32 %53, i32 %54, i32 %23, i32 0)
br label %56
56: ; preds = %50, %15
%57 = phi i32 [ %8, %15 ], [ %23, %50 ]
%58 = phi i8* [ %18, %15 ], [ %55, %50 ]
%59 = load i8*, i8** @MAP_FAILED
%60 = icmp eq i8* %58, %59
br i1 %60, label %61, label %67
61: ; preds = %56
%62 = tail call i32 @close(i32 %57)
%63 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
%64 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = tail call i32 @exit(i32 %65)
unreachable
67: ; preds = %56
store i32 %57, i32* %2
ret i8* %58
}