#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 fork();
int getpid();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
long syscall(int, int, int, char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int status;
  int parent_pid = getpid();
  int child_pid = fork();
  if (!child_pid) {
    char *buffer;
    buffer = (char *)malloc(100 * sizeof(char));
    buffer = "Dear my parent process!";
    printf("Child sending a messege\n");
    syscall(292, parent_pid, strlen(buffer), buffer);
    printf("Child: 1st message sent.\n");
    buffer = "Today is my birthday!";
    printf("Child sending a message \n");
    syscall(292, parent_pid, strlen(buffer), buffer);
    printf("Child: 1st message sent.\n");
    return 0;
  } else {
    waitpid(child_pid, &status, 0);
    char *rbuffer;
    rbuffer = (char *)malloc(100 * sizeof(char));
    printf("Parent receiving 1st message now\n");
    long returnval = syscall(293, child_pid, 100 * sizeof(char), rbuffer);
    printf("Parent: received message len=%d\n", returnval);
    if (returnval > 0) {
      printf("Parent: 1st message received - %s\n", rbuffer);
    } else {
      printf("There is an error when receiving message\n");
    }
    printf("Parent receiving 2nd message now\n");
    returnval = syscall(293, child_pid, 100 * sizeof(char), rbuffer);
    printf("Parent: received message len=%d\n", returnval);
    if (returnval > 0) {
      printf("Parent: 2nd message received - %s\n", rbuffer);
    } else {
      printf("There is an error when receiving message\n");
    }
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1250 <main+0xe0>
mov    $0x64,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
lea    0xe47(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x40(%rbp),%esi
mov    %eax,%edx
mov    -0x28(%rbp),%rcx
mov    $0x124,%edi
call   1060 <syscall@plt>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe49(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%esi
mov    %eax,%edx
mov    -0x28(%rbp),%rcx
mov    $0x124,%edi
call   1060 <syscall@plt>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1347 <main+0x1d7>
mov    -0x1c(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    $0x64,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    $0x125,%edi
mov    $0x64,%edx
call   1060 <syscall@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
jle    12c8 <main+0x158>
mov    -0x30(%rbp),%rsi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12d6 <main+0x166>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    $0x125,%edi
mov    $0x64,%edx
call   1060 <syscall@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x38(%rbp)
jle    1332 <main+0x1c2>
mov    -0x30(%rbp),%rsi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1340 <main+0x1d0>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     120c <main+0x8c>
mov    %eax,%ebp
lea    0xc(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    $0x64,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
lea    0xf1f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x125,%edi
mov    %ebp,%esi
mov    $0x64,%edx
mov    %r14,%rcx
call   1070 <syscall@plt>
mov    %rax,%rbx
lea    0xe4b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
test   %rbx,%rbx
jle    1282 <main+0x102>
lea    0xe52(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    128e <main+0x10e>
mov    $0x64,%edi
call   1080 <malloc@plt>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
lea    0xdd7(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    $0x124,%edi
mov    %ebx,%esi
mov    %eax,%edx
mov    %rbp,%rcx
call   1070 <syscall@plt>
lea    0xe7d(%rip),%r14
mov    %r14,%rdi
call   1030 <puts@plt>
lea    0xe55(%rip),%rdi
call   1030 <puts@plt>
lea    0xdb4(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    $0x124,%edi
mov    %ebx,%esi
mov    %eax,%edx
mov    %rbp,%rcx
call   1070 <syscall@plt>
mov    %r14,%rdi
jmp    12e1 <main+0x161>
lea    0xe98(%rip),%rdi
call   1030 <puts@plt>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x125,%edi
mov    %ebp,%esi
mov    $0x64,%edx
mov    %r14,%rcx
call   1070 <syscall@plt>
mov    %rax,%rbx
lea    0xd76(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
test   %rbx,%rbx
jle    12da <main+0x15a>
lea    0xda4(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12e6 <main+0x166>
lea    0xe40(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Dear my parent process!\00"
@.str.1 = [25 x i8] c"Child sending a messege\0A\00"
@.str.2 = [26 x i8] c"Child: 1st message sent.\0A\00"
@.str.3 = [22 x i8] c"Today is my birthday!\00"
@.str.4 = [26 x i8] c"Child sending a message \0A\00"
@.str.5 = [34 x i8] c"Parent receiving 1st message now\0A\00"
@.str.6 = [33 x i8] c"Parent: received message len=%d\0A\00"
@.str.7 = [35 x i8] c"Parent: 1st message received - %s\0A\00"
@.str.8 = [42 x i8] c"There is an error when receiving message\0A\00"
@.str.9 = [34 x i8] c"Parent receiving 2nd message now\0A\00"
@.str.10 = [35 x i8] c"Parent: 2nd message received - %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (...) @getpid()
store i32 %12, i32* %7
%13 = call i32 (...) @fork()
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %33, label %16
16: ; preds = %2
%17 = call i64 @malloc(i32 100)
%18 = inttoptr i64 %17 to i8*
store i8* %18, i8** %9
store i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8** %9
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* %7
%21 = load i8*, i8** %9
%22 = call i32 @strlen(i8* %21)
%23 = load i8*, i8** %9
%24 = call i64 @syscall(i32 292, i32 %20, i32 %22, i8* %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8** %9
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* %7
%28 = load i8*, i8** %9
%29 = call i32 @strlen(i8* %28)
%30 = load i8*, i8** %9
%31 = call i64 @syscall(i32 292, i32 %27, i32 %29, i8* %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %66
33: ; preds = %2
%34 = load i32, i32* %8
%35 = call i32 @waitpid(i32 %34, i32* %6, i32 0)
%36 = call i64 @malloc(i32 100)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %10
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0))
%39 = load i32, i32* %8
%40 = load i8*, i8** %10
%41 = call i64 @syscall(i32 293, i32 %39, i32 100, i8* %40)
store i64 %41, i64* %11
%42 = load i64, i64* %11
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i64 %42)
%44 = load i64, i64* %11
%45 = icmp sgt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %33
%47 = load i8*, i8** %10
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i8* %47)
br label %51
49: ; preds = %33
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.8, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0))
%53 = load i32, i32* %8
%54 = load i8*, i8** %10
%55 = call i64 @syscall(i32 293, i32 %53, i32 100, i8* %54)
store i64 %55, i64* %11
%56 = load i64, i64* %11
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i64 %56)
%58 = load i64, i64* %11
%59 = icmp sgt i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %51
%61 = load i8*, i8** %10
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i8* %61)
br label %65
63: ; preds = %51
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.8, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
store i32 0, i32* %3
br label %66
66: ; preds = %65, %16
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [24 x i8] c"Dear my parent process!\00"
@.str.3 = [22 x i8] c"Today is my birthday!\00"
@.str.6 = [33 x i8] c"Parent: received message len=%d\0A\00"
@.str.7 = [35 x i8] c"Parent: 1st message received - %s\0A\00"
@.str.10 = [35 x i8] c"Parent: 2nd message received - %s\0A\00"
@str = [24 x i8] c"Child sending a messege\00"
@str.12 = [25 x i8] c"Child sending a message \00"
@str.13 = [25 x i8] c"Child: 1st message sent.\00"
@str.14 = [33 x i8] c"Parent receiving 1st message now\00"
@str.16 = [33 x i8] c"Parent receiving 2nd message now\00"
@str.17 = [41 x i8] c"There is an error when receiving message\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 (...) @fork()
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %2
%9 = tail call i64 @malloc(i32 100)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @strlen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%12 = tail call i64 @syscall(i32 292, i32 %5, i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.13, i64 0, i64 0))
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.12, i64 0, i64 0))
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i64 @syscall(i32 292, i32 %5, i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.13, i64 0, i64 0))
br label %39
18: ; preds = %2
%19 = call i32 @waitpid(i32 %6, i32* %3, i32 0)
%20 = call i64 @malloc(i32 100)
%21 = inttoptr i64 %20 to i8*
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.14, i64 0, i64 0))
%23 = call i64 @syscall(i32 293, i32 %6, i32 100, i8* %21)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i64 %23)
%25 = icmp sgt i64 %23, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i8* %21)
br label %30
28: ; preds = %18
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.17, i64 0, i64 0))
br label %30
30: ; preds = %28, %26
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.16, i64 0, i64 0))
%32 = call i64 @syscall(i32 293, i32 %6, i32 100, i8* %21)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0), i64 %32)
%34 = icmp sgt i64 %32, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i8* %21)
br label %39
37: ; preds = %30
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.17, i64 0, i64 0))
br label %39
39: ; preds = %35, %37, %8
ret i32 0
}