#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 /*<<< orphan*/ buf;

/* Variables and functions */
int dup(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main() {
  int fd_in = dup(0);
  if (fd_in == -1) {
    printf("fd_in dup failed\n");
  } else {
    printf("dup(0) = %d\n", fd_in);
  }
  int fd_out;
  if ((fd_out = dup(1)) == -1) {
    printf("fd_out dup failed\n");
  } else {
    printf("dup(1) = %d\n", fd_out);
  }
  int fds[2] = {fd_in, fd_out};
  pipe(fds);
  char buf[256] = {0};
  int n;
  n = write(fd_out, "test", 4);
  printf("@write, n = %d\n", n);
  printf("@write, buf = %s\n", buf);
  n = read(fd_in, buf, sizeof(buf));
  if (n == -1) {
    fprintf(stderr, "read error");
  } else if (n == 0) {
    fprintf(stderr, "read to the end of the file");
  } else {
    fprintf(stderr, "@read, n = %d\n", n);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
call   1050 <dup@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a9 <main+0x39>
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11ba <main+0x4a>
mov    -0x8(%rbp),%esi
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1050 <dup@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11e3 <main+0x73>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    11f4 <main+0x84>
mov    -0xc(%rbp),%esi
lea    0xe45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1070 <pipe@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    -0xc(%rbp),%edi
lea    0xe19(%rip),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    %eax,-0x124(%rbp)
mov    -0x124(%rbp),%esi
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x120(%rbp),%rsi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1080 <read@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    12a2 <main+0x132>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    12ed <main+0x17d>
cmpl   $0x0,-0x124(%rbp)
jne    12cb <main+0x15b>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    12e8 <main+0x178>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
mov    -0x124(%rbp),%edx
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    12ed <main+0x17d>
xor    %eax,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x118,%rsp
xor    %edi,%edi
call   1060 <dup@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1199 <main+0x29>
lea    0xe72(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11a5 <main+0x35>
lea    0xee9(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <dup@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11c8 <main+0x58>
lea    0xe50(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11d4 <main+0x64>
lea    0xea8(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,0x8(%rsp)
mov    %ebp,0xc(%rsp)
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
lea    0xdbf(%rip),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1040 <write@plt>
lea    0xdb1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdb1(%rip),%rdi
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
call   1080 <read@plt>
test   %eax,%eax
je     12b7 <main+0x147>
cmp    $0xffffffff,%eax
jne    12d0 <main+0x160>
mov    0x2d34(%rip),%rax
mov    (%rax),%edi
lea    0xd8c(%rip),%rsi
jmp    12c7 <main+0x157>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    12e9 <main+0x179>
mov    0x2d09(%rip),%rcx
mov    (%rcx),%edi
lea    0xd88(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"fd_in dup failed\0A\00"
@.str.1 = [13 x i8] c"dup(0) = %d\0A\00"
@.str.2 = [19 x i8] c"fd_out dup failed\0A\00"
@.str.3 = [13 x i8] c"dup(1) = %d\0A\00"
@.str.4 = [5 x i8] c"test\00"
@.str.5 = [16 x i8] c"@write, n = %d\0A\00"
@.str.6 = [18 x i8] c"@write, buf = %s\0A\00"
@stderr = global i32 0
@.str.7 = [11 x i8] c"read error\00"
@.str.8 = [28 x i8] c"read to the end of the file\00"
@.str.9 = [15 x i8] c"@read, n = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i32]
%5 = alloca [256 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @dup(i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
br label %15
12: ; preds = %0
%13 = load i32, i32* %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %10
%16 = call i32 @dup(i32 1)
store i32 %16, i32* %3
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %23
20: ; preds = %15
%21 = load i32, i32* %3
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %20, %18
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%25 = load i32, i32* %2
store i32 %25, i32* %24
%26 = getelementptr inbounds i32, i32* %24, i64 1
%27 = load i32, i32* %3
store i32 %27, i32* %26
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%29 = call i32 @pipe(i32* %28)
%30 = bitcast [256 x i8]* %5 to i8*
%31 = load i32, i32* %3
%32 = call i32 @write(i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %2
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%39 = call i32 @read(i32 %37, i8* %38, i32 256)
store i32 %39, i32* %6
%40 = load i32, i32* %6
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %23
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
br label %56
45: ; preds = %23
%46 = load i32, i32* %6
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %55
51: ; preds = %45
%52 = load i32, i32* @stderr
%53 = load i32, i32* %6
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %51, %48
br label %56
56: ; preds = %55, %42
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"dup(0) = %d\0A\00"
@.str.3 = [13 x i8] c"dup(1) = %d\0A\00"
@.str.4 = [5 x i8] c"test\00"
@.str.5 = [16 x i8] c"@write, n = %d\0A\00"
@.str.6 = [18 x i8] c"@write, buf = %s\0A\00"
@stderr = global i32 0
@.str.7 = [11 x i8] c"read error\00"
@.str.8 = [28 x i8] c"read to the end of the file\00"
@.str.9 = [15 x i8] c"@read, n = %d\0A\00"
@str = [18 x i8] c"fd_out dup failed\00"
@str.10 = [17 x i8] c"fd_in dup failed\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [256 x i8]
%3 = tail call i32 @dup(i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.10, i64 0, i64 0))
br label %9
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %3)
br label %9
9: ; preds = %7, %5
%10 = tail call i32 @dup(i32 1)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %16
14: ; preds = %9
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %10)
br label %16
16: ; preds = %14, %12
%17 = bitcast [2 x i32]* %1 to i8*
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
store i32 %3, i32* %18
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
store i32 %10, i32* %19
%20 = call i32 @pipe(i32* %18)
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%22 = call i32 @write(i32 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 4)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i8* %21)
%25 = call i32 @read(i32 %3, i8* %21, i32 256)
switch i32 %25, label %32 [
i32 -1, label %26
i32 0, label %29
]
26: ; preds = %16
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0))
br label %35
29: ; preds = %16
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %35
32: ; preds = %16
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0), i32 %25)
br label %35
35: ; preds = %29, %32, %26
ret i32 0
}