#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_DUPFD;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int main(void) {
  char *path = "/etc/passwd";
  int fd = open(path, O_RDONLY);
  if (fd >= 0) {
    int fd2 = fcntl(fd, F_DUPFD, fd);
    if (fd2 >= 0)
      close(fd2);
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     119a <main+0x6a>
mov    -0x14(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1192 <main+0x62>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    (%rax),%esi
lea    0xebf(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     1170 <main+0x40>
mov    %eax,%ebx
mov    0x2e8d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1169 <main+0x39>
mov    %eax,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@F_DUPFD = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
%5 = load i8*, i8** %2
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* %5, i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp sge i32 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %0
%11 = load i32, i32* %3
%12 = load i32, i32* @F_DUPFD
%13 = load i32, i32* %3
%14 = call i32 @fcntl(i32 %11, i32 %12, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp sge i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i32, i32* %4
%19 = call i32 @close(i32 %18)
br label %20
20: ; preds = %17, %10
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %20, %0
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@F_DUPFD = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp sgt i32 %2, -1
br i1 %3, label %4, label %12
4: ; preds = %0
%5 = load i32, i32* @F_DUPFD
%6 = tail call i32 @fcntl(i32 %2, i32 %5, i32 %2)
%7 = icmp sgt i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @close(i32 %6)
br label %10
10: ; preds = %8, %4
%11 = tail call i32 @close(i32 %2)
br label %12
12: ; preds = %10, %0
ret i32 0
}