#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 close(int);
int dup(int);
int dup2(int, int);
int errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int f, f2, f3;
  printf("DUP\n");
  f = open("/", O_RDONLY);
  f2 = open("/", O_RDONLY);
  f3 = dup(f);
  printf("errno: %d\n", errno);
  printf("f: %d\n", f != f2 && f != f3);
  printf("f2,f3: %d\n", f2 != f3);
  printf("close(f1): %d\n", close(f));
  printf("close(f2): %d\n", close(f2));
  printf("close(f3): %d\n", close(f3));
  printf("\n");
  errno = 0;
  printf("DUP2\n");
  f = open("/", O_RDONLY);
  f2 = open("/", O_RDONLY);
  f3 = dup2(f, f2);
  printf("errno: %d\n", errno);
  printf("f: %d\n", f != f2 && f != f3);
  printf("f2,f3: %d\n", f2 == f3);
  printf("close(f1): %d\n", close(f));
  printf("close(f2): %d\n", close(f2));
  printf("close(f3): %d\n", close(f3));
  printf("\n");
  errno = 0;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe70(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x11(%rbp)
je     11dc <main+0x8c>
mov    -0x8(%rbp),%eax
cmp    -0x10(%rbp),%eax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
and    $0x1,%al
movzbl %al,%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
setne  %al
and    $0x1,%al
movzbl %al,%esi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    %eax,%esi
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    %eax,%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    %eax,%esi
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d6d(%rip),%rax
movl   $0x0,(%rax)
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
lea    0xd76(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
lea    0xd5e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <dup2@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
lea    0xd3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x12(%rbp)
je     12f1 <main+0x1a1>
mov    -0x8(%rbp),%eax
cmp    -0x10(%rbp),%eax
setne  %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
and    $0x1,%al
movzbl %al,%esi
lea    0xd12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%esi
lea    0xcfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    %eax,%esi
lea    0xcf0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    %eax,%esi
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    %eax,%esi
lea    0xcde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xce3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c58(%rip),%rax
movl   $0x0,(%rax)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xeca(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e5a(%rip),%rbx
mov    (%rbx),%esi
lea    0xe69(%rip),%r12
mov    %r12,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
mov    (%rbx),%esi
mov    %r12,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1070 <dup@plt>
mov    %eax,%r14d
mov    0x2e1a(%rip),%r15
mov    (%r15),%esi
lea    0xe3a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    %ebx,%ebp
setne  %al
cmp    %r14d,%ebp
setne  %cl
and    %al,%cl
movzbl %cl,%esi
lea    0xe27(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %esi,%esi
cmp    %r14d,%ebx
setne  %sil
lea    0xe17(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0xe0d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
lea    0xe05(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%edi
call   1080 <close@plt>
lea    0xdfc(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x0,(%r15)
lea    0xdeb(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d77(%rip),%rbx
mov    (%rbx),%esi
mov    %r12,%rdi
call   1090 <open@plt>
mov    %eax,%r13d
mov    (%rbx),%esi
mov    %r12,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
mov    %r13d,%edi
mov    %eax,%esi
call   1050 <dup2@plt>
mov    %eax,%ebx
mov    (%r15),%esi
lea    0xd62(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    %ebp,%r13d
setne  %al
cmp    %ebx,%r13d
setne  %cl
and    %al,%cl
movzbl %cl,%esi
lea    0xd4e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %esi,%esi
cmp    %ebx,%ebp
sete   %sil
lea    0xd3f(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13d,%edi
call   1080 <close@plt>
lea    0xd34(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0xd2c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x0,(%r15)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"DUP\0A\00"
@.str.1 = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.2 = [11 x i8] c"errno: %d\0A\00"
@errno = global i32 0
@.str.3 = [7 x i8] c"f: %d\0A\00"
@.str.4 = [11 x i8] c"f2,f3: %d\0A\00"
@.str.5 = [15 x i8] c"close(f1): %d\0A\00"
@.str.6 = [15 x i8] c"close(f2): %d\0A\00"
@.str.7 = [15 x i8] c"close(f3): %d\0A\00"
@.str.8 = [2 x i8] c"\0A\00"
@.str.9 = [6 x i8] c"DUP2\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @O_RDONLY
%7 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %2
%11 = call i32 @dup(i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* @errno
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %2
%15 = load i32, i32* %3
%16 = icmp ne i32 %14, %15
br i1 %16, label %17, label %21
17: ; preds = %0
%18 = load i32, i32* %2
%19 = load i32, i32* %4
%20 = icmp ne i32 %18, %19
br label %21
21: ; preds = %17, %0
%22 = phi i1 [ false, %0 ], [ %20, %17 ]
%23 = zext i1 %22 to i32
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %3
%26 = load i32, i32* %4
%27 = icmp ne i32 %25, %26
%28 = zext i1 %27 to i32
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %3
%34 = call i32 @close(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %4
%37 = call i32 @close(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %37)
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* @errno
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%41 = load i32, i32* @O_RDONLY
%42 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %41)
store i32 %42, i32* %2
%43 = load i32, i32* @O_RDONLY
%44 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %43)
store i32 %44, i32* %3
%45 = load i32, i32* %2
%46 = load i32, i32* %3
%47 = call i32 @dup2(i32 %45, i32 %46)
store i32 %47, i32* %4
%48 = load i32, i32* @errno
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %2
%51 = load i32, i32* %3
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %57
53: ; preds = %21
%54 = load i32, i32* %2
%55 = load i32, i32* %4
%56 = icmp ne i32 %54, %55
br label %57
57: ; preds = %53, %21
%58 = phi i1 [ false, %21 ], [ %56, %53 ]
%59 = zext i1 %58 to i32
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %3
%62 = load i32, i32* %4
%63 = icmp eq i32 %61, %62
%64 = zext i1 %63 to i32
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %64)
%66 = load i32, i32* %2
%67 = call i32 @close(i32 %66)
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %67)
%69 = load i32, i32* %3
%70 = call i32 @close(i32 %69)
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %70)
%72 = load i32, i32* %4
%73 = call i32 @close(i32 %72)
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %73)
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
store i32 0, i32* @errno
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"/\00"
@O_RDONLY = global i32 0
@.str.2 = [11 x i8] c"errno: %d\0A\00"
@errno = global i32 0
@.str.3 = [7 x i8] c"f: %d\0A\00"
@.str.4 = [11 x i8] c"f2,f3: %d\0A\00"
@.str.5 = [15 x i8] c"close(f1): %d\0A\00"
@.str.6 = [15 x i8] c"close(f2): %d\0A\00"
@.str.7 = [15 x i8] c"close(f3): %d\0A\00"
@str = [4 x i8] c"DUP\00"
@str.10 = [5 x i8] c"DUP2\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = tail call i32 @dup(i32 %3)
%7 = load i32, i32* @errno
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = icmp ne i32 %3, %5
%10 = icmp ne i32 %3, %6
%11 = select i1 %9, i1 %10, i1 false
%12 = zext i1 %11 to i32
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = icmp ne i32 %5, %6
%15 = zext i1 %14 to i32
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = tail call i32 @close(i32 %3)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %17)
%19 = tail call i32 @close(i32 %5)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %19)
%21 = tail call i32 @close(i32 %6)
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %21)
%23 = tail call i32 @putchar(i32 10)
store i32 0, i32* @errno
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.10, i64 0, i64 0))
%25 = load i32, i32* @O_RDONLY
%26 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = load i32, i32* @O_RDONLY
%28 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = tail call i32 @dup2(i32 %26, i32 %28)
%30 = load i32, i32* @errno
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = icmp ne i32 %26, %28
%33 = icmp ne i32 %26, %29
%34 = select i1 %32, i1 %33, i1 false
%35 = zext i1 %34 to i32
%36 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = icmp eq i32 %28, %29
%38 = zext i1 %37 to i32
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = tail call i32 @close(i32 %26)
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %40)
%42 = tail call i32 @close(i32 %28)
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %42)
%44 = tail call i32 @close(i32 %29)
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i32 %44)
%46 = tail call i32 @putchar(i32 10)
store i32 0, i32* @errno
ret i32 0
}