#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_CUR;
int dup(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int main(void) {
  char *fname = "test.txt";
  int fd1, fd2, cnt;
  char buf[30];
  fd1 = open(fname, O_RDONLY);
  if (fd1 < 0) {
    perror("open()");
    exit(-1);
  }
  fd2 = dup(fd1);
  cnt = read(fd1, buf, 12);
  buf[cnt] = '\0';
  printf("fd1's printf: %s\n", buf);
  lseek(fd1, 1, SEEK_CUR);
  cnt = read(fd2, buf, 12);
  buf[cnt] = '\0';
  printf("fd2's printf: %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11bf <main+0x4f>
lea    0xe59(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xe1e(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1050 <lseek@plt>
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xded(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1209 <main+0x99>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <dup@plt>
mov    %eax,%r14d
mov    %rsp,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1060 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe54(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <lseek@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1060 <read@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe2c(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [18 x i8] c"fd1's printf: %s\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [18 x i8] c"fd2's printf: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [30 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %2
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %3
%17 = call i32 @dup(i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %3
%19 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 12)
store i32 %20, i32* %5
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 %22
store i8 0, i8* %23
%24 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%25 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load i32, i32* %3
%27 = load i32, i32* @SEEK_CUR
%28 = call i32 @lseek(i32 %26, i32 1, i32 %27)
%29 = load i32, i32* %4
%30 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 12)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 %33
store i8 0, i8* %34
%35 = getelementptr inbounds [30 x i8], [30 x i8]* %6, i64 0, i64 0
%36 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %35)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [18 x i8] c"fd1's printf: %s\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [18 x i8] c"fd2's printf: %s\0A\00"
define i32 @main() {
%1 = alloca [30 x i8]
%2 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %0
%10 = tail call i32 @dup(i32 %4)
%11 = call i32 @read(i32 %4, i8* %2, i32 12)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%15 = load i32, i32* @SEEK_CUR
%16 = call i32 @lseek(i32 %4, i32 1, i32 %15)
%17 = call i32 @read(i32 %10, i8* %2, i32 12)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [30 x i8], [30 x i8]* %1, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %2)
ret i32 0
}