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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dprintf(int, char *, char *);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ execl(char *, char *, char *, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int waitpid(int, int *, int /*<<< orphan*/);
int main() {
  char *line = NULL;
  size_t length;
  pid_t pid;
  while (getline(&line, &length, stdin) != -1) {
    int fd[2];
    if (pipe(fd) == -1)
      error("Couldn't create pipe");
    pid = fork();
    if (pid == -1)
      error("Couldn't fork");
    if (!pid) {
      close(fd[1]);
      dup2(fd[0], 0);
      free(line);
      line = NULL;
      execl("../lsa", "../lsa", "-b", 0);
    }
    close(fd[0]);
    printf("Input: %s\n", line);
    dprintf(fd[1], "%s", line);
    free(line);
    line = NULL;
    int pid_status;
    if (waitpid(pid, &pid_status, 0) == -1)
      error("Waiting for child process");
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   10b0 <getline@plt>
cmp    $0xffffffff,%eax
je     12dd <main+0x12d>
lea    -0x24(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1204 <main+0x54>
lea    0xe01(%rip),%rdi
call   1090 <error@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1224 <main+0x74>
lea    0xdf6(%rip),%rdi
call   1090 <error@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1269 <main+0xb9>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
lea    0xdcb(%rip),%rsi
lea    0xdcb(%rip),%rdx
xor    %ecx,%ecx
mov    %rsi,%rdi
call   10c0 <execl@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rsi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rdx
lea    0xda7(%rip),%rsi
call   1070 <dprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    12ca <main+0x11a>
lea    0xd76(%rip),%rdi
call   1090 <error@plt>
lea    0xd65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11c7 <main+0x17>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movq   $0x0,0x8(%rsp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   10c0 <getline@plt>
cmp    $0xffffffff,%eax
je     1332 <main+0x172>
lea    0xe2e(%rip),%rbx
lea    0xe32(%rip),%rbp
lea    0x24(%rsp),%r12
lea    0x8(%rsp),%r14
lea    0x18(%rsp),%r15
jmp    1247 <main+0x87>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %r15,%rsi
call   10c0 <getline@plt>
cmp    $0xffffffff,%eax
je     1332 <main+0x172>
lea    0x10(%rsp),%rdi
call   1090 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1262 <main+0xa2>
lea    0xda3(%rip),%rdi
call   10a0 <error@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,%r13d
test   %eax,%eax
je     1290 <main+0xd0>
cmp    $0xffffffff,%r13d
jne    12cf <main+0x10f>
lea    0xd98(%rip),%rdi
call   10a0 <error@plt>
jmp    12cf <main+0x10f>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x14(%rsp),%edi
call   1070 <close@plt>
mov    0x10(%rsp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
movq   $0x0,0x8(%rsp)
lea    0xd65(%rip),%rdi
mov    %rdi,%rsi
lea    0xd62(%rip),%rdx
xor    %ecx,%ecx
call   10d0 <execl@plt>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x14(%rsp),%edi
mov    0x8(%rsp),%rdx
mov    %rbp,%rsi
call   1080 <dprintf@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
movq   $0x0,0x8(%rsp)
mov    %r13d,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   10b0 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1220 <main+0x60>
lea    0xd13(%rip),%rdi
call   10a0 <error@plt>
jmp    1220 <main+0x60>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [21 x i8] c"Couldn't create pipe\00"
@.str.1 = [14 x i8] c"Couldn't fork\00"
@.str.2 = [7 x i8] c"../lsa\00"
@.str.3 = [3 x i8] c"-b\00"
@.str.4 = [11 x i8] c"Input: %s\0A\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [26 x i8] c"Waiting for child process\00"
@.str.7 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = alloca i32
store i32 0, i32* %1
store i8* null, i8** %2
br label %7
7: ; preds = %53, %0
%8 = load i32, i32* @stdin
%9 = call i32 @getline(i8** %2, i64* %3, i32 %8)
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %55
11: ; preds = %7
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = call i32 (...) @fork()
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = load i32, i32* %4
%25 = icmp ne i32 %24, 0
br i1 %25, label %36, label %26
26: ; preds = %23
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%31 = load i32, i32* %30
%32 = call i32 @dup2(i32 %31, i32 0)
%33 = load i8*, i8** %2
%34 = call i32 @free(i8* %33)
store i8* null, i8** %2
%35 = call i32 @execl(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 0)
br label %36
36: ; preds = %26, %23
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %2
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%43 = load i32, i32* %42
%44 = load i8*, i8** %2
%45 = call i32 @dprintf(i32 %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %44)
%46 = load i8*, i8** %2
%47 = call i32 @free(i8* %46)
store i8* null, i8** %2
%48 = load i32, i32* %4
%49 = call i32 @waitpid(i32 %48, i32* %6, i32 0)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %36
%52 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %53
53: ; preds = %51, %36
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
br label %7
55: ; preds = %7
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [21 x i8] c"Couldn't create pipe\00"
@.str.1 = [14 x i8] c"Couldn't fork\00"
@.str.2 = [7 x i8] c"../lsa\00"
@.str.3 = [3 x i8] c"-b\00"
@.str.4 = [11 x i8] c"Input: %s\0A\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [26 x i8] c"Waiting for child process\00"
define i32 @main() {
%1 = alloca i8*
%2 = alloca i64
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%6 = bitcast i64* %2 to i8*
%7 = load i32, i32* @stdin
%8 = call i32 @getline(i8** %1, i64* %2, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %51, label %10
10: ; preds = %0
%11 = bitcast [2 x i32]* %3 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%14 = bitcast i32* %4 to i8*
br label %15
15: ; preds = %10, %46
%16 = call i32 @pipe(i32* %12)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = call i32 (...) @fork()
switch i32 %21, label %32 [
i32 -1, label %22
i32 0, label %24
]
22: ; preds = %20
%23 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %32
24: ; preds = %20
%25 = load i32, i32* %13
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %12
%28 = call i32 @dup2(i32 %27, i32 0)
%29 = load i8*, i8** %1
%30 = call i32 @free(i8* %29)
store i8* null, i8** %1
%31 = call i32 @execl(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 0)
br label %32
32: ; preds = %22, %20, %24
%33 = load i32, i32* %12
%34 = call i32 @close(i32 %33)
%35 = load i8*, i8** %1
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %13
%38 = load i8*, i8** %1
%39 = call i32 @dprintf(i32 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %38)
%40 = load i8*, i8** %1
%41 = call i32 @free(i8* %40)
store i8* null, i8** %1
%42 = call i32 @waitpid(i32 %21, i32* %4, i32 0)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %32
%45 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
br label %46
46: ; preds = %44, %32
%47 = call i32 @putchar(i32 10)
%48 = load i32, i32* @stdin
%49 = call i32 @getline(i8** %1, i64* %2, i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %15
51: ; preds = %46, %0
ret i32 0
}