#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*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int _redirect_fd(int nfd, int ofd) {
  int res = 0;
  switch (nfd) {
  case (-1):
    if (close(ofd) < 0)
      res = 22;
    break;
  case (-2):
    break;
  default:
    assert(nfd >= 0);
    if (close(ofd) < 0) {
      res = 22;
      break;
    }
    if (dup2(nfd, ofd) < 0)
      res = 22;
    break;
  }
  return res;
}


x86 assembly (clang, -O0):
<_redirect_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0xfffffffe,%eax
je     1187 <_redirect_fd+0x57>
jmp    1159 <_redirect_fd+0x29>
mov    -0x10(%rbp),%eax
sub    $0xffffffff,%eax
jne    118c <_redirect_fd+0x5c>
jmp    116a <_redirect_fd+0x3a>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%eax
jge    1182 <_redirect_fd+0x52>
movl   $0x16,-0xc(%rbp)
jmp    11da <_redirect_fd+0xaa>
jmp    11da <_redirect_fd+0xaa>
cmpl   $0x0,-0x4(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%eax
jge    11ba <_redirect_fd+0x8a>
movl   $0x16,-0xc(%rbp)
jmp    11da <_redirect_fd+0xaa>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0x0,%eax
jge    11d5 <_redirect_fd+0xa5>
movl   $0x16,-0xc(%rbp)
jmp    11da <_redirect_fd+0xaa>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_redirect_fd>:
push   %rbp
push   %rbx
push   %rax
cmp    $0xfffffffe,%edi
je     114a <_redirect_fd+0x1a>
mov    %esi,%ebx
mov    %edi,%ebp
cmp    $0xffffffff,%edi
jne    114e <_redirect_fd+0x1e>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    116e <_redirect_fd+0x3e>
xor    %eax,%eax
jmp    1174 <_redirect_fd+0x44>
mov    %ebp,%edi
not    %edi
shr    $0x1f,%edi
call   1050 <assert@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %eax,%eax
js     117b <_redirect_fd+0x4b>
mov    %ebp,%edi
mov    %ebx,%esi
call   1030 <dup2@plt>
sar    $0x1f,%eax
and    $0x16,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x16,%eax
jmp    1174 <_redirect_fd+0x44>

LLVM IR (-O0):
define i32 @_redirect_fd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
%6 = load i32, i32* %3
switch i32 %6, label %14 [
i32 -1, label %7
i32 -2, label %13
]
7: ; preds = %2
%8 = load i32, i32* %4
%9 = call i32 @close(i32 %8)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %7
store i32 22, i32* %5
br label %12
12: ; preds = %11, %7
br label %30
13: ; preds = %2
br label %30
14: ; preds = %2
%15 = load i32, i32* %3
%16 = icmp sge i32 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %14
store i32 22, i32* %5
br label %30
23: ; preds = %14
%24 = load i32, i32* %3
%25 = load i32, i32* %4
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 22, i32* %5
br label %29
29: ; preds = %28, %23
br label %30
30: ; preds = %29, %22, %13, %12
%31 = load i32, i32* %5
ret i32 %31
}

LLVM IR (-O2):
define i32 @_redirect_fd(i32 %0, i32 %1) {
switch i32 %0, label %7 [
i32 -1, label %3
i32 -2, label %17
]
3: ; preds = %2
%4 = tail call i32 @close(i32 %1)
%5 = icmp slt i32 %4, 0
%6 = select i1 %5, i32 22, i32 0
br label %17
7: ; preds = %2
%8 = xor i32 %0, -1
%9 = lshr i32 %8, 31
%10 = tail call i32 @assert(i32 %9)
%11 = tail call i32 @close(i32 %1)
%12 = icmp slt i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %7
%14 = tail call i32 @dup2(i32 %0, i32 %1)
%15 = icmp slt i32 %14, 0
%16 = select i1 %15, i32 22, i32 0
br label %17
17: ; preds = %13, %3, %7, %2
%18 = phi i32 [ 0, %2 ], [ %6, %3 ], [ 22, %7 ], [ %16, %13 ]
ret i32 %18
}