#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_size;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ exit(int);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
void *mmap(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void *mmap_stdin(off_t *stdin_size) {
  void *result;
  struct stat stat;
  if (fstat(0, &stat) < 0) {
    perror("fstat");
    exit(1);
  }
  *stdin_size = stat.st_size;
  result = mmap(0, stat.st_size, PROT_READ, MAP_PRIVATE, 0, 0);
  if (result == (void *)-1) {
    perror("mmap");
    exit(1);
  }
  return result;
}


x86 assembly (clang, -O0):
<mmap_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
jge    1177 <mmap_stdin+0x37>
lea    0xe98(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x18(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%esi
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
xor    %r9d,%r9d
mov    %r9d,%edi
mov    %r9d,%r8d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    11ce <mmap_stdin+0x8e>
lea    0xe47(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_stdin>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
xor    %edi,%edi
call   1060 <fstat@plt>
test   %rax,%rax
js     118a <mmap_stdin+0x4a>
mov    0x8(%rsp),%esi
mov    %esi,(%rbx)
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
mov    0x2e69(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1193 <mmap_stdin+0x53>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe6f(%rip),%rdi
jmp    119a <mmap_stdin+0x5a>
lea    0xe6c(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [5 x i8] c"mmap\00"
define i8* @mmap_stdin(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca %struct.stat
store i32* %0, i32** %2
%5 = call i64 @fstat(i32 0, %struct.stat* %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load i32*, i32** %2
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @MAP_PRIVATE
%18 = call i8* @mmap(i32 0, i32 %15, i32 %16, i32 %17, i32 0, i32 0)
store i8* %18, i8** %3
%19 = load i8*, i8** %3
%20 = icmp eq i8* %19, inttoptr (i64 -1 to i8*)
br i1 %20, label %21, label %24
21: ; preds = %10
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %10
%25 = load i8*, i8** %3
ret i8* %25
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [5 x i8] c"mmap\00"
define i8* @mmap_stdin(i32* nocapture writeonly %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @fstat(i32 0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %0
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @MAP_PRIVATE
%14 = tail call i8* @mmap(i32 0, i32 %11, i32 %12, i32 %13, i32 0, i32 0)
%15 = icmp eq i8* %14, inttoptr (i64 -1 to i8*)
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %9
ret i8* %14
}