#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*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ dup2(int, int);
int fork();
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char *argv[]) {
  int fd = open("abc", O_RDONLY);
  int fd2 = dup(fd);
  int fd3 = open("abc", O_RDONLY);
  int n;
  char buf[1024];
  int pid = fork();
  if (pid < 0) {
    perror("fork");
  } else if (pid == 0) {
    sleep(2);
    printf("child fd: %d\n", fd2);
    while ((n = read(fd, buf, 1024)) > 0) {
      write(STDOUT_FILENO, buf, n);
    }
    if (n < 0) {
      perror("read failed");
    }
  } else {
    lseek(fd2, 9, SEEK_SET);
    close(fd2);
    dup2(fd3, fd2);
    printf("parent fd: %d\n", fd2);
    while ((n = read(fd2, buf, 1024)) > 0) {
      write(STDOUT_FILENO, buf, n);
    }
    if (n < 0) {
      perror("read failed");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
lea    0xe04(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    122f <main+0x7f>
lea    0xddf(%rip),%rdi
call   10b0 <perror@plt>
jmp    1342 <main+0x192>
cmpl   $0x0,-0x424(%rbp)
jne    12ad <main+0xfd>
mov    $0x2,%edi
call   10c0 <sleep@plt>
mov    -0x18(%rbp),%esi
lea    0xdb9(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    1292 <main+0xe2>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
jmp    1255 <main+0xa5>
cmpl   $0x0,-0x20(%rbp)
jge    12a8 <main+0xf8>
lea    0xd74(%rip),%rdi
call   10b0 <perror@plt>
jmp    133d <main+0x18d>
mov    -0x18(%rbp),%edi
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
mov    $0x9,%esi
call   1070 <lseek@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%esi
lea    0xd43(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    1322 <main+0x172>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
jmp    12e5 <main+0x135>
cmpl   $0x0,-0x20(%rbp)
jge    1338 <main+0x188>
lea    0xce4(%rip),%rdi
call   10b0 <perror@plt>
jmp    133d <main+0x18d>
jmp    1342 <main+0x192>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    0x2e1c(%rip),%r15
mov    (%r15),%esi
lea    0xe32(%rip),%r14
mov    %r14,%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <dup@plt>
mov    %eax,%ebx
mov    (%r15),%esi
mov    %r14,%rdi
call   10a0 <open@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
js     12f4 <main+0x144>
je     1282 <main+0xd2>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x9,%esi
call   1070 <lseek@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14d,%edi
mov    %ebx,%esi
call   1040 <dup2@plt>
lea    0xdf2(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    12e0 <main+0x130>
mov    0x2d9a(%rip),%r14
mov    %rsp,%rbp
nopl   0x0(%rax)
mov    (%r14),%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
jg     1260 <main+0xb0>
jmp    12e0 <main+0x130>
mov    $0x2,%edi
call   10c0 <sleep@plt>
lea    0xd76(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    12e0 <main+0x130>
mov    0x2d3c(%rip),%r14
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
jg     12c0 <main+0x110>
test   %eax,%eax
js     12fd <main+0x14d>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd09(%rip),%rdi
jmp    1304 <main+0x154>
lea    0xd13(%rip),%rdi
call   10b0 <perror@plt>
jmp    12e4 <main+0x134>

LLVM IR (-O0):
@.str = [4 x i8] c"abc\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [14 x i8] c"child fd: %d\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [12 x i8] c"read failed\00"
@SEEK_SET = global i32 0
@.str.4 = [15 x i8] c"parent fd: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = call i32 @dup(i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %8
%18 = call i32 (...) @fork()
store i32 %18, i32* %11
%19 = load i32, i32* %11
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %74
23: ; preds = %2
%24 = load i32, i32* %11
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %46
26: ; preds = %23
%27 = call i32 @sleep(i32 2)
%28 = load i32, i32* %7
%29 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %35, %26
%31 = load i32, i32* %6
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 1024)
store i32 %33, i32* %9
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i32, i32* @STDOUT_FILENO
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%38 = load i32, i32* %9
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
br label %30
40: ; preds = %30
%41 = load i32, i32* %9
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %40
br label %73
46: ; preds = %23
%47 = load i32, i32* %7
%48 = load i32, i32* @SEEK_SET
%49 = call i32 @lseek(i32 %47, i32 9, i32 %48)
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %8
%53 = load i32, i32* %7
%54 = call i32 @dup2(i32 %52, i32 %53)
%55 = load i32, i32* %7
%56 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %62, %46
%58 = load i32, i32* %7
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%60 = call i32 @read(i32 %58, i8* %59, i32 1024)
store i32 %60, i32* %9
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %57
%63 = load i32, i32* @STDOUT_FILENO
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%65 = load i32, i32* %9
%66 = call i32 @write(i32 %63, i8* %64, i32 %65)
br label %57
67: ; preds = %57
%68 = load i32, i32* %9
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %67
%71 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %72
72: ; preds = %70, %67
br label %73
73: ; preds = %72, %45
br label %74
74: ; preds = %73, %21
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"abc\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [14 x i8] c"child fd: %d\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [12 x i8] c"read failed\00"
@SEEK_SET = global i32 0
@.str.4 = [15 x i8] c"parent fd: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @dup(i32 %5)
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%10 = tail call i32 (...) @fork()
%11 = icmp slt i32 %10, 0
br i1 %11, label %45, label %12
12: ; preds = %2
%13 = icmp eq i32 %10, 0
br i1 %13, label %14, label %28
14: ; preds = %12
%15 = tail call i32 @sleep(i32 2)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%17 = call i32 @read(i32 %5, i8* %9, i32 1024)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %14, %19
%20 = phi i32 [ %23, %19 ], [ %17, %14 ]
%21 = load i32, i32* @STDOUT_FILENO
%22 = call i32 @write(i32 %21, i8* %9, i32 %20)
%23 = call i32 @read(i32 %5, i8* %9, i32 1024)
%24 = icmp sgt i32 %23, 0
br i1 %24, label %19, label %25
25: ; preds = %19, %14
%26 = phi i32 [ %17, %14 ], [ %23, %19 ]
%27 = icmp slt i32 %26, 0
br i1 %27, label %45, label %48
28: ; preds = %12
%29 = load i32, i32* @SEEK_SET
%30 = tail call i32 @lseek(i32 %6, i32 9, i32 %29)
%31 = tail call i32 @close(i32 %6)
%32 = tail call i32 @dup2(i32 %8, i32 %6)
%33 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %6)
%34 = call i32 @read(i32 %6, i8* %9, i32 1024)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %28, %36
%37 = phi i32 [ %40, %36 ], [ %34, %28 ]
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @write(i32 %38, i8* %9, i32 %37)
%40 = call i32 @read(i32 %6, i8* %9, i32 1024)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %36, label %42
42: ; preds = %36, %28
%43 = phi i32 [ %34, %28 ], [ %40, %36 ]
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42, %25, %2
%46 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %2 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), %25 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), %42 ]
%47 = tail call i32 @perror(i8* %46)
br label %48
48: ; preds = %45, %25, %42
ret i32 0
}