#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*/ SEEK_SET;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char const *);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  char ch;
  const char *me;
  int fd = open("/proc/self/maps", O_RDONLY);
  if (fd == -1) {
    perror("open failed:");
    return 1;
  }
  if (fork() > 0) {
    me = "parent";
    while (1) {
      int ret = read(fd, &ch, 1);
      if (ret == 0) {
        lseek(fd, 0, SEEK_SET);
      } else if (ret == -1) {
        exit(0);
      }
    }
  } else {
    me = "child";
    while (1) {
      int ret = read(fd, &ch, 1);
      if (ret == 0) {
        lseek(fd, 0, SEEK_SET);
      } else if (ret == -1) {
        exit(0);
      }
      sleep(1);
    }
  }
  printf("%s done\n", me);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11bf <main+0x4f>
lea    0xe61(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jle    122c <main+0xbc>
lea    0xe46(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1211 <main+0xa1>
mov    -0x24(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
jmp    1227 <main+0xb7>
cmpl   $0xffffffff,-0x28(%rbp)
jne    1222 <main+0xb2>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1227 <main+0xb7>
jmp    11db <main+0x6b>
lea    0xdf1(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    126d <main+0xfd>
mov    -0x24(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
jmp    1283 <main+0x113>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    127e <main+0x10e>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1283 <main+0x113>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1237 <main+0xc7>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    11ae <main+0x3e>
lea    0xe76(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %eax,%ebx
xor    %eax,%eax
call   1090 <fork@plt>
lea    0xf(%rsp),%r14
mov    0x2e15(%rip),%r15
test   %rax,%rax
jg     11dc <main+0x6c>
jmp    1216 <main+0xa6>
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11d0 <main+0x60>
cmp    $0xffffffff,%eax
jne    11dc <main+0x6c>
jmp    122e <main+0xbe>
cs nopw 0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
test   %eax,%eax
je     1200 <main+0x90>
cmp    $0xffffffff,%eax
jne    120c <main+0x9c>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"/proc/self/maps\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"open failed:\00"
@.str.2 = [7 x i8] c"parent\00"
@SEEK_SET = global i32 0
@.str.3 = [6 x i8] c"child\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
ret i32 1
17: ; preds = %2
%18 = call i64 (...) @fork()
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %37
20: ; preds = %17
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8** %7
br label %21
21: ; preds = %20, %36
%22 = load i32, i32* %8
%23 = call i32 @read(i32 %22, i8* %6, i32 1)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %21
%27 = load i32, i32* %8
%28 = load i32, i32* @SEEK_SET
%29 = call i32 @lseek(i32 %27, i32 0, i32 %28)
br label %36
30: ; preds = %21
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %30
br label %36
36: ; preds = %35, %26
br label %21
37: ; preds = %17
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8** %7
br label %38
38: ; preds = %37, %53
%39 = load i32, i32* %8
%40 = call i32 @read(i32 %39, i8* %6, i32 1)
store i32 %40, i32* %10
%41 = load i32, i32* %10
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = load i32, i32* %8
%45 = load i32, i32* @SEEK_SET
%46 = call i32 @lseek(i32 %44, i32 0, i32 %45)
br label %53
47: ; preds = %38
%48 = load i32, i32* %10
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @exit(i32 0)
unreachable
52: ; preds = %47
br label %53
53: ; preds = %52, %43
%54 = call i32 @sleep(i32 1)
br label %38
}

LLVM IR (-O2):
@.str = [16 x i8] c"/proc/self/maps\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"open failed:\00"
@SEEK_SET = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
ret i32 1
9: ; preds = %2
%10 = tail call i64 (...) @fork()
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %9, %14
%13 = call i32 @read(i32 %5, i8* %3, i32 1)
switch i32 %13, label %14 [
i32 0, label %15
i32 -1, label %18
]
14: ; preds = %12, %15
br label %12
15: ; preds = %12
%16 = load i32, i32* @SEEK_SET
%17 = tail call i32 @lseek(i32 %5, i32 0, i32 %16)
br label %14
18: ; preds = %12
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %9, %27
%21 = call i32 @read(i32 %5, i8* %3, i32 1)
switch i32 %21, label %27 [
i32 0, label %22
i32 -1, label %25
]
22: ; preds = %20
%23 = load i32, i32* @SEEK_SET
%24 = tail call i32 @lseek(i32 %5, i32 0, i32 %23)
br label %27
25: ; preds = %20
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %20, %22
%28 = tail call i32 @sleep(i32 1)
br label %20
}