#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*/ fname;

/* Variables and functions */
void *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
scalar_t__ ftruncate64(int, unsigned long const);
int /*<<< orphan*/ memcpy(void *, char *, int);
int mkstemp64(char *);
void *mmap(int /*<<< orphan*/ *, unsigned long const, int const, int const, int,
           unsigned long const);
int printmsg(int, char *);
scalar_t__ unlink(char *);
int do_test(void) {
  const int prot = PROT_READ | PROT_WRITE;
  const int flags = MAP_SHARED;
  const unsigned long length = 0x10000;
  const unsigned long offset = 0xace00000;
  const unsigned long size = offset + length;
  void *addr;
  int fd;
  char fname[] = "tst-mmap-offset-XXXXXX";
  fd = mkstemp64(fname);
  if (fd < 0)
    return printmsg(1, "mkstemp");
  if (unlink(fname))
    return printmsg(1, "unlink");
  if (ftruncate64(fd, size))
    return printmsg(0, "ftruncate64");
  addr = mmap(NULL, length, prot, flags, fd, offset);
  if (MAP_FAILED == addr)
    return printmsg(1, "mmap");
  memcpy(addr, fname, sizeof(fname));
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    0x2e58(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movq   $0x10000,-0x18(%rbp)
mov    $0xace00000,%eax
mov    %rax,-0x20(%rbp)
mov    $0xace10000,%eax
mov    %rax,-0x28(%rbp)
mov    0xe56(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe53(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe51(%rip),%eax
mov    %eax,-0x40(%rbp)
mov    0xe4b(%rip),%ax
mov    %ax,-0x3c(%rbp)
mov    0xe43(%rip),%al
mov    %al,-0x3a(%rbp)
lea    -0x50(%rbp),%rdi
call   1050 <mkstemp64@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1205 <do_test+0xa5>
mov    $0x1,%edi
lea    0xe1f(%rip),%rsi
call   1070 <printmsg@plt>
mov    %eax,-0x4(%rbp)
jmp    12c6 <do_test+0x166>
lea    -0x50(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
je     1231 <do_test+0xd1>
mov    $0x1,%edi
lea    0xdfb(%rip),%rsi
call   1070 <printmsg@plt>
mov    %eax,-0x4(%rbp)
jmp    12c6 <do_test+0x166>
mov    -0x34(%rbp),%edi
mov    $0xace10000,%esi
call   1080 <ftruncate64@plt>
cmp    $0x0,%rax
je     125e <do_test+0xfe>
xor    %edi,%edi
lea    0xdd5(%rip),%rsi
call   1070 <printmsg@plt>
mov    %eax,-0x4(%rbp)
jmp    12c6 <do_test+0x166>
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x34(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x10000,%esi
mov    $0xace00000,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
cmp    -0x30(%rbp),%rax
jne    12ad <do_test+0x14d>
mov    $0x1,%edi
lea    0xd92(%rip),%rsi
call   1070 <printmsg@plt>
mov    %eax,-0x4(%rbp)
jmp    12c6 <do_test+0x166>
mov    -0x30(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x17,%edx
call   1060 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e87(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%ebx
or     (%rax),%ebx
mov    0x2e65(%rip),%rax
mov    (%rax),%r14d
movaps 0xe73(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x5858585858582d,%rax
mov    %rax,0xf(%rsp)
mov    %rsp,%rdi
call   1050 <mkstemp64@plt>
test   %eax,%eax
js     11c4 <do_test+0x64>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1030 <unlink@plt>
test   %rax,%rax
je     11e6 <do_test+0x86>
lea    0xe5d(%rip),%rsi
jmp    11cb <do_test+0x6b>
lea    0xe4c(%rip),%rsi
mov    $0x1,%edi
call   1070 <printmsg@plt>
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xace00000,%eax
lea    0x10000(%rax),%rsi
mov    %ebp,%edi
call   1080 <ftruncate64@plt>
test   %rax,%rax
je     1209 <do_test+0xa9>
lea    0xe21(%rip),%rsi
xor    %edi,%edi
jmp    11d0 <do_test+0x70>
xor    %r15d,%r15d
mov    $0x10000,%esi
mov    $0xace00000,%r9d
xor    %edi,%edi
mov    %ebx,%edx
mov    %r14d,%ecx
mov    %ebp,%r8d
call   1040 <mmap@plt>
mov    0x2d9b(%rip),%rcx
cmp    %rax,(%rcx)
je     1244 <do_test+0xe4>
mov    %rsp,%rsi
mov    %rax,%rdi
mov    $0x17,%edx
call   1060 <memcpy@plt>
jmp    11d8 <do_test+0x78>
lea    0xde7(%rip),%rsi
jmp    11cb <do_test+0x6b>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@__const.do_test.fname = [23 x i8] c"tst-mmap-offset-XXXXXX\00"
@.str = [8 x i8] c"mkstemp\00"
@.str.1 = [7 x i8] c"unlink\00"
@.str.2 = [12 x i8] c"ftruncate64\00"
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [23 x i8]
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
store i32 %12, i32* %2
%13 = load i32, i32* @MAP_SHARED
store i32 %13, i32* %3
store i64 65536, i64* %4
store i64 2900361216, i64* %5
store i64 2900426752, i64* %6
%14 = bitcast [23 x i8]* %9 to i8*
%15 = getelementptr inbounds [23 x i8], [23 x i8]* %9, i64 0, i64 0
%16 = call i32 @mkstemp64(i8* %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 @printmsg(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 %20, i32* %1
br label %47
21: ; preds = %0
%22 = getelementptr inbounds [23 x i8], [23 x i8]* %9, i64 0, i64 0
%23 = call i64 @unlink(i8* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @printmsg(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 %26, i32* %1
br label %47
27: ; preds = %21
%28 = load i32, i32* %8
%29 = call i64 @ftruncate64(i32 %28, i64 2900426752)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @printmsg(i32 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 %32, i32* %1
br label %47
33: ; preds = %27
%34 = load i32, i32* %2
%35 = load i32, i32* %3
%36 = load i32, i32* %8
%37 = call i8* @mmap(i32* null, i64 65536, i32 %34, i32 %35, i32 %36, i64 2900361216)
store i8* %37, i8** %7
%38 = load i8*, i8** @MAP_FAILED
%39 = load i8*, i8** %7
%40 = icmp eq i8* %38, %39
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @printmsg(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i32 %42, i32* %1
br label %47
43: ; preds = %33
%44 = load i8*, i8** %7
%45 = getelementptr inbounds [23 x i8], [23 x i8]* %9, i64 0, i64 0
%46 = call i32 @memcpy(i8* %44, i8* %45, i32 23)
store i32 0, i32* %1
br label %47
47: ; preds = %43, %41, %31, %25, %19
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@__const.do_test.fname = [23 x i8] c"tst-mmap-offset-XXXXXX\00"
@.str = [8 x i8] c"mkstemp\00"
@.str.1 = [7 x i8] c"unlink\00"
@.str.2 = [12 x i8] c"ftruncate64\00"
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
define i32 @do_test() {
%1 = alloca [23 x i8]
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @PROT_WRITE
%4 = or i32 %3, %2
%5 = load i32, i32* @MAP_SHARED
%6 = getelementptr inbounds [23 x i8], [23 x i8]* %1, i64 0, i64 0
%7 = call i32 @mkstemp64(i8* %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @printmsg(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %29
11: ; preds = %0
%12 = call i64 @unlink(i8* %6)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @printmsg(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %29
16: ; preds = %11
%17 = call i64 @ftruncate64(i32 %7, i64 2900426752)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @printmsg(i32 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %29
21: ; preds = %16
%22 = call i8* @mmap(i32* null, i64 65536, i32 %4, i32 %5, i32 %7, i64 2900361216)
%23 = load i8*, i8** @MAP_FAILED
%24 = icmp eq i8* %23, %22
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @printmsg(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %29
27: ; preds = %21
%28 = call i32 @memcpy(i8* %22, i8* %6, i32 23)
br label %29
29: ; preds = %27, %25, %19, %14, %9
%30 = phi i32 [ %10, %9 ], [ %15, %14 ], [ %20, %19 ], [ %26, %25 ], [ 0, %27 ]
ret i32 %30
}