#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 /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int dup(int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execute(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ malloc(int);
int /*<<< orphan*/ pipe(int *);
char *strsep(char **, char *);
int /*<<< orphan*/ wait(int *);
void do_pipes(char *s) {
  char *this = (char *)malloc(256);
  this = strsep(&s, "|");
  int fd1 = dup(STDOUT_FILENO);
  int fd2 = dup(STDIN_FILENO);
  int fds[2];
  pipe(fds);
  int f = fork();
  int status;
  if (f == 0) {
    dup2(fds[1], STDOUT_FILENO);
    execute(this);
    exit(0);
  } else {
    wait(&status);
    dup2(fds[0], STDIN_FILENO);
    execute(s);
    dup2(fd2, STDIN_FILENO);
  }
}


x86 assembly (clang, -O0):
<do_pipes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1080 <strsep@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1040 <dup@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
call   1040 <dup@plt>
mov    %eax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    121e <do_pipes+0x8e>
mov    -0x1c(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x10(%rbp),%rdi
call   1050 <execute@plt>
xor    %edi,%edi
call   1090 <exit@plt>
lea    -0x28(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x20(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%rdi
call   1050 <execute@plt>
mov    -0x18(%rbp),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_pipes>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,(%rsp)
mov    $0x100,%edi
call   1070 <malloc@plt>
lea    0xe51(%rip),%rsi
mov    %rsp,%rdi
call   1080 <strsep@plt>
mov    %rax,%r14
mov    0x2e2f(%rip),%r15
mov    (%r15),%edi
call   1040 <dup@plt>
mov    0x2e08(%rip),%rbx
mov    (%rbx),%edi
call   1040 <dup@plt>
mov    %eax,%ebp
lea    0xc(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1220 <do_pipes+0x90>
lea    0x14(%rsp),%rdi
call   10a0 <wait@plt>
mov    0xc(%rsp),%edi
mov    (%rbx),%esi
call   1030 <dup2@plt>
mov    (%rsp),%rdi
call   1050 <execute@plt>
mov    (%rbx),%esi
mov    %ebp,%edi
call   1030 <dup2@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x10(%rsp),%edi
mov    (%r15),%esi
call   1030 <dup2@plt>
mov    %r14,%rdi
call   1050 <execute@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"|\00"
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
define void @do_pipes(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
%9 = call i64 @malloc(i32 256)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %3
%11 = call i8* @strsep(i8** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %3
%12 = load i32, i32* @STDOUT_FILENO
%13 = call i32 @dup(i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* @STDIN_FILENO
%15 = call i32 @dup(i32 %14)
store i32 %15, i32* %5
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%17 = call i32 @pipe(i32* %16)
%18 = call i32 (...) @fork()
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %1
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%23 = load i32, i32* %22
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @dup2(i32 %23, i32 %24)
%26 = load i8*, i8** %3
%27 = call i32 @execute(i8* %26)
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %1
%30 = call i32 @wait(i32* %8)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%32 = load i32, i32* %31
%33 = load i32, i32* @STDIN_FILENO
%34 = call i32 @dup2(i32 %32, i32 %33)
%35 = load i8*, i8** %2
%36 = call i32 @execute(i8* %35)
%37 = load i32, i32* %5
%38 = load i32, i32* @STDIN_FILENO
%39 = call i32 @dup2(i32 %37, i32 %38)
br label %40
40: ; preds = %29
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"|\00"
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
define void @do_pipes(i8* %0) {
%2 = alloca i8*
%3 = alloca [2 x i32]
%4 = alloca i32
store i8* %0, i8** %2
%5 = tail call i64 @malloc(i32 256)
%6 = call i8* @strsep(i8** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @STDOUT_FILENO
%8 = call i32 @dup(i32 %7)
%9 = load i32, i32* @STDIN_FILENO
%10 = call i32 @dup(i32 %9)
%11 = bitcast [2 x i32]* %3 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = call i32 (...) @fork()
%15 = bitcast i32* %4 to i8*
%16 = icmp eq i32 %14, 0
br i1 %16, label %17, label %24
17: ; preds = %1
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%19 = load i32, i32* %18
%20 = load i32, i32* @STDOUT_FILENO
%21 = call i32 @dup2(i32 %19, i32 %20)
%22 = call i32 @execute(i8* %6)
%23 = call i32 @exit(i32 0)
unreachable
24: ; preds = %1
%25 = call i32 @wait(i32* %4)
%26 = load i32, i32* %12
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @dup2(i32 %26, i32 %27)
%29 = load i8*, i8** %2
%30 = call i32 @execute(i8* %29)
%31 = load i32, i32* @STDIN_FILENO
%32 = call i32 @dup2(i32 %10, i32 %31)
ret void
}