#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 */
int EXIT_FAILURE;
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
void *ipc_map(int fd, int size) {
  void *data;
  struct stat sb;
  data = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (data == MAP_FAILED)
    do {
      perror("mmap");
      exit(EXIT_FAILURE);
    } while (0);
  if (fstat(fd, &sb) == -1)
    do {
      perror("fstat2");
      exit(EXIT_FAILURE);
    } while (0);
  if (!S_ISREG(sb.st_mode)) {
    fprintf(stderr, "is not a file2\n");
    exit(1);
  }
  return data;
}


x86 assembly (clang, -O0):
<ipc_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
mov    0x2e3f(%rip),%rax
or     (%rax),%edx
mov    0x2e5e(%rip),%rax
mov    (%rax),%ecx
mov    -0x4(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
jne    11d2 <ipc_map+0x72>
jmp    11b8 <ipc_map+0x58>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1206 <ipc_map+0xa6>
jmp    11ec <ipc_map+0x8c>
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1236 <ipc_map+0xd6>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_map>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2e8c(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e21(%rip),%rax
cmp    (%rax),%rbx
je     11c1 <ipc_map+0x61>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     11ca <ipc_map+0x6a>
mov    (%rsp),%edi
call   1050 <S_ISREG@plt>
test   %eax,%eax
je     11e4 <ipc_map+0x84>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe38(%rip),%rdi
jmp    11d1 <ipc_map+0x71>
lea    0xe34(%rip),%rdi
call   1060 <perror@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"fstat2\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"is not a file2\0A\00"
define i8* @ipc_map(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_SHARED
%12 = load i32, i32* %3
%13 = call i8* @mmap(i32 0, i32 %7, i32 %10, i32 %11, i32 %12, i32 0)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = load i8*, i8** @MAP_FAILED
%16 = icmp eq i8* %14, %15
br i1 %16, label %17, label %23
17: ; preds = %2
br label %18
18: ; preds = %17
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; No predecessors!
br label %23
23: ; preds = %22, %2
%24 = load i32, i32* %3
%25 = call i32 @fstat(i32 %24, %struct.stat* %6)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %33
27: ; preds = %23
br label %28
28: ; preds = %27
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; No predecessors!
br label %33
33: ; preds = %32, %23
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @S_ISREG(i32 %35)
%37 = icmp ne i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %33
%43 = load i8*, i8** %5
ret i8* %43
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"fstat2\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"is not a file2\0A\00"
define i8* @ipc_map(i32 %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_SHARED
%9 = tail call i8* @mmap(i32 0, i32 %1, i32 %7, i32 %8, i32 %0, i32 0)
%10 = load i8*, i8** @MAP_FAILED
%11 = icmp eq i8* %9, %10
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = call i32 @fstat(i32 %0, %struct.stat* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %16
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = tail call i32 @S_ISREG(i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %23
ret i8* %9
}