#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ runsyscmd(char *);
int /*<<< orphan*/ write(int, char *, int);
int runpipe(char *cmd) {
  int fd[2], err = 1;
  pid_t pid;
  char c;
  if (cmd != NULL && *cmd && !pipe(fd)) {
    if ((pid = fork()) < 0)
      exit(1);
    else if (pid > 0) {
      close(fd[1]);
      while (read(fd[0], &c, 1))
        write(1, &c, 1);
      err = 0;
    } else {
      close(fd[0]);
      runsyscmd(cmd);
      exit(1);
    }
  }
  return err;
}


x86 assembly (clang, -O0):
<runpipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x1,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1243 <runpipe+0xd3>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1243 <runpipe+0xd3>
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%eax
jne    1243 <runpipe+0xd3>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    11cf <runpipe+0x5f>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jle    121e <runpipe+0xae>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
je     1212 <runpipe+0xa2>
mov    $0x1,%edx
lea    -0x21(%rbp),%rsi
mov    %edx,%edi
call   1040 <write@plt>
jmp    11e2 <runpipe+0x72>
movl   $0x0,-0x14(%rbp)
jmp    1239 <runpipe+0xc9>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%rdi
call   1030 <runsyscmd@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    123e <runpipe+0xce>
jmp    1243 <runpipe+0xd3>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runpipe>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    $0x1,%ebp
test   %rdi,%rdi
je     11f8 <runpipe+0x88>
mov    %rdi,%rbx
cmpb   $0x0,(%rdi)
je     11f8 <runpipe+0x88>
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
test   %eax,%eax
jne    11f8 <runpipe+0x88>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1201 <runpipe+0x91>
je     120b <runpipe+0x9b>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     11f8 <runpipe+0x88>
lea    0xf(%rsp),%rbx
nopl   0x0(%rax)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jne    11d0 <runpipe+0x60>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    %rbx,%rdi
call   1030 <runsyscmd@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
define i32 @runpipe(i8* %0) {
%2 = alloca i8*
%3 = alloca [2 x i32]
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8
store i8* %0, i8** %2
store i32 1, i32* %4
%7 = load i8*, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %47
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %47
14: ; preds = %9
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %47, label %18
18: ; preds = %14
%19 = call i64 (...) @fork()
store i64 %19, i64* %5
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %18
%24 = load i64, i64* %5
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %38
26: ; preds = %23
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %35, %26
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i64 @read(i32 %32, i8* %6, i32 1)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @write(i32 1, i8* %6, i32 1)
br label %30
37: ; preds = %30
store i32 0, i32* %4
br label %45
38: ; preds = %23
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = load i8*, i8** %2
%43 = call i32 @runsyscmd(i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %37
br label %46
46: ; preds = %45
br label %47
47: ; preds = %46, %14, %9, %1
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
define i32 @runpipe(i8* %0) {
%2 = alloca [2 x i32]
%3 = alloca i8
%4 = bitcast [2 x i32]* %2 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %37, label %6
6: ; preds = %1
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %37, label %9
9: ; preds = %6
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %37
13: ; preds = %9
%14 = call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %13
%19 = icmp eq i64 %14, 0
br i1 %19, label %32, label %20
20: ; preds = %18
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* %10
%25 = call i64 @read(i32 %24, i8* %3, i32 1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %20, %27
%28 = call i32 @write(i32 1, i8* %3, i32 1)
%29 = load i32, i32* %10
%30 = call i64 @read(i32 %29, i8* %3, i32 1)
%31 = icmp eq i64 %30, 0
br i1 %31, label %37, label %27
32: ; preds = %18
%33 = load i32, i32* %10
%34 = call i32 @close(i32 %33)
%35 = call i32 @runsyscmd(i8* %0)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27, %20, %9, %6, %1
%38 = phi i32 [ 1, %9 ], [ 1, %6 ], [ 1, %1 ], [ 0, %20 ], [ 0, %27 ]
ret i32 %38
}