#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*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ resign();
int /*<<< orphan*/ sync();
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  sync();
  unlink("/comm");
  sync();
  int fd = open("/comm", 0x0002 | 0x0200);
  int pid = fork();
  if (pid != 0) {
    printf("parent: writes two lines\n");
    write(fd, "Hello\n", 6);
    write(fd, "Test\n", 5);
    int i;
    for (i = 0; i < 10; i++)
      resign();
    printf("parent: writes two lines\n");
    write(fd, "Two more\n", 9);
    write(fd, "lines\n", 6);
    for (i = 0; i < 10; i++)
      resign();
    printf("parent: closes fd and leaves\n");
    close(fd);
    sync();
    exit(0);
  } else {
    char buf[80];
    int bytes;
    lseek(fd, 0, 0);
    int i;
    for (i = 0; i < 200; i++) {
      bytes = read(fd, buf, bytes);
      if (bytes > 0) {
        printf("BUF: ");
        write(1, buf, bytes);
        printf("\n");
      } else
        resign();
    }
    close(fd);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1090 <sync@plt>
lea    0xe33(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x0,%al
call   1090 <sync@plt>
lea    0xe20(%rip),%rdi
mov    $0x202,%esi
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     12d7 <main+0x127>
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xe09(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    0xdfc(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0xa,-0x10(%rbp)
jge    125b <main+0xab>
mov    $0x0,%al
call   10c0 <resign@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    123c <main+0x8c>
lea    0xda4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xdbc(%rip),%rsi
mov    $0x9,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    0xd94(%rip),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0xa,-0x10(%rbp)
jge    12b5 <main+0x105>
mov    $0x0,%al
call   10c0 <resign@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1296 <main+0xe6>
lea    0xd7b(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    $0x0,%al
call   1090 <sync@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <lseek@plt>
movl   $0x0,-0x68(%rbp)
cmpl   $0xc8,-0x68(%rbp)
jge    135b <main+0x1ab>
mov    -0x8(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    -0x64(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jle    1341 <main+0x191>
lea    0xd3b(%rip),%rdi
call   1050 <printf@plt>
lea    -0x60(%rbp),%rsi
mov    -0x64(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
lea    0xcfe(%rip),%rdi
call   1050 <printf@plt>
jmp    1348 <main+0x198>
mov    $0x0,%al
call   10c0 <resign@plt>
jmp    134d <main+0x19d>
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    12ea <main+0x13a>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <sync@plt>
lea    0xe4e(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
call   1080 <sync@plt>
mov    %rbx,%rdi
mov    $0x202,%esi
call   1090 <open@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     12ef <main+0x14f>
lea    0xe3e(%rip),%r14
mov    %r14,%rdi
call   1040 <puts@plt>
lea    0xe11(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1050 <write@plt>
lea    0xe05(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1050 <write@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
lea    0xdaa(%rip),%rsi
mov    %ebx,%edi
mov    $0x9,%edx
call   1050 <write@plt>
lea    0xda1(%rip),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1050 <write@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
xor    %eax,%eax
call   10b0 <resign@plt>
lea    0xd68(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
call   1080 <sync@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <lseek@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"/comm\00"
@.str.1 = [26 x i8] c"parent: writes two lines\0A\00"
@.str.2 = [7 x i8] c"Hello\0A\00"
@.str.3 = [6 x i8] c"Test\0A\00"
@.str.4 = [10 x i8] c"Two more\0A\00"
@.str.5 = [7 x i8] c"lines\0A\00"
@.str.6 = [30 x i8] c"parent: closes fd and leaves\0A\00"
@.str.7 = [6 x i8] c"BUF: \00"
@.str.8 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [80 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 (...) @sync()
%9 = call i32 @unlink(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%10 = call i32 (...) @sync()
%11 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 514)
store i32 %11, i32* %2
%12 = call i32 (...) @fork()
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %49
15: ; preds = %0
%16 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* %2
%18 = call i32 @write(i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%19 = load i32, i32* %2
%20 = call i32 @write(i32 %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
store i32 0, i32* %4
br label %21
21: ; preds = %26, %15
%22 = load i32, i32* %4
%23 = icmp slt i32 %22, 10
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = call i32 (...) @resign()
br label %26
26: ; preds = %24
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %21
29: ; preds = %21
%30 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* %2
%32 = call i32 @write(i32 %31, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 9)
%33 = load i32, i32* %2
%34 = call i32 @write(i32 %33, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 6)
store i32 0, i32* %4
br label %35
35: ; preds = %40, %29
%36 = load i32, i32* %4
%37 = icmp slt i32 %36, 10
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = call i32 (...) @resign()
br label %40
40: ; preds = %38
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %35
43: ; preds = %35
%44 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%45 = load i32, i32* %2
%46 = call i32 @close(i32 %45)
%47 = call i32 (...) @sync()
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %0
%50 = load i32, i32* %2
%51 = call i32 @lseek(i32 %50, i32 0, i32 0)
store i32 0, i32* %7
br label %52
52: ; preds = %71, %49
%53 = load i32, i32* %7
%54 = icmp slt i32 %53, 200
br i1 %54, label %55, label %74
55: ; preds = %52
%56 = load i32, i32* %2
%57 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%58 = load i32, i32* %6
%59 = call i32 @read(i32 %56, i8* %57, i32 %58)
store i32 %59, i32* %6
%60 = load i32, i32* %6
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %55
%63 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%64 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%65 = load i32, i32* %6
%66 = call i32 @write(i32 1, i8* %64, i32 %65)
%67 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
br label %70
68: ; preds = %55
%69 = call i32 (...) @resign()
br label %70
70: ; preds = %68, %62
br label %71
71: ; preds = %70
%72 = load i32, i32* %7
%73 = add nsw i32 %72, 1
store i32 %73, i32* %7
br label %52
74: ; preds = %52
%75 = load i32, i32* %2
%76 = call i32 @close(i32 %75)
%77 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"/comm\00"
@.str.2 = [7 x i8] c"Hello\0A\00"
@.str.3 = [6 x i8] c"Test\0A\00"
@.str.4 = [10 x i8] c"Two more\0A\00"
@.str.5 = [7 x i8] c"lines\0A\00"
@str.9 = [25 x i8] c"parent: writes two lines\00"
@str.10 = [29 x i8] c"parent: closes fd and leaves\00"
define i32 @main() {
%1 = tail call i32 (...) @sync()
%2 = tail call i32 @unlink(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 (...) @sync()
%4 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 514)
%5 = tail call i32 (...) @fork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %38, label %7
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
%9 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%10 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%11 = tail call i32 (...) @resign()
%12 = tail call i32 (...) @resign()
%13 = tail call i32 (...) @resign()
%14 = tail call i32 (...) @resign()
%15 = tail call i32 (...) @resign()
%16 = tail call i32 (...) @resign()
%17 = tail call i32 (...) @resign()
%18 = tail call i32 (...) @resign()
%19 = tail call i32 (...) @resign()
%20 = tail call i32 (...) @resign()
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
%22 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 9)
%23 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32 6)
%24 = tail call i32 (...) @resign()
%25 = tail call i32 (...) @resign()
%26 = tail call i32 (...) @resign()
%27 = tail call i32 (...) @resign()
%28 = tail call i32 (...) @resign()
%29 = tail call i32 (...) @resign()
%30 = tail call i32 (...) @resign()
%31 = tail call i32 (...) @resign()
%32 = tail call i32 (...) @resign()
%33 = tail call i32 (...) @resign()
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.10, i64 0, i64 0))
%35 = tail call i32 @close(i32 %4)
%36 = tail call i32 (...) @sync()
%37 = tail call i32 @exit(i32 0)
unreachable
38: ; preds = %0
%39 = tail call i32 @lseek(i32 %4, i32 0, i32 0)
unreachable
}