#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);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
main() {
  int fd[2];
  int n;
  char buf[30];
  pipe(fd);
  if (fork() == 0) {
    printf("\nread from pipe:");
    close(fd[1]);
    while (n = read(fd[0], buf, 1) > 0) {
      write(1, buf, n);
    }
    close(fd[0]);
  } else {
    printf("read from stdin");
    close(fd[0]);
    while (n = read(0, buf, 1) > 0) {
      write(fd[1], buf, n);
    }
    printf("\ncontent of buf is:\n", buf);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    11f2 <main+0x92>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
setg   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x10(%rbp)
test   $0x1,%al
jne    11cf <main+0x6f>
jmp    11e5 <main+0x85>
lea    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    119f <main+0x3f>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    125d <main+0xfd>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
lea    -0x30(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
setg   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x10(%rbp)
test   $0x1,%al
jne    1237 <main+0xd7>
jmp    124b <main+0xeb>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
jmp    1208 <main+0xa8>
lea    -0x30(%rbp),%rsi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11f3 <main+0x83>
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jle    11e5 <main+0x75>
lea    0x10(%rsp),%rbx
nopl   (%rax)
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jg     11c0 <main+0x50>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
jmp    1261 <main+0xf1>
lea    0xe06(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jle    1258 <main+0xe8>
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jg     1230 <main+0xc0>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"\0Aread from pipe:\00"
@.str.1 = [16 x i8] c"read from stdin\00"
@.str.2 = [21 x i8] c"\0Acontent of buf is:\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [30 x i8]
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %29
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%12 = load i32, i32* %11
%13 = call i32 @close(i32 %12)
br label %14
14: ; preds = %21, %9
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%16 = load i32, i32* %15
%17 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%18 = call i64 @read(i32 %16, i8* %17, i32 1)
%19 = icmp sgt i64 %18, 0
%20 = zext i1 %19 to i32
store i32 %20, i32* %3
br i1 %19, label %21, label %25
21: ; preds = %14
%22 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%23 = load i32, i32* %3
%24 = call i32 @write(i32 1, i8* %22, i32 %23)
br label %14
25: ; preds = %14
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
br label %48
29: ; preds = %0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %39, %29
%35 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%36 = call i64 @read(i32 0, i8* %35, i32 1)
%37 = icmp sgt i64 %36, 0
%38 = zext i1 %37 to i32
store i32 %38, i32* %3
br i1 %37, label %39, label %45
39: ; preds = %34
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%43 = load i32, i32* %3
%44 = call i32 @write(i32 %41, i8* %42, i32 %43)
br label %34
45: ; preds = %34
%46 = getelementptr inbounds [30 x i8], [30 x i8]* %4, i64 0, i64 0
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %45, %25
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@.str = [17 x i8] c"\0Aread from pipe:\00"
@.str.1 = [16 x i8] c"read from stdin\00"
@str = [20 x i8] c"\0Acontent of buf is:\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [30 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [30 x i8], [30 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %25
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%12 = load i32, i32* %11
%13 = call i32 @close(i32 %12)
%14 = load i32, i32* %5
%15 = call i64 @read(i32 %14, i8* %4, i32 1)
%16 = icmp sgt i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %9, %17
%18 = call i32 @write(i32 1, i8* %4, i32 1)
%19 = load i32, i32* %5
%20 = call i64 @read(i32 %19, i8* %4, i32 1)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %17, label %22
22: ; preds = %17, %9
%23 = load i32, i32* %5
%24 = call i32 @close(i32 %23)
br label %40
25: ; preds = %0
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
%29 = call i64 @read(i32 0, i8* %4, i32 1)
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %25
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %33
33: ; preds = %31, %33
%34 = load i32, i32* %32
%35 = call i32 @write(i32 %34, i8* %4, i32 1)
%36 = call i64 @read(i32 0, i8* %4, i32 1)
%37 = icmp sgt i64 %36, 0
br i1 %37, label %33, label %38
38: ; preds = %33, %25
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %40
40: ; preds = %38, %22
ret i32 0
}