#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;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int open(char *, int, int);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
void main() {
  int i;
  char input_buf[512];
  for (i = 0; i < 512; i++)
    input_buf[i] = '\0';
  char output_buf[512];
  int pipe_fd[2];
  int file_fd;
  int count;
  pid_t pid = 0;
  printf("Please input some words:\n");
  scanf("%s", input_buf);
  count = strlen(input_buf);
  pipe(pipe_fd);
  pid = fork();
  if (pid > 0) {
    write(pipe_fd[1], input_buf, count);
    wait(NULL);
    close(pipe_fd[1]);
    exit(0);
  } else {
    file_fd = open("./file.txt", O_RDWR | O_CREAT, 0777);
    count = read(pipe_fd[0], output_buf, count);
    write(file_fd, output_buf, count);
    close(pipe_fd[0]);
    close(file_fd);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
cmpl   $0x200,-0x4(%rbp)
jge    11e9 <main+0x39>
movslq -0x4(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    11c2 <main+0x12>
movq   $0x0,-0x428(%rbp)
lea    0xe05(%rip),%rdi
call   1050 <printf@plt>
lea    -0x210(%rbp),%rsi
lea    0xe0c(%rip),%rdi
call   1090 <scanf@plt>
lea    -0x210(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x420(%rbp)
lea    -0x418(%rbp),%rdi
call   1070 <pipe@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jle    1280 <main+0xd0>
mov    -0x414(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    -0x420(%rbp),%edx
call   1030 <write@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    -0x414(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    0x2d68(%rip),%rax
or     (%rax),%esi
lea    0xd84(%rip),%rdi
mov    $0x1ff,%edx
call   10a0 <open@plt>
mov    %eax,-0x41c(%rbp)
mov    -0x418(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x420(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x420(%rbp)
mov    -0x41c(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x420(%rbp),%edx
call   1030 <write@plt>
mov    -0x418(%rbp),%edi
call   1060 <close@plt>
mov    -0x41c(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
lea    0x10(%rsp),%rbx
mov    $0x200,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xe28(%rip),%rdi
call   1030 <puts@plt>
lea    0xe0e(%rip),%rdi
mov    %rbx,%rsi
call   10a0 <scanf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
jle    1237 <main+0x77>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    %ebx,%edx
call   1040 <write@plt>
xor    %edi,%edi
call   10d0 <wait@plt>
mov    0xc(%rsp),%edi
jmp    1289 <main+0xc9>
mov    0x2da2(%rip),%rax
mov    0x2db3(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xdb3(%rip),%rdi
mov    $0x1ff,%edx
call   10b0 <open@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%edi
lea    0x210(%rsp),%r14
mov    %r14,%rsi
mov    %ebx,%edx
call   1090 <read@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"Please input some words:\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [11 x i8] c"./file.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
define void @main() {
%1 = alloca i32
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i32 0, i32* %1
br label %8
8: ; preds = %15, %0
%9 = load i32, i32* %1
%10 = icmp slt i32 %9, 512
br i1 %10, label %11, label %18
11: ; preds = %8
%12 = load i32, i32* %1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %11
%16 = load i32, i32* %1
%17 = add nsw i32 %16, 1
store i32 %17, i32* %1
br label %8
18: ; preds = %8
store i64 0, i64* %7
%19 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%21 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
store i32 %23, i32* %6
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%25 = call i32 @pipe(i32* %24)
%26 = call i64 (...) @fork()
store i64 %26, i64* %7
%27 = load i64, i64* %7
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %40
29: ; preds = %18
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%33 = load i32, i32* %6
%34 = call i32 @write(i32 %31, i8* %32, i32 %33)
%35 = call i32 @wait(i32* null)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %18
%41 = load i32, i32* @O_RDWR
%42 = load i32, i32* @O_CREAT
%43 = or i32 %41, %42
%44 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %43, i32 511)
store i32 %44, i32* %5
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%46 = load i32, i32* %45
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%48 = load i32, i32* %6
%49 = call i32 @read(i32 %46, i8* %47, i32 %48)
store i32 %49, i32* %6
%50 = load i32, i32* %5
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%52 = load i32, i32* %6
%53 = call i32 @write(i32 %50, i8* %51, i32 %52)
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %5
%58 = call i32 @close(i32 %57)
%59 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [11 x i8] c"./file.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@str = [25 x i8] c"Please input some words:\00"
define void @main() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = alloca [512 x i8]
%4 = alloca [2 x i32]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = bitcast [2 x i32]* %4 to i8*
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%9 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%10 = call i32 @strlen(i8* %5)
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = call i64 (...) @fork()
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %0
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @write(i32 %17, i8* %5, i32 %10)
%19 = call i32 @wait(i32* null)
%20 = load i32, i32* %16
%21 = call i32 @close(i32 %20)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %0
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %25, %24
%27 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 511)
%28 = load i32, i32* %11
%29 = call i32 @read(i32 %28, i8* %6, i32 %10)
%30 = call i32 @write(i32 %27, i8* %6, i32 %29)
%31 = load i32, i32* %11
%32 = call i32 @close(i32 %31)
%33 = call i32 @close(i32 %27)
%34 = call i32 @exit(i32 0)
unreachable
}