#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void sym_read(void) {
  char buf[32] = {0};
  int rc;
  if (!fork()) {
    printf("[child]\n");
    rc = read(0, buf, sizeof(buf));
    printf("[child] read(): %d -> '%s'\n", rc, buf);
    exit(0);
  }
  printf("[parent] reading...\n");
  rc = read(0, buf, sizeof(buf));
  printf("[parent] read(): %d -> '%s'\n", rc, buf);
  waitpid(-1, NULL, 0);
}


x86 assembly (clang, -O0):
<sym_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%eax
jne    11c5 <sym_read+0x65>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rsi
xor    %edi,%edi
mov    $0x20,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    -0x20(%rbp),%rdx
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rsi
xor    %edi,%edi
mov    $0x20,%edx
call   1050 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    -0x20(%rbp),%rdx
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sym_read>:
push   %rbx
sub    $0x20,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11c1 <sym_read+0x61>
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1050 <read@plt>
lea    0xe7b(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <waitpid@plt>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1050 <read@plt>
lea    0xe1a(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"[child]\0A\00"
@.str.1 = [28 x i8] c"[child] read(): %d -> '%s'\0A\00"
@.str.2 = [21 x i8] c"[parent] reading...\0A\00"
@.str.3 = [29 x i8] c"[parent] read(): %d -> '%s'\0A\00"
define void @sym_read() {
%1 = alloca [32 x i8]
%2 = alloca i32
%3 = bitcast [32 x i8]* %1 to i8*
%4 = call i32 (...) @fork()
%5 = icmp ne i32 %4, 0
br i1 %5, label %14, label %6
6: ; preds = %0
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%9 = call i32 @read(i32 0, i8* %8, i32 32)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %10, i8* %11)
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%17 = call i32 @read(i32 0, i8* %16, i32 32)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %18, i8* %19)
%21 = call i32 @waitpid(i32 -1, i32* null, i32 0)
ret void
}

LLVM IR (-O2):
@.str.1 = [28 x i8] c"[child] read(): %d -> '%s'\0A\00"
@.str.3 = [29 x i8] c"[parent] read(): %d -> '%s'\0A\00"
@str = [8 x i8] c"[child]\00"
@str.4 = [20 x i8] c"[parent] reading...\00"
define void @sym_read() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%7 = call i32 @read(i32 0, i8* %2, i32 32)
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %7, i8* %2)
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
%12 = call i32 @read(i32 0, i8* %2, i32 32)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %12, i8* %2)
%14 = call i32 @waitpid(i32 -1, i32* null, i32 0)
ret void
}