#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 O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_SET;
unsigned long __NR_dup;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int /*<<< orphan*/);
int open(char *, int, ...);
int syscall(unsigned long, unsigned long, unsigned long, unsigned long,
            unsigned long, unsigned long, unsigned long);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd, ret;
  struct stat fs;
  void *r;
  int rc;
  fd = open("foobar", O_WRONLY | O_CREAT | O_TRUNC, 0600);
  lseek(fd, 1024, SEEK_SET);
  write(fd, "abcdef", 6);
  close(fd);
  fd = open("foobar", O_RDONLY);
  ret = fstat(fd, &fs);
  r = mmap(NULL, fs.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (r != MAP_FAILED)
    munmap(r, fs.st_size);
  close(fd);
  rc = syscall(
      __NR_dup, (unsigned long)-12345, (unsigned long)0xffffffffffffffff,
      (unsigned long)0xa5a5a5a5a5a5a5a5, (unsigned long)0xf0f0f0f0f0f0f0f0,
      (unsigned long)0x5a5a5a5a5a5a5a5a, (unsigned long)0xe38e38e38e38e38e);
  rc = syscall(
      (unsigned long)0xffff, (unsigned long)0x1c71c71c71c71c71,
      (unsigned long)0x0f0f0f0f0f0f0f0f, (unsigned long)0xdb6db6db6db6db6d,
      (unsigned long)0x2492492492492492, (unsigned long)0xad6b5ad6b5ad6b5a,
      (unsigned long)0xdef7ddef7ddef7dd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
lea    0xe4f(%rip),%rdi
mov    $0x180,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1050 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    0xe27(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10a0 <fstat@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    0x2daa(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%rax
je     1264 <main+0xe4>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1080 <munmap@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%rdi
mov    $0xffffffffffffcfc7,%rsi
mov    $0xffffffffffffffff,%rdx
movabs $0xa5a5a5a5a5a5a5a5,%rcx
a5 a5 a5
movabs $0xf0f0f0f0f0f0f0f0,%r8
f0 f0 f0
movabs $0x5a5a5a5a5a5a5a5a,%r9
5a 5a 5a
movabs $0xe38e38e38e38e38e,%rax
38 8e e3
mov    %rax,(%rsp)
call   1070 <syscall@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xffff,%edi
movabs $0x1c71c71c71c71c71,%rsi
c7 71 1c
movabs $0xf0f0f0f0f0f0f0f,%rdx
0f 0f 0f
movabs $0xdb6db6db6db6db6d,%rcx
b6 6d db
movabs $0x2492492492492492,%r8
49 92 24
movabs $0xad6b5ad6b5ad6b5a,%r9
5a 6b ad
movabs $0xdef7ddef7ddef7dd,%rax
dd f7 de
mov    %rax,(%rsp)
call   1070 <syscall@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e0b(%rip),%rax
mov    0x2e64(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e51(%rip),%rax
or     (%rax),%esi
lea    0xe58(%rip),%rbx
mov    %rbx,%rdi
mov    $0x180,%edx
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebp
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x400,%esi
call   1050 <lseek@plt>
lea    0xe32(%rip),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
mov    0x10(%rsp),%ebp
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    0x2dbb(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2d73(%rip),%rcx
cmp    (%rcx),%rax
je     1244 <main+0xc4>
mov    %rax,%rdi
mov    %ebp,%esi
call   1080 <munmap@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%rdi
movabs $0xe38e38e38e38e38e,%rax
38 8e e3
mov    %rax,(%rsp)
movabs $0xa5a5a5a5a5a5a5a5,%rcx
a5 a5 a5
movabs $0xf0f0f0f0f0f0f0f0,%r8
f0 f0 f0
movabs $0x5a5a5a5a5a5a5a5a,%r9
5a 5a 5a
mov    $0xffffffffffffcfc7,%rsi
mov    $0xffffffffffffffff,%rdx
call   1070 <syscall@plt>
movabs $0xdef7ddef7ddef7dd,%rax
dd f7 de
mov    %rax,(%rsp)
movabs $0x1c71c71c71c71c71,%rsi
c7 71 1c
movabs $0xf0f0f0f0f0f0f0f,%rdx
0f 0f 0f
movabs $0xdb6db6db6db6db6d,%rcx
b6 6d db
movabs $0x2492492492492492,%r8
49 92 24
movabs $0xad6b5ad6b5ad6b5a,%r9
5a 6b ad
mov    $0xffff,%edi
call   1070 <syscall@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"foobar\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [7 x i8] c"abcdef\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@__NR_dup = global i64 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %11, i32 384)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @lseek(i32 %13, i32 1024, i32 %14)
%16 = load i32, i32* %2
%17 = call i32 @write(i32 %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* @O_RDONLY
%21 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = call i32 @fstat(i32 %22, %struct.stat* %4)
store i32 %23, i32* %3
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* %2
%29 = call i8* @mmap(i32* null, i32 %25, i32 %26, i32 %27, i32 %28, i32 0)
store i8* %29, i8** %5
%30 = load i8*, i8** %5
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp ne i8* %30, %31
br i1 %32, label %33, label %38
33: ; preds = %0
%34 = load i8*, i8** %5
%35 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @munmap(i8* %34, i32 %36)
br label %38
38: ; preds = %33, %0
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
%41 = load i64, i64* @__NR_dup
%42 = call i32 @syscall(i64 %41, i64 -12345, i64 -1, i64 -6510615555426900571, i64 -1085102592571150096, i64 6510615555426900570, i64 -2049638230412172402)
store i32 %42, i32* %6
%43 = call i32 @syscall(i64 65535, i64 2049638230412172401, i64 1085102592571150095, i64 -2635249153387078803, i64 2635249153387078802, i64 -5950562604422436006, i64 -2380189857363331107)
store i32 %43, i32* %6
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"foobar\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@SEEK_SET = global i32 0
@.str.1 = [7 x i8] c"abcdef\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@__NR_dup = global i64 0
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %7, i32 384)
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @lseek(i32 %8, i32 1024, i32 %9)
%11 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 6)
%12 = tail call i32 @close(i32 %8)
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @fstat(i32 %14, %struct.stat* %1)
%16 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @PROT_READ
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i8* @mmap(i32* null, i32 %17, i32 %18, i32 %19, i32 %14, i32 0)
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %25, label %23
23: ; preds = %0
%24 = tail call i32 @munmap(i8* %20, i32 %17)
br label %25
25: ; preds = %23, %0
%26 = tail call i32 @close(i32 %14)
%27 = load i64, i64* @__NR_dup
%28 = tail call i32 @syscall(i64 %27, i64 -12345, i64 -1, i64 -6510615555426900571, i64 -1085102592571150096, i64 6510615555426900570, i64 -2049638230412172402)
%29 = tail call i32 @syscall(i64 65535, i64 2049638230412172401, i64 1085102592571150095, i64 -2635249153387078803, i64 2635249153387078802, i64 -5950562604422436006, i64 -2380189857363331107)
ret i32 0
}