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

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ ftello(int /*<<< orphan*/ *);
char *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
char *copy_input(char *name, off_t *in_size) {
  FILE *in = fopen(name, "rb");
  if (in == NULL) {
    err(1, "Error opening file");
  }
  fseek(in, 0L, SEEK_END);
  *in_size = ftello(in);
  rewind(in);
  char *buff = mmap(NULL, *in_size, PROT_READ, MAP_PRIVATE, fileno(in), 0);
  if (buff == NULL) {
    err(1, "Error mapping file");
  }
  fclose(in);
  return buff;
}


x86 assembly (clang, -O0):
<copy_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <copy_input+0x40>
mov    $0x1,%edi
lea    0xe48(%rip),%rsi
call   1080 <err@plt>
mov    -0x18(%rbp),%rdi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <fseek@plt>
mov    -0x18(%rbp),%rdi
call   1030 <ftello@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <fileno@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    %eax,%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1252 <copy_input+0xd2>
mov    $0x1,%edi
lea    0xdc9(%rip),%rsi
call   1080 <err@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copy_input>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe6f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11af <copy_input+0x2f>
lea    0xe5e(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1030 <ftello@plt>
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    (%r14),%r14d
mov    0x2e19(%rip),%rax
mov    (%rax),%r15d
mov    0x2de7(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1070 <fileno@plt>
xor    %edi,%edi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1221 <copy_input+0xa1>
lea    0xdff(%rip),%rsi
mov    $0x1,%edi
call   1080 <err@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"Error opening file\00"
@SEEK_END = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [19 x i8] c"Error mapping file\00"
define i8* @copy_input(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %3
store i32* %1, i32** %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @err(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i32*, i32** %5
%15 = load i32, i32* @SEEK_END
%16 = call i32 @fseek(i32* %14, i64 0, i32 %15)
%17 = load i32*, i32** %5
%18 = call i32 @ftello(i32* %17)
%19 = load i32*, i32** %4
store i32 %18, i32* %19
%20 = load i32*, i32** %5
%21 = call i32 @rewind(i32* %20)
%22 = load i32*, i32** %4
%23 = load i32, i32* %22
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @MAP_PRIVATE
%26 = load i32*, i32** %5
%27 = call i32 @fileno(i32* %26)
%28 = call i8* @mmap(i32* null, i32 %23, i32 %24, i32 %25, i32 %27, i32 0)
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %13
%32 = call i32 @err(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %13
%34 = load i32*, i32** %5
%35 = call i32 @fclose(i32* %34)
%36 = load i8*, i8** %6
ret i8* %36
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"Error opening file\00"
@SEEK_END = global i32 0
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.2 = [19 x i8] c"Error mapping file\00"
define i8* @copy_input(i8* nocapture %0, i32* nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %3, i64 0, i32 %8)
%10 = tail call i32 @ftello(i32* %3)
store i32 %10, i32* %1
%11 = tail call i32 @rewind(i32* %3)
%12 = load i32, i32* %1
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @MAP_PRIVATE
%15 = tail call i32 @fileno(i32* %3)
%16 = tail call i8* @mmap(i32* null, i32 %12, i32 %13, i32 %14, i32 %15, i32 0)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %7
%19 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %7
%21 = tail call i32 @fclose(i32* %3)
ret i8* %16
}