#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*/ readbuffer;
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main(int argc, char *argv[]) {
  int fd[2];
  int nbytes;
  pid_t childpid;
  char string[] = "Hello, world!\n";
  char readbuffer[80];
  pipe(fd);
  if ((childpid = fork()) == -1) {
    perror("fork");
    exit(1);
  }
  if (childpid == 0) {
    close(fd[0]);
    write(fd[1], string, (strlen(string) + 1));
    exit(0);
  } else {
    close(fd[1]);
    nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
    printf("Received string: %s", readbuffer);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe50(%rip),%rax
mov    %rax,-0x2f(%rbp)
mov    0xe4e(%rip),%eax
mov    %eax,-0x27(%rbp)
mov    0xe48(%rip),%ax
mov    %ax,-0x23(%rbp)
mov    0xe40(%rip),%al
mov    %al,-0x21(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1203 <main+0x73>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
jne    1252 <main+0xc2>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x8c(%rbp)
lea    -0x2f(%rbp),%rax
mov    %rax,-0x88(%rbp)
lea    -0x2f(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x8c(%rbp),%edi
mov    -0x88(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x50,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x80(%rbp),%rsi
lea    0xd9b(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x78,%rsp
movabs $0xa21646c726f77,%rax
mov    %rax,0x17(%rsp)
movabs $0x77202c6f6c6c6548,%rax
2c 20 77
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1207 <main+0x77>
cmp    $0xffffffff,%eax
je     1236 <main+0xa6>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x50,%edx
call   1080 <read@plt>
lea    0xe1f(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0xc(%rsp),%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xdd2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.string = [15 x i8] c"Hello, world!\0A\00"
@.str = [5 x i8] c"fork\00"
@.str.1 = [20 x i8] c"Received string: %s\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 i32
%9 = alloca [15 x i8]
%10 = alloca [80 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [15 x i8]* %9 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = call i32 (...) @fork()
store i32 %14, i32* %8
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %34
22: ; preds = %19
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%27 = load i32, i32* %26
%28 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 0
%29 = getelementptr inbounds [15 x i8], [15 x i8]* %9, i64 0, i64 0
%30 = call i64 @strlen(i8* %29)
%31 = add nsw i64 %30, 1
%32 = call i32 @write(i32 %27, i8* %28, i64 %31)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %19
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%39 = load i32, i32* %38
%40 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 80)
store i32 %41, i32* %7
%42 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%43 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %42)
br label %44
44: ; preds = %34
ret i32 0
}

LLVM IR (-O2):
@__const.main.string = [15 x i8] c"Hello, world!\0A\00"
@.str = [5 x i8] c"fork\00"
@.str.1 = [20 x i8] c"Received string: %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [15 x i8]
%5 = alloca [80 x i8]
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = call i32 (...) @fork()
switch i32 %11, label %24 [
i32 -1, label %12
i32 0, label %15
]
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* %9
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i64 @strlen(i8* %7)
%21 = add nsw i64 %20, 1
%22 = call i32 @write(i32 %19, i8* %7, i64 %21)
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %2
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %9
%29 = call i32 @read(i32 %28, i8* %8, i32 80)
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %8)
ret i32 0
}