#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int dup2(int, int);
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ execlp(char *, char *, char *, char *, char *, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, ...);
scalar_t__ fork();
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int main() {
  int fd[2];
  int ret1 = pipe(fd);
  if (ret1 < 0)
    err_sys("pipe error");
  pid_t pid = fork();
  if (pid < 0)
    err_sys("fork error");
  else if (0 == pid) {
    int flags = fcntl(fd[1], F_GETFD);
    flags &= ~FD_CLOEXEC;
    fcntl(fd[1], F_SETFD, flags);
    int ret2 = dup2(1, fd[1]);
    if (ret2 < 0)
      err_sys("dup2 error1");
    execlp("expr", "expr", "5", "+", "4", (char *)0);
    exit(0);
  } else {
    sleep(1);
    char a[1000];
    int n = read(fd[0], a, 1000);
    printf("n=%d", n);
    a[n] = '\0';
    printf("%s\n", a);
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11d4 <main+0x34>
lea    0xe31(%rip),%rdi
call   1030 <err_sys@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11fb <main+0x5b>
lea    0xe1a(%rip),%rdi
call   1030 <err_sys@plt>
jmp    12fe <main+0x15e>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    129b <main+0xfb>
mov    -0x8(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0x8(%rbp),%esi
mov    $0x1,%edi
call   1050 <dup2@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    126b <main+0xcb>
lea    0xdb0(%rip),%rdi
call   1030 <err_sys@plt>
lea    0xdb0(%rip),%rsi
lea    0xdae(%rip),%rdx
lea    0xda9(%rip),%rcx
lea    0xda4(%rip),%r8
xor    %eax,%eax
mov    %eax,%r9d
mov    %rsi,%rdi
call   10a0 <execlp@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3e8,%edx
call   1080 <read@plt>
mov    %eax,-0x414(%rbp)
mov    -0x414(%rbp),%esi
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x3f8,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %eax,%eax
jns    11d3 <main+0x23>
lea    0xe32(%rip),%rdi
call   1040 <err_sys@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
jns    11f7 <main+0x47>
lea    0xe25(%rip),%rdi
call   1040 <err_sys@plt>
xor    %eax,%eax
add    $0x3f8,%rsp
pop    %rbx
pop    %rbp
ret
jne    127e <main+0xce>
mov    0xc(%rsp),%edi
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <fcntl@plt>
mov    0x2dc0(%rip),%rcx
mov    (%rcx),%edx
not    %edx
and    %eax,%edx
mov    0xc(%rsp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1050 <fcntl@plt>
mov    0xc(%rsp),%esi
mov    $0x1,%edi
call   1060 <dup2@plt>
test   %eax,%eax
jns    1250 <main+0xa0>
lea    0xdcb(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xdcb(%rip),%rdi
lea    0xdc9(%rip),%rdx
lea    0xdc4(%rip),%rcx
lea    0xdbf(%rip),%r8
mov    %rdi,%rsi
xor    %r9d,%r9d
call   10b0 <execlp@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x3e8,%edx
call   1090 <read@plt>
mov    %eax,%ebp
lea    0xd86(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"pipe error\00"
@.str.1 = [11 x i8] c"fork error\00"
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@.str.2 = [12 x i8] c"dup2 error1\00"
@.str.3 = [5 x i8] c"expr\00"
@.str.4 = [2 x i8] c"5\00"
@.str.5 = [2 x i8] c"+\00"
@.str.6 = [2 x i8] c"4\00"
@.str.7 = [5 x i8] c"n=%d\00"
@.str.8 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1000 x i8]
%8 = alloca i32
store i32 0, i32* %1
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = call i64 (...) @fork()
store i64 %16, i64* %4
%17 = load i64, i64* %4
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %62
21: ; preds = %15
%22 = load i64, i64* %4
%23 = icmp eq i64 0, %22
br i1 %23, label %24, label %48
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = load i32, i32* @F_GETFD
%28 = call i32 (i32, i32, ...) @fcntl(i32 %26, i32 %27)
store i32 %28, i32* %5
%29 = load i32, i32* @FD_CLOEXEC
%30 = xor i32 %29, -1
%31 = load i32, i32* %5
%32 = and i32 %31, %30
store i32 %32, i32* %5
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%34 = load i32, i32* %33
%35 = load i32, i32* @F_SETFD
%36 = load i32, i32* %5
%37 = call i32 (i32, i32, ...) @fcntl(i32 %34, i32 %35, i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @dup2(i32 1, i32 %39)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %24
%44 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %24
%46 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i8* null)
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %21
%49 = call i32 @sleep(i32 1)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%51 = load i32, i32* %50
%52 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%53 = call i32 @read(i32 %51, i8* %52, i32 1000)
store i32 %53, i32* %8
%54 = load i32, i32* %8
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 %57
store i8 0, i8* %58
%59 = getelementptr inbounds [1000 x i8], [1000 x i8]* %7, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0), i8* %59)
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %19
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"pipe error\00"
@.str.1 = [11 x i8] c"fork error\00"
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@.str.2 = [12 x i8] c"dup2 error1\00"
@.str.3 = [5 x i8] c"expr\00"
@.str.4 = [2 x i8] c"5\00"
@.str.5 = [2 x i8] c"+\00"
@.str.6 = [2 x i8] c"4\00"
@.str.7 = [5 x i8] c"n=%d\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1000 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = call i64 (...) @fork()
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
14: ; preds = %9
%15 = icmp eq i64 %10, 0
br i1 %15, label %16, label %35
16: ; preds = %14
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%18 = load i32, i32* %17
%19 = load i32, i32* @F_GETFD
%20 = call i32 (i32, i32, ...) @fcntl(i32 %18, i32 %19)
%21 = load i32, i32* @FD_CLOEXEC
%22 = xor i32 %21, -1
%23 = and i32 %20, %22
%24 = load i32, i32* %17
%25 = load i32, i32* @F_SETFD
%26 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %25, i32 %23)
%27 = load i32, i32* %17
%28 = call i32 @dup2(i32 1, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %16
%31 = call i32 @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %16
%33 = call i32 @execlp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i8* null)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %14
%36 = call i32 @sleep(i32 1)
%37 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 0
%38 = load i32, i32* %4
%39 = call i32 @read(i32 %38, i8* %37, i32 1000)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0), i32 %39)
%41 = sext i32 %39 to i64
%42 = getelementptr inbounds [1000 x i8], [1000 x i8]* %2, i64 0, i64 %41
store i8 0, i8* %42
%43 = call i32 @puts(i8* %37)
%44 = call i32 @exit(i32 0)
unreachable
}