#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 */
typedef int /*<<< orphan*/ read_buf;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd[2] = {};
  int ret = 0;
  ret = pipe(fd);
  if (ret < 0) {
    perror("pipe");
  }
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork");
  }
  if (pid == 0) {
    char buf[100] = "hello world";
    sleep(3);
    write(fd[1], buf, strlen(buf));
    exit(0);
  } else {
    fcntl(fd[0], F_SETFL, O_NONBLOCK);
    while (1) {
      char read_buf[100] = "";
      sleep(1);
      read(fd[0], read_buf, sizeof(read_buf));
      printf("read_buf is %s\n", read_buf);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
movl   $0x0,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
call   1080 <pipe@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1212 <main+0x52>
lea    0xdf3(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1234 <main+0x74>
lea    0xdd6(%rip),%rdi
call   10b0 <perror@plt>
cmpq   $0x0,-0x28(%rbp)
jne    129f <main+0xdf>
lea    -0x90(%rbp),%rdi
lea    0xdd3(%rip),%rsi
mov    $0x64,%edx
call   10a0 <memcpy@plt>
mov    $0x3,%edi
call   10d0 <sleep@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10c(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x108(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x10c(%rbp),%edi
mov    -0x108(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
lea    -0x100(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1070 <memset@plt>
mov    $0x1,%edi
call   10d0 <sleep@plt>
mov    -0x18(%rbp),%edi
lea    -0x100(%rbp),%rsi
mov    $0x64,%edx
call   1090 <read@plt>
lea    -0x100(%rbp),%rsi
lea    0xd12(%rip),%rdi
call   1060 <printf@plt>
jmp    12b9 <main+0xf9>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x78,%rsp
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
test   %eax,%eax
js     1251 <main+0xb1>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1262 <main+0xc2>
je     1273 <main+0xd3>
mov    0x8(%rsp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    0x2dfc(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
lea    0x10(%rsp),%rbx
lea    0xe0b(%rip),%r14
nop
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1080 <read@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
jmp    1200 <main+0x60>
lea    0xda8(%rip),%rdi
call   1090 <perror@plt>
jmp    11c2 <main+0x22>
lea    0xd9c(%rip),%rdi
call   1090 <perror@plt>
jmp    11d8 <main+0x38>
movaps 0xdf6(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xdda(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xdbe(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xda2(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xd86(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xd6a(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    0xc(%rsp),%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@__const.main.buf = [100 x i8] c"hello world\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [16 x i8] c"read_buf is %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i64
%9 = alloca [100 x i8]
%10 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [2 x i32]* %6 to i8*
store i32 0, i32* %7
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = call i64 (...) @fork()
store i64 %19, i64* %8
%20 = load i64, i64* %8
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i64, i64* %8
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = bitcast [100 x i8]* %9 to i8*
%29 = call i32 @sleep(i32 3)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @write(i32 %31, i8* %32, i32 %34)
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %24
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%39 = load i32, i32* %38
%40 = load i32, i32* @F_SETFL
%41 = load i32, i32* @O_NONBLOCK
%42 = call i32 @fcntl(i32 %39, i32 %40, i32 %41)
br label %43
43: ; preds = %37, %43
%44 = bitcast [100 x i8]* %10 to i8*
%45 = call i32 @sleep(i32 1)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%47 = load i32, i32* %46
%48 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 100)
%50 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%51 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %50)
br label %43
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@__const.main.buf = [100 x i8] c"hello world\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [16 x i8] c"read_buf is %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i64
%4 = alloca [100 x i8]
%5 = alloca [100 x i8]
%6 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%7 = bitcast i64* %3 to i32*
%8 = call i32 @pipe(i32* %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
17: ; preds = %12
%18 = icmp eq i64 %13, 0
br i1 %18, label %19, label %28
19: ; preds = %17
%20 = bitcast i64* %3 to [2 x i32]*
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%22 = call i32 @sleep(i32 3)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %20, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @strlen(i8* %21)
%26 = call i32 @write(i32 %24, i8* %21, i32 %25)
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %15, %17
%29 = load i32, i32* %7
%30 = load i32, i32* @F_SETFL
%31 = load i32, i32* @O_NONBLOCK
%32 = call i32 @fcntl(i32 %29, i32 %30, i32 %31)
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
br label %34
34: ; preds = %34, %28
%35 = call i32 @sleep(i32 1)
%36 = load i32, i32* %7
%37 = call i32 @read(i32 %36, i8* %33, i32 100)
%38 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %33)
br label %34
}