#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ command;

/* Variables and functions */
scalar_t__ __fork();
int /*<<< orphan*/ do_child(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ shell_command(char *);
int /*<<< orphan*/ wait(scalar_t__);
void fork_and_communicate(void) {
  pid_t child;
  char command[0x1000];
  memset(command, 0, sizeof(command));
  gets(command);
  if ((child = __fork()) < 0) {
    puts("There was a problem forking");
    fflush(NULL);
  } else {
    if (child == 0) {
      if (!shell_command(command))
        do_child(command);
    } else {
      wait(child);
    }
  }
  return;
}


x86 assembly (clang, -O0):
<fork_and_communicate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
lea    -0x1010(%rbp),%rdi
call   1070 <gets@plt>
mov    $0x0,%al
call   1050 <__fork@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jge    11d9 <fork_and_communicate+0x59>
lea    0xe3a(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <fflush@plt>
jmp    1218 <fork_and_communicate+0x98>
cmpq   $0x0,-0x8(%rbp)
jne    120a <fork_and_communicate+0x8a>
lea    -0x1010(%rbp),%rdi
call   1090 <shell_command@plt>
cmp    $0x0,%eax
jne    1205 <fork_and_communicate+0x85>
lea    -0x1010(%rbp),%rdi
call   1030 <do_child@plt>
jmp    1213 <fork_and_communicate+0x93>
mov    -0x8(%rbp),%rdi
call   10a0 <wait@plt>
jmp    1218 <fork_and_communicate+0x98>
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_and_communicate>:
push   %rbx
sub    $0x1000,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
call   1070 <gets@plt>
xor    %eax,%eax
call   1050 <__fork@plt>
test   %rax,%rax
js     11ba <fork_and_communicate+0x3a>
je     11cf <fork_and_communicate+0x4f>
mov    %rax,%rdi
call   10a0 <wait@plt>
jmp    11e3 <fork_and_communicate+0x63>
lea    0xe3f(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1080 <fflush@plt>
jmp    11e3 <fork_and_communicate+0x63>
mov    %rsp,%rdi
call   1090 <shell_command@plt>
test   %eax,%eax
jne    11e3 <fork_and_communicate+0x63>
mov    %rsp,%rdi
call   1030 <do_child@plt>
add    $0x1000,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [28 x i8] c"There was a problem forking\00"
define void @fork_and_communicate() {
%1 = alloca i64
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 4096)
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%6 = call i32 @gets(i8* %5)
%7 = call i64 (...) @__fork()
store i64 %7, i64* %1
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @fflush(i32* null)
br label %27
12: ; preds = %0
%13 = load i64, i64* %1
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %12
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%17 = call i32 @shell_command(i8* %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%21 = call i32 @do_child(i8* %20)
br label %22
22: ; preds = %19, %15
br label %26
23: ; preds = %12
%24 = load i64, i64* %1
%25 = call i32 @wait(i64 %24)
br label %26
26: ; preds = %23, %22
br label %27
27: ; preds = %26, %9
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"There was a problem forking\00"
define void @fork_and_communicate() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 0, i32 4096)
%4 = call i32 @gets(i8* %2)
%5 = call i64 (...) @__fork()
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @fflush(i32* null)
br label %19
10: ; preds = %0
%11 = icmp eq i64 %5, 0
br i1 %11, label %12, label %17
12: ; preds = %10
%13 = call i32 @shell_command(i8* %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %12
%16 = call i32 @do_child(i8* %2)
br label %19
17: ; preds = %10
%18 = call i32 @wait(i64 %5)
br label %19
19: ; preds = %17, %15, %12, %7
ret void
}