#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*/ F_GETFD;
int /*<<< orphan*/ O_RDONLY;
int errno;
int fcntl(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int f_getfd() {
  int fd = open("test.txt", O_RDONLY);
  if (fd == -1) {
    perror("read");
    return errno;
  }
  int newfd = fcntl(fd, F_GETFD);
  if (newfd == -1) {
    perror("F_DUPFD");
    return errno;
  }
  printf("fd %d\n", fd);
  printf("new fd %d\n", newfd);
  return 0;
}


x86 assembly (clang, -O0):
<f_getfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xea8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1187 <f_getfd+0x47>
lea    0xe98(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <f_getfd+0xa7>
mov    -0x8(%rbp),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c2 <f_getfd+0x82>
lea    0xe62(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <f_getfd+0xa7>
mov    -0x8(%rbp),%esi
lea    0xe4e(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe3b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f_getfd>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
lea    0xead(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     119b <f_getfd+0x5b>
mov    %eax,%ebx
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11a4 <f_getfd+0x64>
mov    %eax,%ebp
lea    0xe9d(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
lea    0xe8b(%rip),%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe67(%rip),%rdi
jmp    11ab <f_getfd+0x6b>
lea    0xe63(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
jmp    1194 <f_getfd+0x54>

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_GETFD = global i32 0
@.str.2 = [8 x i8] c"F_DUPFD\00"
@.str.3 = [7 x i8] c"fd %d\0A\00"
@.str.4 = [11 x i8] c"new fd %d\0A\00"
define i32 @f_getfd() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @errno
store i32 %10, i32* %1
br label %25
11: ; preds = %0
%12 = load i32, i32* %2
%13 = load i32, i32* @F_GETFD
%14 = call i32 @fcntl(i32 %12, i32 %13)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @errno
store i32 %19, i32* %1
br label %25
20: ; preds = %11
%21 = load i32, i32* %2
%22 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %3
%24 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %23)
store i32 0, i32* %1
br label %25
25: ; preds = %20, %17, %8
%26 = load i32, i32* %1
ret i32 %26
}

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_GETFD = global i32 0
@.str.2 = [8 x i8] c"F_DUPFD\00"
@.str.3 = [7 x i8] c"fd %d\0A\00"
@.str.4 = [11 x i8] c"new fd %d\0A\00"
define i32 @f_getfd() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%6 = load i32, i32* @errno
br label %17
7: ; preds = %0
%8 = load i32, i32* @F_GETFD
%9 = tail call i32 @fcntl(i32 %2, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%13 = load i32, i32* @errno
br label %17
14: ; preds = %7
%15 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %2)
%16 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %9)
br label %17
17: ; preds = %11, %14, %4
%18 = phi i32 [ %6, %4 ], [ %13, %11 ], [ 0, %14 ]
ret i32 %18
}