#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 */
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
main() {
  int p[2];
  pipe(p);
  perror("pipe");
  if (fork()) {
    char ch;
    printf("enter a char :\n");
    scanf("%c", &ch);
    write(p[1], &ch, sizeof(char));
    wait(0);
  } else {
    char ch;
    read(p[0], &ch, sizeof(char));
    printf("in child, char read is : %c\n", ch);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <pipe@plt>
lea    0xe61(%rip),%rdi
call   1080 <perror@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
je     11f0 <main+0x70>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe4b(%rip),%rdi
lea    -0xd(%rbp),%rsi
call   1070 <scanf@plt>
mov    -0x8(%rbp),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1090 <wait@plt>
jmp    1213 <main+0x93>
mov    -0xc(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
movsbl -0xe(%rbp),%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
lea    0xe5a(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     11f1 <main+0x61>
lea    0xe67(%rip),%rdi
call   1030 <puts@plt>
lea    0xe3b(%rip),%rdi
lea    0x7(%rsp),%rbx
mov    %rbx,%rsi
call   1080 <scanf@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
jmp    1217 <main+0x87>
mov    0x8(%rsp),%edi
lea    0x6(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl 0x6(%rsp),%esi
lea    0xdf8(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [16 x i8] c"enter a char :\0A\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [29 x i8] c"in child, char read is : %c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i8
%4 = alloca 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 i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = call i64 (...) @fork()
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @write(i32 %14, i8* %3, i32 1)
%16 = call i32 @wait(i32 0)
br label %24
17: ; preds = %0
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%19 = load i32, i32* %18
%20 = call i32 @read(i32 %19, i8* %4, i32 1)
%21 = load i8, i8* %4
%22 = sext i8 %21 to i32
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %17, %10
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [29 x i8] c"in child, char read is : %c\0A\00"
@str = [15 x i8] c"enter a char :\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i8
%3 = alloca i8
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
br i1 %9, label %17, label %10
10: ; preds = %0
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @write(i32 %14, i8* %2, i32 1)
%16 = call i32 @wait(i32 0)
br label %23
17: ; preds = %0
%18 = load i32, i32* %5
%19 = call i32 @read(i32 %18, i8* %3, i32 1)
%20 = load i8, i8* %3
%21 = sext i8 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %17, %10
ret i32 0
}