#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*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ F_DUPFD;
int /*<<< orphan*/ O_CLOEXEC;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert_fd(char *, int);
int /*<<< orphan*/ assert_fp(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int creat(char *, int);
int /*<<< orphan*/ dirfd(int /*<<< orphan*/ *);
int dup(int);
int dup2(int, int);
int dup3(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ *fdopen(int, char *);
int open(char *, int /*<<< orphan*/);
int open64(char *, int /*<<< orphan*/);
int openat(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int pipe(int *);
int pipe2(int *, int /*<<< orphan*/);
void test_fd(void) {
  FILE *fp;
  int fd, fd2, fd3, fd4, fds[2];
  fd = creat("file_out", 0666);
  assert_fd("creat", fd);
  close(fd);
  fd = open64("file_out", O_RDONLY);
  assert_fd("open64", fd);
  close(fd);
  fd = open("file_out", O_RDONLY);
  assert_fd("open", fd);
  fd2 = dup2(fd, 40);
  assert_fd("dup2", fd2);
  fd3 = dup3(fd, 60, O_CLOEXEC);
  assert_fd("dup3", fd3);
  fd4 = dup(fd2);
  assert_fd("dup", fd4);
  close(fd2);
  close(fd3);
  close(fd4);
  fd2 = fcntl(fd, F_DUPFD, 80);
  assert_fd("fcntl", fd2);
  close(fd);
  fp = fdopen(fd2, "r");
  assert_fp("fdopen", fp);
  fclose(fp);
  DIR *dir = opendir(".");
  fd = openat(dirfd(dir), "file_out", O_RDONLY);
  assert_fd("openat", fd);
  close(fd);
  closedir(dir);
  assert_fd("pipe", pipe(fds));
  close(fds[0]);
  close(fds[1]);
  assert_fd("pipe2", pipe2(fds, O_NONBLOCK));
  close(fds[0]);
  close(fds[1]);
}


x86 assembly (clang, -O0):
<test_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xdd1(%rip),%rdi
mov    $0x1b6,%esi
call   1130 <creat@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xdc3(%rip),%rdi
call   1050 <assert_fd@plt>
mov    -0xc(%rbp),%edi
call   10c0 <close@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
lea    0xd9d(%rip),%rdi
call   1110 <open64@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xd9a(%rip),%rdi
call   1050 <assert_fd@plt>
mov    -0xc(%rbp),%edi
call   10c0 <close@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
lea    0xd6e(%rip),%rdi
call   1120 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xd8a(%rip),%rdi
call   1050 <assert_fd@plt>
mov    -0xc(%rbp),%edi
mov    $0x28,%esi
call   10a0 <dup2@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xd53(%rip),%rdi
call   1050 <assert_fd@plt>
mov    -0xc(%rbp),%edi
mov    0x2cee(%rip),%rax
mov    (%rax),%edx
mov    $0x3c,%esi
call   1060 <dup3@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xd30(%rip),%rdi
call   1050 <assert_fd@plt>
mov    -0x10(%rbp),%edi
call   10b0 <dup@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd1b(%rip),%rdi
call   1050 <assert_fd@plt>
mov    -0x10(%rbp),%edi
call   10c0 <close@plt>
mov    -0x14(%rbp),%edi
call   10c0 <close@plt>
mov    -0x18(%rbp),%edi
call   10c0 <close@plt>
mov    -0xc(%rbp),%edi
mov    0x2cac(%rip),%rax
mov    (%rax),%esi
mov    $0x50,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
lea    0xcdf(%rip),%rdi
call   1050 <assert_fd@plt>
mov    -0xc(%rbp),%edi
call   10c0 <close@plt>
mov    -0x10(%rbp),%edi
lea    0xcce(%rip),%rsi
call   1100 <fdopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xcbc(%rip),%rdi
call   1030 <assert_fp@plt>
mov    -0x8(%rbp),%rdi
call   1070 <fclose@plt>
lea    0xcae(%rip),%rdi
call   1080 <opendir@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   10f0 <dirfd@plt>
mov    %eax,%edi
mov    0x2c40(%rip),%rax
mov    (%rax),%edx
lea    0xc57(%rip),%rsi
call   1090 <openat@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xc7a(%rip),%rdi
call   1050 <assert_fd@plt>
mov    -0xc(%rbp),%edi
call   10c0 <close@plt>
mov    -0x28(%rbp),%rdi
call   10e0 <closedir@plt>
lea    -0x20(%rbp),%rdi
call   10d0 <pipe@plt>
mov    %eax,%esi
lea    0xc59(%rip),%rdi
call   1050 <assert_fd@plt>
mov    -0x20(%rbp),%edi
call   10c0 <close@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <close@plt>
lea    -0x20(%rbp),%rdi
mov    0x2be5(%rip),%rax
mov    (%rax),%esi
call   1140 <pipe2@plt>
mov    %eax,%esi
lea    0xc2e(%rip),%rdi
call   1050 <assert_fd@plt>
mov    -0x20(%rbp),%edi
call   10c0 <close@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xdce(%rip),%r14
mov    %r14,%rdi
mov    $0x1b6,%esi
call   1130 <creat@plt>
mov    %eax,%ebx
lea    0xdc1(%rip),%rdi
mov    %eax,%esi
call   1050 <assert_fd@plt>
mov    %ebx,%edi
call   10c0 <close@plt>
mov    0x2d83(%rip),%r13
mov    0x0(%r13),%esi
mov    %r14,%rdi
call   1110 <open64@plt>
mov    %eax,%ebx
lea    0xd9d(%rip),%rdi
mov    %eax,%esi
call   1050 <assert_fd@plt>
mov    %ebx,%edi
call   10c0 <close@plt>
mov    0x0(%r13),%esi
mov    %r14,%rdi
call   1120 <open@plt>
mov    %eax,%r15d
lea    0xd98(%rip),%rdi
mov    %eax,%esi
call   1050 <assert_fd@plt>
mov    %r15d,%edi
mov    $0x28,%esi
call   10a0 <dup2@plt>
mov    %eax,%ebx
lea    0xd63(%rip),%rdi
mov    %eax,%esi
call   1050 <assert_fd@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    $0x3c,%esi
call   1060 <dup3@plt>
mov    %eax,%r12d
lea    0xd41(%rip),%rdi
mov    %eax,%esi
call   1050 <assert_fd@plt>
mov    %ebx,%edi
call   10b0 <dup@plt>
mov    %eax,%ebp
lea    0xd2f(%rip),%rdi
mov    %eax,%esi
call   1050 <assert_fd@plt>
mov    %ebx,%edi
call   10c0 <close@plt>
mov    %r12d,%edi
call   10c0 <close@plt>
mov    %ebp,%edi
call   10c0 <close@plt>
mov    0x2cc3(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    $0x50,%edx
call   1040 <fcntl@plt>
mov    %eax,%ebx
lea    0xcf7(%rip),%rdi
mov    %eax,%esi
call   1050 <assert_fd@plt>
mov    %r15d,%edi
call   10c0 <close@plt>
lea    0xce7(%rip),%rsi
mov    %ebx,%edi
call   1100 <fdopen@plt>
mov    %rax,%rbx
lea    0xcd8(%rip),%rdi
mov    %rax,%rsi
call   1030 <assert_fp@plt>
mov    %rbx,%rdi
call   1070 <fclose@plt>
lea    0xcc8(%rip),%rdi
call   1080 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   10f0 <dirfd@plt>
mov    0x0(%r13),%edx
mov    %eax,%edi
mov    %r14,%rsi
call   1090 <openat@plt>
mov    %eax,%ebp
lea    0xca3(%rip),%rdi
mov    %eax,%esi
call   1050 <assert_fd@plt>
mov    %ebp,%edi
call   10c0 <close@plt>
mov    %rbx,%rdi
call   10e0 <closedir@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10d0 <pipe@plt>
lea    0xc82(%rip),%rdi
mov    %eax,%esi
call   1050 <assert_fd@plt>
mov    (%rsp),%edi
call   10c0 <close@plt>
mov    0x4(%rsp),%edi
call   10c0 <close@plt>
mov    0x2c0f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1140 <pipe2@plt>
lea    0xc57(%rip),%rdi
mov    %eax,%esi
call   1050 <assert_fd@plt>
mov    (%rsp),%edi
call   10c0 <close@plt>
mov    0x4(%rsp),%edi
call   10c0 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"file_out\00"
@.str.1 = [6 x i8] c"creat\00"
@O_RDONLY = global i32 0
@.str.2 = [7 x i8] c"open64\00"
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [5 x i8] c"dup2\00"
@O_CLOEXEC = global i32 0
@.str.5 = [5 x i8] c"dup3\00"
@.str.6 = [4 x i8] c"dup\00"
@F_DUPFD = global i32 0
@.str.7 = [6 x i8] c"fcntl\00"
@.str.8 = [2 x i8] c"r\00"
@.str.9 = [7 x i8] c"fdopen\00"
@.str.10 = [2 x i8] c".\00"
@.str.11 = [7 x i8] c"openat\00"
@.str.12 = [5 x i8] c"pipe\00"
@.str.13 = [6 x i8] c"pipe2\00"
@O_NONBLOCK = global i32 0
define void @test_fd() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i32*
%8 = call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 438)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = call i32 @assert_fd(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %2
%12 = call i32 @close(i32 %11)
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open64(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %2
%15 = load i32, i32* %2
%16 = call i32 @assert_fd(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %2
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* @O_RDONLY
%20 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 %20, i32* %2
%21 = load i32, i32* %2
%22 = call i32 @assert_fd(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %2
%24 = call i32 @dup2(i32 %23, i32 40)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = call i32 @assert_fd(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %2
%28 = load i32, i32* @O_CLOEXEC
%29 = call i32 @dup3(i32 %27, i32 60, i32 %28)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = call i32 @assert_fd(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %3
%33 = call i32 @dup(i32 %32)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = call i32 @assert_fd(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %3
%37 = call i32 @close(i32 %36)
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* %2
%43 = load i32, i32* @F_DUPFD
%44 = call i32 @fcntl(i32 %42, i32 %43, i32 80)
store i32 %44, i32* %3
%45 = load i32, i32* %3
%46 = call i32 @assert_fd(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %2
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %3
%50 = call i32* @fdopen(i32 %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
store i32* %50, i32** %1
%51 = load i32*, i32** %1
%52 = call i32 @assert_fp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0), i32* %51)
%53 = load i32*, i32** %1
%54 = call i32 @fclose(i32* %53)
%55 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
store i32* %55, i32** %7
%56 = load i32*, i32** %7
%57 = call i32 @dirfd(i32* %56)
%58 = load i32, i32* @O_RDONLY
%59 = call i32 @openat(i32 %57, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %58)
store i32 %59, i32* %2
%60 = load i32, i32* %2
%61 = call i32 @assert_fd(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0), i32 %60)
%62 = load i32, i32* %2
%63 = call i32 @close(i32 %62)
%64 = load i32*, i32** %7
%65 = call i32 @closedir(i32* %64)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%67 = call i32 @pipe(i32* %66)
%68 = call i32 @assert_fd(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0), i32 %67)
%69 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%70 = load i32, i32* %69
%71 = call i32 @close(i32 %70)
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%73 = load i32, i32* %72
%74 = call i32 @close(i32 %73)
%75 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%76 = load i32, i32* @O_NONBLOCK
%77 = call i32 @pipe2(i32* %75, i32 %76)
%78 = call i32 @assert_fd(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.13, i64 0, i64 0), i32 %77)
%79 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%80 = load i32, i32* %79
%81 = call i32 @close(i32 %80)
%82 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%83 = load i32, i32* %82
%84 = call i32 @close(i32 %83)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"file_out\00"
@.str.1 = [6 x i8] c"creat\00"
@O_RDONLY = global i32 0
@.str.2 = [7 x i8] c"open64\00"
@.str.3 = [5 x i8] c"open\00"
@.str.4 = [5 x i8] c"dup2\00"
@O_CLOEXEC = global i32 0
@.str.5 = [5 x i8] c"dup3\00"
@.str.6 = [4 x i8] c"dup\00"
@F_DUPFD = global i32 0
@.str.7 = [6 x i8] c"fcntl\00"
@.str.8 = [2 x i8] c"r\00"
@.str.9 = [7 x i8] c"fdopen\00"
@.str.10 = [2 x i8] c".\00"
@.str.11 = [7 x i8] c"openat\00"
@.str.12 = [5 x i8] c"pipe\00"
@.str.13 = [6 x i8] c"pipe2\00"
@O_NONBLOCK = global i32 0
define void @test_fd() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = tail call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 438)
%4 = tail call i32 @assert_fd(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = tail call i32 @close(i32 %3)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open64(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i32 @assert_fd(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = tail call i32 @close(i32 %7)
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = tail call i32 @assert_fd(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = tail call i32 @dup2(i32 %11, i32 40)
%14 = tail call i32 @assert_fd(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @O_CLOEXEC
%16 = tail call i32 @dup3(i32 %11, i32 60, i32 %15)
%17 = tail call i32 @assert_fd(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %16)
%18 = tail call i32 @dup(i32 %13)
%19 = tail call i32 @assert_fd(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 %18)
%20 = tail call i32 @close(i32 %13)
%21 = tail call i32 @close(i32 %16)
%22 = tail call i32 @close(i32 %18)
%23 = load i32, i32* @F_DUPFD
%24 = tail call i32 @fcntl(i32 %11, i32 %23, i32 80)
%25 = tail call i32 @assert_fd(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 %24)
%26 = tail call i32 @close(i32 %11)
%27 = tail call i32* @fdopen(i32 %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%28 = tail call i32 @assert_fp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0), i32* %27)
%29 = tail call i32 @fclose(i32* %27)
%30 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
%31 = tail call i32 @dirfd(i32* %30)
%32 = load i32, i32* @O_RDONLY
%33 = tail call i32 @openat(i32 %31, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %32)
%34 = tail call i32 @assert_fd(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0), i32 %33)
%35 = tail call i32 @close(i32 %33)
%36 = tail call i32 @closedir(i32* %30)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%38 = call i32 @pipe(i32* %37)
%39 = call i32 @assert_fd(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %37
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = load i32, i32* @O_NONBLOCK
%46 = call i32 @pipe2(i32* %37, i32 %45)
%47 = call i32 @assert_fd(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.13, i64 0, i64 0), i32 %46)
%48 = load i32, i32* %37
%49 = call i32 @close(i32 %48)
%50 = load i32, i32* %42
%51 = call i32 @close(i32 %50)
ret void
}