#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;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/, int /*<<< orphan*/, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strncpy(void *, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  struct stat mystat;
  void *pmap;
  fd = open("test.txt", O_RDWR);
  if (fd == -1) {
    perror("open");
    exit(1);
  }
  if (fstat(fd, &mystat) < 0) {
    perror("fstat");
    close(fd);
    exit(1);
  }
  pmap = mmap(0, mystat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (pmap == MAP_FAILED) {
    perror("mmap");
    close(fd);
    exit(1);
  }
  strncpy(pmap, "That", 4);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11be <main+0x4e>
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    11f2 <main+0x82>
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%esi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
mov    0x2dc3(%rip),%rax
or     (%rax),%edx
mov    0x2dda(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d92(%rip),%rcx
cmp    (%rcx),%rax
jne    1255 <main+0xe5>
lea    0xdd6(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xdb9(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11fd <main+0x8d>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <fstat@plt>
test   %rax,%rax
js     1213 <main+0xa3>
mov    0x8(%rsp),%esi
mov    0x2e4b(%rip),%rax
mov    0x2e14(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e29(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
je     121c <main+0xac>
lea    0xe38(%rip),%rsi
mov    %rax,%rdi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe05(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdf4(%rip),%rdi
jmp    1223 <main+0xb3>
lea    0xdf1(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [5 x i8] c"That\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i64 @fstat(i32 %17, %struct.stat* %7)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @PROT_WRITE
%30 = or i32 %28, %29
%31 = load i32, i32* @MAP_SHARED
%32 = load i32, i32* %6
%33 = call i8* @mmap(i32 0, i32 %27, i32 %30, i32 %31, i32 %32, i32 0)
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %42
37: ; preds = %25
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* %6
%40 = call i32 @close(i32 %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %25
%43 = load i8*, i8** %8
%44 = call i32 @strncpy(i8* %43, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [5 x i8] c"That\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = call i64 @fstat(i32 %6, %struct.stat* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @close(i32 %6)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @PROT_READ
%22 = load i32, i32* @PROT_WRITE
%23 = or i32 %22, %21
%24 = load i32, i32* @MAP_SHARED
%25 = tail call i8* @mmap(i32 0, i32 %20, i32 %23, i32 %24, i32 %6, i32 0)
%26 = load i8*, i8** @MAP_FAILED
%27 = icmp eq i8* %25, %26
br i1 %27, label %28, label %32
28: ; preds = %18
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @close(i32 %6)
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %18
%33 = tail call i32 @strncpy(i8* %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%34 = tail call i32 @close(i32 %6)
ret i32 0
}