#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 /*<<< orphan*/ F_DUPFD;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memmove(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int f_dupfd() {
  int fd = open("test.txt", O_RDONLY);
  if (fd == -1) {
    perror("read");
    return errno;
  }
  int dupfd = fcntl(fd, F_DUPFD);
  if (dupfd == -1) {
    perror("F_DUPFD");
    return errno;
  }
  printf("new fd %d\n", dupfd);
  char buf[16];
  bzero(buf, sizeof(buf));
  int ret = read(dupfd, buf, sizeof(buf));
  if (ret == -1) {
    perror("read");
    return errno;
  }
  if (ret == 0) {
    printf("read over\n");
    return errno;
  }
  printf("%s\n", buf);
  if (lseek(dupfd, 0, SEEK_SET) == -1) {
    perror("lseek");
    return errno;
  }
  memset(buf, 0, sizeof(buf));
  memmove(buf, "how old are you", sizeof(buf));
  ret = write(dupfd, buf, sizeof(buf));
  if (ret == -1) {
    perror("write");
    return errno;
  }
  printf("%s\n", buf);
  close(dupfd);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<f_dupfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0xe38(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11f7 <f_dupfd+0x47>
lea    0xe28(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <f_dupfd+0x1d9>
mov    -0x8(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1232 <f_dupfd+0x82>
lea    0xdf2(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <f_dupfd+0x1d9>
mov    -0xc(%rbp),%esi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    $0x10,%esi
call   10d0 <bzero@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1090 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    128c <f_dupfd+0xdc>
lea    0xd93(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <f_dupfd+0x1d9>
cmpl   $0x0,-0x24(%rbp)
jne    12b5 <f_dupfd+0x105>
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <f_dupfd+0x1d9>
lea    -0x20(%rbp),%rsi
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1300 <f_dupfd+0x150>
lea    0xd46(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <f_dupfd+0x1d9>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <memset@plt>
lea    -0x20(%rbp),%rdi
lea    0xd1b(%rip),%rsi
mov    $0x10,%edx
call   10a0 <memmove@plt>
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1040 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1360 <f_dupfd+0x1b0>
lea    0xcfc(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1389 <f_dupfd+0x1d9>
lea    -0x20(%rbp),%rsi
lea    0xcc1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f_dupfd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
lea    0xe26(%rip),%rdi
call   10c0 <open@plt>
cmp    $0xffffffff,%eax
je     12c6 <f_dupfd+0x106>
mov    %eax,%r15d
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     12cf <f_dupfd+0x10f>
mov    %eax,%ebp
lea    0xe08(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
call   10e0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   10a0 <read@plt>
test   %eax,%eax
je     12b8 <f_dupfd+0xf8>
cmp    $0xffffffff,%eax
je     12c6 <f_dupfd+0x106>
mov    %rsp,%rdi
call   1030 <puts@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
cmp    $0xffffffff,%eax
je     12d8 <f_dupfd+0x118>
mov    %rsp,%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1080 <memset@plt>
lea    0xda9(%rip),%rsi
mov    %rbx,%rdi
mov    $0x10,%edx
call   10b0 <memmove@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     12e1 <f_dupfd+0x121>
mov    %rsp,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %r15d,%edi
call   1090 <close@plt>
jmp    12f7 <f_dupfd+0x137>
lea    0xd7e(%rip),%rdi
call   1030 <puts@plt>
jmp    12ed <f_dupfd+0x12d>
lea    0xd3c(%rip),%rdi
jmp    12e8 <f_dupfd+0x128>
lea    0xd38(%rip),%rdi
jmp    12e8 <f_dupfd+0x128>
lea    0xd42(%rip),%rdi
jmp    12e8 <f_dupfd+0x128>
lea    0xd4f(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2cdc(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"read\00"
@errno = global i32 0
@F_DUPFD = global i32 0
@.str.2 = [8 x i8] c"F_DUPFD\00"
@.str.3 = [11 x i8] c"new fd %d\0A\00"
@SEEK_SET = global i32 0
@.str.6 = [6 x i8] c"lseek\00"
@.str.7 = [16 x i8] c"how old are you\00"
@.str.8 = [6 x i8] c"write\00"
@str = [10 x i8] c"read over\00"
define i32 @f_dupfd() {
%1 = alloca [16 x i8]
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @errno
br label %48
8: ; preds = %0
%9 = load i32, i32* @F_DUPFD
%10 = tail call i32 @fcntl(i32 %3, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* @errno
br label %48
15: ; preds = %8
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%18 = call i32 @bzero(i8* %17, i32 16)
%19 = call i32 @read(i32 %10, i8* %17, i32 16)
switch i32 %19, label %26 [
i32 -1, label %20
i32 0, label %23
]
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @errno
br label %46
23: ; preds = %15
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%25 = load i32, i32* @errno
br label %46
26: ; preds = %15
%27 = call i32 @puts(i8* %17)
%28 = load i32, i32* @SEEK_SET
%29 = tail call i32 @lseek(i32 %10, i32 0, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
%33 = load i32, i32* @errno
br label %46
34: ; preds = %26
%35 = call i32 @memset(i8* %17, i32 0, i32 16)
%36 = call i32 @memmove(i8* %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 16)
%37 = call i32 @write(i32 %10, i8* %17, i32 16)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
%41 = load i32, i32* @errno
br label %46
42: ; preds = %34
%43 = call i32 @puts(i8* %17)
%44 = call i32 @close(i32 %10)
%45 = call i32 @close(i32 %3)
br label %46
46: ; preds = %42, %39, %31, %23, %20
%47 = phi i32 [ %22, %20 ], [ %25, %23 ], [ %33, %31 ], [ %41, %39 ], [ 0, %42 ]
br label %48
48: ; preds = %12, %46, %5
%49 = phi i32 [ %7, %5 ], [ %14, %12 ], [ %47, %46 ]
ret i32 %49
}