#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 O_APPEND;
int O_RDWR;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_SET;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_sys(char *);
int lseek(int, long, int /*<<< orphan*/);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd, n;
  if ((fd = open("input.dat", O_RDWR | O_APPEND, 0)) < 0) {
    err_sys("opening input.dat");
  }
  if (lseek(fd, 10L, SEEK_CUR) == -1) {
    err_sys("lseek error");
  }
  char buf[128];
  if ((n = read(fd, buf, 10)) < 0) {
    err_sys("read error");
  }
  if (write(STDOUT_FILENO, buf, n) != n) {
    err_sys("write error");
  }
  printf("\n=======\n");
  if (lseek(fd, 16L, SEEK_SET) == -1) {
    err_sys("lseek error when testing relace");
  }
  strcpy(buf, "HIJKLMN\n");
  if (write(fd, buf, 8) != 8) {
    err_sys("write error while replacing");
  }
  if (lseek(fd, 0L, SEEK_SET) == -1) {
    err_sys("lseek error while rewinding");
  }
  if ((n = read(fd, buf, 64)) < 0) {
    err_sys("read error while testing final text");
  }
  if (write(STDOUT_FILENO, buf, n) != n) {
    err_sys("write error while testing final text");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
or     (%rax),%esi
lea    0xe56(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d1 <main+0x51>
lea    0xe34(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x14(%rbp),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
mov    $0xa,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11fc <main+0x7c>
lea    0xe1b(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0xa,%edx
call   1090 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1228 <main+0xa8>
lea    0xdfb(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <write@plt>
cmp    -0x18(%rbp),%eax
je     1255 <main+0xd5>
lea    0xdd9(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xdd9(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
mov    $0x10,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    128c <main+0x10c>
lea    0xdb8(%rip),%rdi
call   1040 <err_sys@plt>
lea    -0xa0(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x8,%edx
call   1050 <write@plt>
cmp    $0x8,%eax
je     12c8 <main+0x148>
lea    0xda5(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x14(%rbp),%edi
mov    0x2cf6(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12f2 <main+0x172>
lea    0xd97(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x40,%edx
call   1090 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    131e <main+0x19e>
lea    0xd87(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2ccb(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <write@plt>
cmp    -0x18(%rbp),%eax
je     134b <main+0x1cb>
lea    0xd7e(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    0x2e4c(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe62(%rip),%rdi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11bf <main+0x3f>
lea    0xe46(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    $0xa,%esi
mov    %ebx,%edi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    11e5 <main+0x65>
lea    0xe32(%rip),%rdi
call   1050 <err_sys@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1090 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1206 <main+0x86>
lea    0xe1d(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x2de3(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
mov    %ebp,%edx
call   1060 <write@plt>
cmp    %ebp,%eax
je     122a <main+0xaa>
lea    0xe04(%rip),%rdi
call   1050 <err_sys@plt>
lea    0xeae(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d8b(%rip),%r15
mov    (%r15),%edx
mov    $0x10,%esi
mov    %ebx,%edi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    125d <main+0xdd>
lea    0xddd(%rip),%rdi
call   1050 <err_sys@plt>
lea    0xdf1(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1060 <write@plt>
cmp    $0x8,%eax
je     128f <main+0x10f>
lea    0xdd4(%rip),%rdi
call   1050 <err_sys@plt>
mov    (%r15),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
jne    12ac <main+0x12c>
lea    0xdd3(%rip),%rdi
call   1050 <err_sys@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x40,%edx
call   1090 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12cd <main+0x14d>
lea    0xdce(%rip),%rdi
call   1050 <err_sys@plt>
mov    (%r14),%edi
mov    %rsp,%rsi
mov    %ebp,%edx
call   1060 <write@plt>
cmp    %ebp,%eax
je     12ea <main+0x16a>
lea    0xdd5(%rip),%rdi
call   1050 <err_sys@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"input.dat\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [18 x i8] c"opening input.dat\00"
@SEEK_CUR = global i32 0
@.str.2 = [12 x i8] c"lseek error\00"
@.str.3 = [11 x i8] c"read error\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [12 x i8] c"write error\00"
@.str.5 = [10 x i8] c"\0A=======\0A\00"
@SEEK_SET = global i32 0
@.str.6 = [32 x i8] c"lseek error when testing relace\00"
@.str.7 = [9 x i8] c"HIJKLMN\0A\00"
@.str.8 = [28 x i8] c"write error while replacing\00"
@.str.9 = [28 x i8] c"lseek error while rewinding\00"
@.str.10 = [36 x i8] c"read error while testing final text\00"
@.str.11 = [37 x i8] c"write error while testing final text\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_APPEND
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11, i32 0)
store i32 %12, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i32, i32* %6
%18 = load i32, i32* @SEEK_CUR
%19 = call i32 @lseek(i32 %17, i64 10, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
%24 = load i32, i32* %6
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 10)
store i32 %26, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
%31 = load i32, i32* @STDOUT_FILENO
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%33 = load i32, i32* %7
%34 = call i32 @write(i32 %31, i8* %32, i32 %33)
%35 = load i32, i32* %7
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %30
%40 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%41 = load i32, i32* %6
%42 = load i32, i32* @SEEK_SET
%43 = call i32 @lseek(i32 %41, i64 16, i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @err_sys(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
br label %47
47: ; preds = %45, %39
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%49 = call i32 @strcpy(i8* %48, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%50 = load i32, i32* %6
%51 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%52 = call i32 @write(i32 %50, i8* %51, i32 8)
%53 = icmp ne i32 %52, 8
br i1 %53, label %54, label %56
54: ; preds = %47
%55 = call i32 @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %56
56: ; preds = %54, %47
%57 = load i32, i32* %6
%58 = load i32, i32* @SEEK_SET
%59 = call i32 @lseek(i32 %57, i64 0, i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %56
%62 = call i32 @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
br label %63
63: ; preds = %61, %56
%64 = load i32, i32* %6
%65 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%66 = call i32 @read(i32 %64, i8* %65, i32 64)
store i32 %66, i32* %7
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 @err_sys(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.10, i64 0, i64 0))
br label %70
70: ; preds = %68, %63
%71 = load i32, i32* @STDOUT_FILENO
%72 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%73 = load i32, i32* %7
%74 = call i32 @write(i32 %71, i8* %72, i32 %73)
%75 = load i32, i32* %7
%76 = icmp ne i32 %74, %75
br i1 %76, label %77, label %79
77: ; preds = %70
%78 = call i32 @err_sys(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.11, i64 0, i64 0))
br label %79
79: ; preds = %77, %70
%80 = load i32, i32* %6
%81 = call i32 @close(i32 %80)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"input.dat\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.1 = [18 x i8] c"opening input.dat\00"
@SEEK_CUR = global i32 0
@.str.2 = [12 x i8] c"lseek error\00"
@.str.3 = [11 x i8] c"read error\00"
@STDOUT_FILENO = global i32 0
@.str.4 = [12 x i8] c"write error\00"
@SEEK_SET = global i32 0
@.str.6 = [32 x i8] c"lseek error when testing relace\00"
@.str.7 = [9 x i8] c"HIJKLMN\0A\00"
@.str.8 = [28 x i8] c"write error while replacing\00"
@.str.9 = [28 x i8] c"lseek error while rewinding\00"
@.str.10 = [36 x i8] c"read error while testing final text\00"
@.str.11 = [37 x i8] c"write error while testing final text\00"
@str = [9 x i8] c"\0A=======\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [128 x i8]
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_APPEND
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @err_sys(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = load i32, i32* @SEEK_CUR
%13 = tail call i32 @lseek(i32 %7, i64 10, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%19 = call i32 @read(i32 %7, i8* %18, i32 10)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @write(i32 %24, i8* %18, i32 %19)
%26 = icmp eq i32 %25, %19
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = tail call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%31 = load i32, i32* @SEEK_SET
%32 = tail call i32 @lseek(i32 %7, i64 16, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = tail call i32 @err_sys(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = call i32 @strcpy(i8* %18, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%38 = call i32 @write(i32 %7, i8* %18, i32 8)
%39 = icmp eq i32 %38, 8
br i1 %39, label %42, label %40
40: ; preds = %36
%41 = call i32 @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = load i32, i32* @SEEK_SET
%44 = call i32 @lseek(i32 %7, i64 0, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
%49 = call i32 @read(i32 %7, i8* %18, i32 64)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 @err_sys(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.10, i64 0, i64 0))
br label %53
53: ; preds = %51, %48
%54 = load i32, i32* @STDOUT_FILENO
%55 = call i32 @write(i32 %54, i8* %18, i32 %49)
%56 = icmp eq i32 %55, %49
br i1 %56, label %59, label %57
57: ; preds = %53
%58 = call i32 @err_sys(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.11, i64 0, i64 0))
br label %59
59: ; preds = %57, %53
%60 = call i32 @close(i32 %7)
ret i32 0
}