#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int S_IWUSR;
int close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fstat(int, struct stat *);
int lseek(int, size_t, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int mmap_file_rw(void **p, char *name, size_t size) {
  int fd = open(name, O_RDWR | O_CREAT | O_TRUNC,
                S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd == -1) {
    perror("open");
    return 0;
  }
  struct stat sb;
  if (fstat(fd, &sb) == -1) {
    perror("fstat");
    close(fd);
    return 0;
  }
  if (!S_ISREG(sb.st_mode)) {
    fprintf(stderr, "%s not a file\n", name);
    close(fd);
    return 0;
  }
  if (lseek(fd, size - 1, SEEK_SET) == -1) {
    perror("lseek");
    close(fd);
    return 0;
  }
  if (write(fd, "", 1) != 1) {
    perror("write");
    close(fd);
    return 0;
  }
  *p = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (*p == MAP_FAILED) {
    perror("mmap");
    close(fd);
    return 0;
  }
  if (close(fd) == -1) {
    perror("close");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<mmap_file_rw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
mov    0x2ded(%rip),%rax
or     (%rax),%edx
mov    0x2dc4(%rip),%rax
or     (%rax),%edx
mov    0x2db3(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1211 <mmap_file_rw+0x81>
lea    0xe00(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <mmap_file_rw+0x212>
mov    -0x24(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1246 <mmap_file_rw+0xb6>
lea    0xdd8(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <mmap_file_rw+0x212>
mov    -0x28(%rbp),%edi
call   1080 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1284 <mmap_file_rw+0xf4>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xda0(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <mmap_file_rw+0x212>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
sub    $0x1,%rsi
mov    0x2d12(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12c6 <mmap_file_rw+0x136>
lea    0xd6d(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <mmap_file_rw+0x212>
mov    -0x24(%rbp),%edi
lea    0xd49(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     1303 <mmap_file_rw+0x173>
lea    0xd36(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <mmap_file_rw+0x212>
mov    -0x20(%rbp),%rsi
mov    0x2cda(%rip),%rax
mov    (%rax),%edx
mov    0x2c79(%rip),%rax
or     (%rax),%edx
mov    0x2cb8(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2c37(%rip),%rcx
cmp    (%rcx),%rax
jne    1372 <mmap_file_rw+0x1e2>
lea    0xccd(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <mmap_file_rw+0x212>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    139b <mmap_file_rw+0x20b>
lea    0xca1(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    13a2 <mmap_file_rw+0x212>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_file_rw>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e2a(%rip),%rcx
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e40(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcf(%rip),%rbp
mov    0x2e00(%rip),%rcx
mov    (%rcx),%ecx
or     0x0(%rbp),%ecx
mov    0x2dd4(%rip),%rbp
or     0x0(%rbp),%ecx
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%r14
mov    0x2db9(%rip),%rdx
or     (%rdx),%ecx
mov    %rsi,%rdi
mov    %eax,%esi
mov    %ecx,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12c4 <mmap_file_rw+0x134>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
je     12d2 <mmap_file_rw+0x142>
mov    (%rsp),%edi
call   1080 <S_ISREG@plt>
test   %eax,%eax
je     12aa <mmap_file_rw+0x11a>
lea    -0x1(%r15),%rsi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     12db <mmap_file_rw+0x14b>
lea    0xdda(%rip),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    12e4 <mmap_file_rw+0x154>
mov    0x2d8d(%rip),%rax
mov    0x2d2e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d6b(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,(%r14)
mov    0x2cf5(%rip),%rcx
cmp    (%rcx),%rax
je     1306 <mmap_file_rw+0x176>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
jne    12f9 <mmap_file_rw+0x169>
lea    0xd83(%rip),%rdi
jmp    12cb <mmap_file_rw+0x13b>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
mov    %rbp,%rdx
call   1070 <fprintf@plt>
jmp    12f0 <mmap_file_rw+0x160>
lea    0xd35(%rip),%rdi
call   10a0 <perror@plt>
jmp    12f7 <mmap_file_rw+0x167>
lea    0xd2c(%rip),%rdi
jmp    12eb <mmap_file_rw+0x15b>
lea    0xd38(%rip),%rdi
jmp    12eb <mmap_file_rw+0x15b>
lea    0xd35(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd19(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    12f9 <mmap_file_rw+0x169>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [6 x i8] c"fstat\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"%s not a file\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"lseek\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [6 x i8] c"write\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.6 = [5 x i8] c"mmap\00"
@.str.7 = [6 x i8] c"close\00"
define i32 @mmap_file_rw(i8** %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.stat
store i8** %0, i8*** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %6
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IROTH
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %10, i32 %15, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %3
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %90
28: ; preds = %3
%29 = load i32, i32* %8
%30 = call i32 @fstat(i32 %29, %struct.stat* %9)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* %8
%35 = call i32 @close(i32 %34)
store i32 0, i32* %4
br label %90
36: ; preds = %28
%37 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @S_ISREG(i32 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %47, label %41
41: ; preds = %36
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %6
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %43)
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 0, i32* %4
br label %90
47: ; preds = %36
%48 = load i32, i32* %8
%49 = load i64, i64* %7
%50 = sub i64 %49, 1
%51 = load i32, i32* @SEEK_SET
%52 = call i32 @lseek(i32 %48, i64 %50, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %58
54: ; preds = %47
%55 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%56 = load i32, i32* %8
%57 = call i32 @close(i32 %56)
store i32 0, i32* %4
br label %90
58: ; preds = %47
%59 = load i32, i32* %8
%60 = call i32 @write(i32 %59, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i32 1)
%61 = icmp ne i32 %60, 1
br i1 %61, label %62, label %66
62: ; preds = %58
%63 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%64 = load i32, i32* %8
%65 = call i32 @close(i32 %64)
store i32 0, i32* %4
br label %90
66: ; preds = %58
%67 = load i64, i64* %7
%68 = load i32, i32* @PROT_READ
%69 = load i32, i32* @PROT_WRITE
%70 = or i32 %68, %69
%71 = load i32, i32* @MAP_SHARED
%72 = load i32, i32* %8
%73 = call i8* @mmap(i32 0, i64 %67, i32 %70, i32 %71, i32 %72, i32 0)
%74 = load i8**, i8*** %5
store i8* %73, i8** %74
%75 = load i8**, i8*** %5
%76 = load i8*, i8** %75
%77 = load i8*, i8** @MAP_FAILED
%78 = icmp eq i8* %76, %77
br i1 %78, label %79, label %83
79: ; preds = %66
%80 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%81 = load i32, i32* %8
%82 = call i32 @close(i32 %81)
store i32 0, i32* %4
br label %90
83: ; preds = %66
%84 = load i32, i32* %8
%85 = call i32 @close(i32 %84)
%86 = icmp eq i32 %85, -1
br i1 %86, label %87, label %89
87: ; preds = %83
%88 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 0, i32* %4
br label %90
89: ; preds = %83
store i32 1, i32* %4
br label %90
90: ; preds = %89, %87, %79, %62, %54, %41, %32, %26
%91 = load i32, i32* %4
ret i32 %91
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [6 x i8] c"fstat\00"
@stderr = global i32 0
@.str.2 = [15 x i8] c"%s not a file\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"lseek\00"
@.str.4 = [1 x i8] zeroinitializer
@.str.5 = [6 x i8] c"write\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.6 = [5 x i8] c"mmap\00"
@.str.7 = [6 x i8] c"close\00"
define i32 @mmap_file_rw(i8** nocapture writeonly %0, i8* %1, i64 %2) {
%4 = alloca %struct.stat
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* %1, i32 %9, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %69
21: ; preds = %3
%22 = bitcast %struct.stat* %4 to i8*
%23 = call i32 @fstat(i32 %17, %struct.stat* %4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @close(i32 %17)
br label %67
28: ; preds = %21
%29 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%30 = load i32, i32* %29
%31 = tail call i32 @S_ISREG(i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%36 = tail call i32 @close(i32 %17)
br label %67
37: ; preds = %28
%38 = add i64 %2, -1
%39 = load i32, i32* @SEEK_SET
%40 = tail call i32 @lseek(i32 %17, i64 %38, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%44 = tail call i32 @close(i32 %17)
br label %67
45: ; preds = %37
%46 = tail call i32 @write(i32 %17, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i32 1)
%47 = icmp eq i32 %46, 1
br i1 %47, label %51, label %48
48: ; preds = %45
%49 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%50 = tail call i32 @close(i32 %17)
br label %67
51: ; preds = %45
%52 = load i32, i32* @PROT_READ
%53 = load i32, i32* @PROT_WRITE
%54 = or i32 %53, %52
%55 = load i32, i32* @MAP_SHARED
%56 = tail call i8* @mmap(i32 0, i64 %2, i32 %54, i32 %55, i32 %17, i32 0)
store i8* %56, i8** %0
%57 = load i8*, i8** @MAP_FAILED
%58 = icmp eq i8* %56, %57
br i1 %58, label %59, label %62
59: ; preds = %51
%60 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%61 = tail call i32 @close(i32 %17)
br label %67
62: ; preds = %51
%63 = tail call i32 @close(i32 %17)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %67
67: ; preds = %62, %65, %59, %48, %42, %33, %25
%68 = phi i32 [ 0, %25 ], [ 0, %42 ], [ 0, %48 ], [ 0, %59 ], [ 0, %65 ], [ 0, %33 ], [ 1, %62 ]
br label %69
69: ; preds = %67, %19
%70 = phi i32 [ 0, %19 ], [ %68, %67 ]
ret i32 %70
}