#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;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int dup(int);
int /*<<< orphan*/ *fdopen(int, char const *);
FILE *fddupopen(int fd, const char *mode) {
  int t;
  FILE *r = NULL;
  if (mode == NULL)
    return NULL;
  if ((t = dup(fd)) < 0)
    return NULL;
  if ((r = fdopen(t, mode)) == NULL)
    close(t);
  return r;
}


x86 assembly (clang, -O0):
<fddupopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    115f <fddupopen+0x2f>
movq   $0x0,-0x8(%rbp)
jmp    11aa <fddupopen+0x7a>
mov    -0xc(%rbp),%edi
call   1030 <dup@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1180 <fddupopen+0x50>
movq   $0x0,-0x8(%rbp)
jmp    11aa <fddupopen+0x7a>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a2 <fddupopen+0x72>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fddupopen>:
push   %rbp
push   %rbx
push   %rax
test   %rsi,%rsi
je     115c <fddupopen+0x2c>
mov    %rsi,%rbx
call   1030 <dup@plt>
test   %eax,%eax
js     115c <fddupopen+0x2c>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <fdopen@plt>
test   %rax,%rax
jne    115e <fddupopen+0x2e>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32* @fddupopen(i32 %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* null, i32** %7
%8 = load i8*, i8** %5
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i32* null, i32** %3
br label %26
11: ; preds = %2
%12 = load i32, i32* %4
%13 = call i32 @dup(i32 %12)
store i32 %13, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11
store i32* null, i32** %3
br label %26
16: ; preds = %11
%17 = load i32, i32* %6
%18 = load i8*, i8** %5
%19 = call i32* @fdopen(i32 %17, i8* %18)
store i32* %19, i32** %7
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* %6
%23 = call i32 @close(i32 %22)
br label %24
24: ; preds = %21, %16
%25 = load i32*, i32** %7
store i32* %25, i32** %3
br label %26
26: ; preds = %24, %15, %10
%27 = load i32*, i32** %3
ret i32* %27
}

LLVM IR (-O2):
define noalias i32* @fddupopen(i32 %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %12, label %4
4: ; preds = %2
%5 = tail call i32 @dup(i32 %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %4
%8 = tail call i32* @fdopen(i32 %5, i8* %1)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @close(i32 %5)
br label %12
12: ; preds = %7, %10, %4, %2
%13 = phi i32* [ null, %2 ], [ null, %4 ], [ null, %10 ], [ %8, %7 ]
ret i32* %13
}