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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, void *, int);
int write(int, int *, int);
int main(int argc, char **argv) {
  int rc;
  int fd = open(argv[1], O_WRONLY);
  if (fd == -1) {
    perror(argv[1]);
    goto read;
  }
  rc = write(fd, (void *)0x4096000, 5);
  if (rc != 5)
    perror("write badarea (Should have failed)");
  rc = write(fd, &fd, 0);
  if (rc != 0)
    perror("write zero bytes");
  rc = write(fd, &fd, 1);
  if (rc != 1)
    perror("write one byte");
  rc = write(fd, &fd, 2UL * 1024 * 1024);
  if (rc != 2UL * 1024 * 1024)
    perror("write 2M");
  rc = write(fd, &fd, 2UL * 1024 * 1024 * 1024);
  if (rc != 2UL * 1024 * 1024 * 1024)
    perror("write 2G");
  rc = write(fd, &fd, -2);
  if (rc != -2)
    perror("write -2");
  close(fd);
read:
  fd = open(argv[1], O_RDONLY);
  if (fd == -1)
    return 0;
  rc = read(fd, (void *)0x4096000, 5);
  perror("read");
  close(fd);
  if (!rc)
    return -1;
  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    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    119b <main+0x4b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <perror@plt>
jmp    12ab <main+0x15b>
mov    -0x18(%rbp),%edi
mov    $0x4096000,%esi
mov    $0x5,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
je     11c6 <main+0x76>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11ed <main+0x9d>
lea    0xe3b(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
je     1217 <main+0xc7>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x200000,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
cmp    $0x200000,%rax
je     1247 <main+0xf7>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x80000000,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
mov    $0x80000000,%ecx
cmp    %rcx,%rax
je     1279 <main+0x129>
lea    0xdd8(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xfffffffe,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xfffffffe,-0x14(%rbp)
je     12a3 <main+0x153>
lea    0xdb7(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d2e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12da <main+0x18a>
movl   $0x0,-0x4(%rbp)
jmp    1320 <main+0x1d0>
mov    -0x18(%rbp),%edi
mov    $0x4096000,%esi
mov    $0x5,%edx
call   1050 <read@plt>
mov    %eax,-0x14(%rbp)
lea    0xd68(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1319 <main+0x1c9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1320 <main+0x1d0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
jne    1184 <main+0x34>
mov    0x8(%rbx),%rdi
call   1070 <perror@plt>
jmp    1247 <main+0xf7>
mov    %eax,%ebp
mov    $0x4096000,%esi
mov    %eax,%edi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0x5,%eax
je     11a8 <main+0x58>
lea    0xe5d(%rip),%rdi
call   1070 <perror@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <write@plt>
test   %eax,%eax
je     11c6 <main+0x76>
lea    0xe62(%rip),%rdi
call   1070 <perror@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     11e8 <main+0x98>
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
mov    $0x200000,%edx
call   1030 <write@plt>
cmp    $0x200000,%eax
je     120c <main+0xbc>
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
lea    0xc(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x80000000,%edx
call   1030 <write@plt>
lea    0xe25(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xfffffffe,%edx
call   1030 <write@plt>
cmp    $0xfffffffe,%eax
jne    1298 <main+0x148>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x8(%rbx),%rdi
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
xor    %eax,%eax
cmp    $0xffffffff,%ebx
jne    126b <main+0x11b>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0x4096000,%esi
mov    %ebx,%edi
mov    $0x5,%edx
call   1050 <read@plt>
mov    %eax,%ebp
lea    0xdd9(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1040 <close@plt>
cmp    $0x1,%ebp
sbb    %eax,%eax
jmp    1262 <main+0x112>
lea    0xdb6(%rip),%rdi
call   1070 <perror@plt>
jmp    1240 <main+0xf0>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@.str = [35 x i8] c"write badarea (Should have failed)\00"
@.str.1 = [17 x i8] c"write zero bytes\00"
@.str.2 = [15 x i8] c"write one byte\00"
@.str.3 = [9 x i8] c"write 2M\00"
@.str.4 = [9 x i8] c"write 2G\00"
@.str.5 = [9 x i8] c"write -2\00"
@O_RDONLY = global i32 0
@.str.6 = [5 x i8] c"read\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @perror(i8* %18)
br label %67
20: ; preds = %2
%21 = load i32, i32* %7
%22 = call i32 @write(i32 %21, i32* inttoptr (i64 67723264 to i32*), i32 5)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp ne i32 %23, 5
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = load i32, i32* %7
%29 = call i32 @write(i32 %28, i32* %7, i32 0)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %27
%35 = load i32, i32* %7
%36 = call i32 @write(i32 %35, i32* %7, i32 1)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %34
%42 = load i32, i32* %7
%43 = call i32 @write(i32 %42, i32* %7, i32 2097152)
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = icmp ne i64 %45, 2097152
br i1 %46, label %47, label %49
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %41
%50 = load i32, i32* %7
%51 = call i32 @write(i32 %50, i32* %7, i32 -2147483648)
store i32 %51, i32* %6
%52 = load i32, i32* %6
%53 = sext i32 %52 to i64
%54 = icmp ne i64 %53, 2147483648
br i1 %54, label %55, label %57
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %55, %49
%58 = load i32, i32* %7
%59 = call i32 @write(i32 %58, i32* %7, i32 -2)
store i32 %59, i32* %6
%60 = load i32, i32* %6
%61 = icmp ne i32 %60, -2
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %64
64: ; preds = %62, %57
%65 = load i32, i32* %7
%66 = call i32 @close(i32 %65)
br label %67
67: ; preds = %64, %15
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 1
%70 = load i8*, i8** %69
%71 = load i32, i32* @O_RDONLY
%72 = call i32 @open(i8* %70, i32 %71)
store i32 %72, i32* %7
%73 = load i32, i32* %7
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %76
75: ; preds = %67
store i32 0, i32* %3
br label %86
76: ; preds = %67
%77 = load i32, i32* %7
%78 = call i32 @read(i32 %77, i8* inttoptr (i64 67723264 to i8*), i32 5)
store i32 %78, i32* %6
%79 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%80 = load i32, i32* %7
%81 = call i32 @close(i32 %80)
%82 = load i32, i32* %6
%83 = icmp ne i32 %82, 0
br i1 %83, label %85, label %84
84: ; preds = %76
store i32 -1, i32* %3
br label %86
85: ; preds = %76
store i32 0, i32* %3
br label %86
86: ; preds = %85, %84, %75
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@.str = [35 x i8] c"write badarea (Should have failed)\00"
@.str.1 = [17 x i8] c"write zero bytes\00"
@.str.2 = [15 x i8] c"write one byte\00"
@.str.3 = [9 x i8] c"write 2M\00"
@.str.4 = [9 x i8] c"write 2G\00"
@.str.5 = [9 x i8] c"write -2\00"
@O_RDONLY = global i32 0
@.str.6 = [5 x i8] c"read\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_WRONLY
%8 = tail call i32 @open(i8* %6, i32 %7)
store i32 %8, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i8*, i8** %5
%12 = tail call i32 @perror(i8* %11)
br label %42
13: ; preds = %2
%14 = tail call i32 @write(i32 %8, i32* inttoptr (i64 67723264 to i32*), i32 5)
%15 = icmp eq i32 %14, 5
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = call i32 @write(i32 %8, i32* %3, i32 0)
%20 = icmp eq i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %18
%24 = call i32 @write(i32 %8, i32* %3, i32 1)
%25 = icmp eq i32 %24, 1
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %23
%29 = call i32 @write(i32 %8, i32* %3, i32 2097152)
%30 = icmp eq i32 %29, 2097152
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = call i32 @write(i32 %8, i32* %3, i32 -2147483648)
%35 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @write(i32 %8, i32* %3, i32 -2)
%37 = icmp eq i32 %36, -2
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %38, %33
%41 = tail call i32 @close(i32 %8)
br label %42
42: ; preds = %40, %10
%43 = load i8*, i8** %5
%44 = load i32, i32* @O_RDONLY
%45 = tail call i32 @open(i8* %43, i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %53, label %47
47: ; preds = %42
%48 = tail call i32 @read(i32 %45, i8* inttoptr (i64 67723264 to i8*), i32 5)
%49 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%50 = tail call i32 @close(i32 %45)
%51 = icmp eq i32 %48, 0
%52 = sext i1 %51 to i32
br label %53
53: ; preds = %47, %42
%54 = phi i32 [ 0, %42 ], [ %52, %47 ]
ret i32 %54
}