#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*/ PROT_WRITE;
int fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int modify_file_using_mmap(int fd) {
  struct stat st;
  int ret = fstat(fd, &st);
  if (ret) {
    perror("fstat");
    return -1;
  }
  void *map_addr = mmap(NULL, st.st_size, PROT_WRITE, MAP_SHARED, fd, 0);
  if (map_addr == MAP_FAILED) {
    perror("mmap");
    return -1;
  }
  char *buf = map_addr;
  *buf = 'J';
  return 0;
}


x86 assembly (clang, -O0):
<modify_file_using_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <fstat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     116c <modify_file_using_mmap+0x3c>
lea    0xea5(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <modify_file_using_mmap+0xa7>
mov    -0x10(%rbp),%esi
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    0x2e71(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
jne    11c1 <modify_file_using_mmap+0x91>
lea    0xe56(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <modify_file_using_mmap+0xa7>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x4a,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<modify_file_using_mmap>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    %rsp,%rsi
call   1050 <fstat@plt>
test   %eax,%eax
jne    117d <modify_file_using_mmap+0x4d>
mov    (%rsp),%esi
mov    0x2e8d(%rip),%rax
mov    (%rax),%edx
mov    0x2e9c(%rip),%rax
mov    (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%rax
je     1186 <modify_file_using_mmap+0x56>
movb   $0x4a,(%rax)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe7c(%rip),%rdi
jmp    118d <modify_file_using_mmap+0x5d>
lea    0xe79(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1174 <modify_file_using_mmap+0x44>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"fstat\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define i32 @modify_file_using_mmap(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @fstat(i32 %8, %struct.stat* %4)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %29
14: ; preds = %1
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @PROT_WRITE
%18 = load i32, i32* @MAP_SHARED
%19 = load i32, i32* %3
%20 = call i8* @mmap(i32* null, i32 %16, i32 %17, i32 %18, i32 %19, i32 0)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = load i8*, i8** @MAP_FAILED
%23 = icmp eq i8* %21, %22
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %29
26: ; preds = %14
%27 = load i8*, i8** %6
store i8* %27, i8** %7
%28 = load i8*, i8** %7
store i8 74, i8* %28
store i32 0, i32* %2
br label %29
29: ; preds = %26, %24, %12
%30 = load i32, i32* %2
ret i32 %30
}

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