#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*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int O_APPEND;
int O_RDWR;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd, n;
  int len;
  char buf[64];
  if ((fd = open("./rwex.c", O_RDWR | O_APPEND)) == -1) {
    fprintf(stderr, "Unable to open '%s': %s\n", "./rwex.c", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((n = read(fd, buf, 64)) > 0) {
    if (write(STDOUT_FILENO, buf, n) != n) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
  }
  printf("\n\nOk, we read the first %d bytes. Now let's write something.\n",
         64);
  len = sizeof("\n/* Just another comment. */\n") - 1;
  if (write(fd, "\n/* Just another comment. */\n", len) != len) {
    fprintf(stderr, "Unable to write: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e02(%rip),%rax
or     (%rax),%esi
lea    0xe51(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1203 <main+0x83>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rcx
lea    0xe22(%rip),%rsi
lea    0xe12(%rip),%rdx
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    127f <main+0xff>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
cmp    -0x18(%rbp),%eax
je     127a <main+0xfa>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x68(%rbp),%edi
mov    %rax,%rdx
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    127f <main+0xff>
lea    0xdb1(%rip),%rdi
mov    $0x40,%esi
call   1040 <printf@plt>
movl   $0x1d,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x1c(%rbp),%edx
lea    0xdd1(%rip),%rsi
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     12ee <main+0x16e>
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
lea    0xd49(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
mov    0x2e53(%rip),%rax
mov    0x2e14(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe61(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1247 <main+0xc7>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x40,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11db <main+0x5b>
mov    %eax,%ebp
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jne    121b <main+0x9b>
lea    0xe55(%rip),%rdi
mov    $0x40,%esi
call   1040 <printf@plt>
lea    0xe82(%rip),%rsi
mov    %ebx,%edi
mov    $0x1d,%edx
call   1030 <write@plt>
cmp    $0x1d,%eax
jne    121b <main+0x9b>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dae(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xde9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1278 <main+0xf8>
mov    0x2d82(%rip),%rax
mov    (%rax),%ebx
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xda4(%rip),%rsi
lea    0xd94(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"./rwex.c\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Unable to open '%s': %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [21 x i8] c"Unable to write: %s\0A\00"
@.str.3 = [62 x i8] c"\0A\0AOk, we read the first %d bytes. Now let's write something.\0A\00"
@.str.4 = [30 x i8] c"\0A/* Just another comment. */\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [64 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_APPEND
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i32, i32* %6
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%25 = call i32 @read(i32 %23, i8* %24, i32 64)
store i32 %25, i32* %7
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %42
27: ; preds = %22
%28 = load i32, i32* @STDOUT_FILENO
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%30 = load i32, i32* %7
%31 = call i32 @write(i32 %28, i8* %29, i32 %30)
%32 = load i32, i32* %7
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %41
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %27
br label %42
42: ; preds = %41, %22
%43 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i32 64)
store i32 29, i32* %8
%44 = load i32, i32* %6
%45 = load i32, i32* %8
%46 = call i32 @write(i32 %44, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %8
%48 = icmp ne i32 %46, %47
br i1 %48, label %49, label %56
49: ; preds = %42
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %42
%57 = load i32, i32* %6
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* @EXIT_SUCCESS
ret i32 %59
}

LLVM IR (-O2):
@.str = [9 x i8] c"./rwex.c\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str.1 = [25 x i8] c"Unable to open '%s': %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [21 x i8] c"Unable to write: %s\0A\00"
@.str.3 = [62 x i8] c"\0A\0AOk, we read the first %d bytes. Now let's write something.\0A\00"
@.str.4 = [30 x i8] c"\0A/* Just another comment. */\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [64 x i8]
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_APPEND
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = call i32 @read(i32 %8, i8* %4, i32 64)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %17
%21 = load i32, i32* @STDOUT_FILENO
%22 = call i32 @write(i32 %21, i8* %4, i32 %18)
%23 = icmp eq i32 %22, %18
br i1 %23, label %31, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %20, %17
%32 = tail call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.3, i64 0, i64 0), i32 64)
%33 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 29)
%34 = icmp eq i32 %33, 29
br i1 %34, label %42, label %35
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = tail call i8* @strerror(i32 %37)
%39 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = load i32, i32* @EXIT_FAILURE
%41 = tail call i32 @exit(i32 %40)
unreachable
42: ; preds = %31
%43 = tail call i32 @close(i32 %8)
%44 = load i32, i32* @EXIT_SUCCESS
ret i32 %44
}