#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*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
void dup2_inout(char *file, int from, mode_t mode) {
  int to = open(file, mode);
  if (to == from || to == -1)
    return;
  while (1) {
    dup2(to, from);
    fcntl(from, F_SETFD, 0);
    if (from++ != 1)
      break;
  }
  close(to);
}


x86 assembly (clang, -O0):
<dup2_inout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1177 <dup2_inout+0x37>
cmpl   $0xffffffff,-0x14(%rbp)
jne    117c <dup2_inout+0x3c>
jmp    11c5 <dup2_inout+0x85>
jmp    1181 <dup2_inout+0x41>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x1,%eax
je     11b8 <dup2_inout+0x78>
jmp    11bd <dup2_inout+0x7d>
jmp    1181 <dup2_inout+0x41>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dup2_inout>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %edx,%esi
call   1060 <open@plt>
cmp    %ebp,%eax
je     119f <dup2_inout+0x5f>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     119f <dup2_inout+0x5f>
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <dup2@plt>
mov    0x2e78(%rip),%r14
mov    (%r14),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0x1,%ebp
jne    1194 <dup2_inout+0x54>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    (%r14),%esi
mov    $0x2,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <close@plt>
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@F_SETFD = global i32 0
define void @dup2_inout(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i8*, i8** %4
%9 = load i32, i32* %6
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = load i32, i32* %5
%13 = icmp eq i32 %11, %12
br i1 %13, label %17, label %14
14: ; preds = %3
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %14, %3
br label %34
18: ; preds = %14
br label %19
19: ; preds = %18, %30
%20 = load i32, i32* %7
%21 = load i32, i32* %5
%22 = call i32 @dup2(i32 %20, i32 %21)
%23 = load i32, i32* %5
%24 = load i32, i32* @F_SETFD
%25 = call i32 @fcntl(i32 %23, i32 %24, i32 0)
%26 = load i32, i32* %5
%27 = add nsw i32 %26, 1
store i32 %27, i32* %5
%28 = icmp ne i32 %26, 1
br i1 %28, label %29, label %30
29: ; preds = %19
br label %31
30: ; preds = %19
br label %19
31: ; preds = %29
%32 = load i32, i32* %7
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %31, %17
ret void
}

LLVM IR (-O2):
@F_SETFD = global i32 0
define void @dup2_inout(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32 @open(i8* %0, i32 %2)
%5 = icmp eq i32 %4, %1
%6 = icmp eq i32 %4, -1
%7 = or i1 %5, %6
br i1 %7, label %19, label %8
8: ; preds = %3
%9 = tail call i32 @dup2(i32 %4, i32 %1)
%10 = load i32, i32* @F_SETFD
%11 = tail call i32 @fcntl(i32 %1, i32 %10, i32 0)
%12 = icmp eq i32 %1, 1
br i1 %12, label %13, label %17
13: ; preds = %8
%14 = tail call i32 @dup2(i32 %4, i32 2)
%15 = load i32, i32* @F_SETFD
%16 = tail call i32 @fcntl(i32 2, i32 %15, i32 0)
br label %17
17: ; preds = %13, %8
%18 = tail call i32 @close(i32 %4)
br label %19
19: ; preds = %3, %17
ret void
}