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

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strlen(char *);
int main(int argc, char *argv[]) {
  char buf[] = "hello world, hello world1234";
  printf("1 failed\n");
  int fd = open("a.txt", O_RDWR, 00664);
  printf("2 failed\n");
  if (fd < 0) {
    printf("open failed\n");
    return -1;
  }
  printf("3 failed\n");
  char *addr =
      (char *)mmap(NULL, 1024, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
  printf("4 failed\n");
  if (addr == (char *)-1) {
    printf("mmap failed\n");
    return -1;
  }
  printf("5 %d  failed\n", (int)strlen(buf));
  printf("x %ld  failed\n", sizeof(*addr));
  printf("x %ld  failed\n", sizeof(addr));
  printf("6 failed\n");
  printf("7 failed\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe93(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe90(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe8d(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe8b(%rip),%eax
mov    %eax,-0x18(%rbp)
mov    0xe86(%rip),%al
mov    %al,-0x14(%rbp)
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0xe70(%rip),%rdi
mov    $0x1b4,%edx
call   1070 <open@plt>
mov    %eax,-0x34(%rbp)
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x34(%rbp)
jge    11f6 <main+0xa6>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <main+0x18b>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
mov    0x2de4(%rip),%rax
or     (%rax),%edx
mov    0x2dcb(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x400,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x40(%rbp)
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x40(%rbp)
jne    1271 <main+0x121>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <main+0x18b>
lea    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xde2(%rip),%rdi
mov    $0x1,%esi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdcf(%rip),%rdi
mov    $0x8,%esi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xec8(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
mov    $0x1b4,%edx
call   1070 <open@plt>
mov    %eax,%ebp
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
test   %ebp,%ebp
js     123a <main+0xea>
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e25(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e33(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    $0x400,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
cmp    $0xffffffffffffffff,%rbx
je     1243 <main+0xf3>
lea    0xe1e(%rip),%rdi
mov    $0x1c,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe19(%rip),%rbx
mov    $0x1,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x8,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe27(%rip),%rdi
call   1030 <puts@plt>
lea    0xe24(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1255 <main+0x105>
lea    0xe24(%rip),%rdi
jmp    124a <main+0xfa>
lea    0xe0f(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.buf = [29 x i8] c"hello world, hello world1234\00"
@.str = [10 x i8] c"1 failed\0A\00"
@.str.1 = [6 x i8] c"a.txt\00"
@O_RDWR = global i32 0
@.str.2 = [10 x i8] c"2 failed\0A\00"
@.str.3 = [13 x i8] c"open failed\0A\00"
@.str.4 = [10 x i8] c"3 failed\0A\00"
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.5 = [10 x i8] c"4 failed\0A\00"
@.str.6 = [13 x i8] c"mmap failed\0A\00"
@.str.7 = [14 x i8] c"5 %d failed\0A\00"
@.str.8 = [15 x i8] c"x %ld failed\0A\00"
@.str.9 = [10 x i8] c"6 failed\0A\00"
@.str.10 = [10 x i8] c"7 failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [29 x i8]
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [29 x i8]* %6 to i8*
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 436)
store i32 %12, i32* %7
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
%20 = load i32, i32* @PROT_WRITE
%21 = load i32, i32* @PROT_READ
%22 = or i32 %20, %21
%23 = load i32, i32* @MAP_SHARED
%24 = load i32, i32* %7
%25 = call i64 @mmap(i32* null, i32 1024, i32 %22, i32 %23, i32 %24, i32 0)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %8
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%28 = load i8*, i8** %8
%29 = icmp eq i8* %28, inttoptr (i64 -1 to i8*)
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %43
32: ; preds = %18
%33 = getelementptr inbounds [29 x i8], [29 x i8]* %6, i64 0, i64 0
%34 = call i64 @strlen(i8* %33)
%35 = trunc i64 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i64 1)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i64 8)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.9, i64 0, i64 0))
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0))
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
store i32 0, i32* %3
br label %43
43: ; preds = %32, %30, %16
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"a.txt\00"
@O_RDWR = global i32 0
@PROT_WRITE = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.7 = [14 x i8] c"5 %d failed\0A\00"
@.str.8 = [15 x i8] c"x %ld failed\0A\00"
@str = [9 x i8] c"1 failed\00"
@str.11 = [9 x i8] c"2 failed\00"
@str.12 = [9 x i8] c"3 failed\00"
@str.13 = [9 x i8] c"4 failed\00"
@str.14 = [9 x i8] c"6 failed\00"
@str.15 = [9 x i8] c"7 failed\00"
@str.16 = [12 x i8] c"mmap failed\00"
@str.17 = [12 x i8] c"open failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 436)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.11, i64 0, i64 0))
%7 = icmp slt i32 %5, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.17, i64 0, i64 0))
br label %29
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.12, i64 0, i64 0))
%12 = load i32, i32* @PROT_WRITE
%13 = load i32, i32* @PROT_READ
%14 = or i32 %13, %12
%15 = load i32, i32* @MAP_SHARED
%16 = tail call i64 @mmap(i32* null, i32 1024, i32 %14, i32 %15, i32 %5, i32 0)
%17 = inttoptr i64 %16 to i8*
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.13, i64 0, i64 0))
%19 = icmp eq i8* %17, inttoptr (i64 -1 to i8*)
br i1 %19, label %20, label %22
20: ; preds = %10
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.16, i64 0, i64 0))
br label %29
22: ; preds = %10
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0), i32 28)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i64 1)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i64 8)
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.14, i64 0, i64 0))
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.15, i64 0, i64 0))
%28 = tail call i32 @close(i32 %5)
br label %29
29: ; preds = %20, %22, %8
%30 = phi i32 [ -1, %8 ], [ -1, %20 ], [ 0, %22 ]
ret i32 %30
}