#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 dup2(int /*<<< orphan*/, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char const *);
int file2fd(const char *path, const char *mode, int fd) {
  int ok = 0;
  FILE *file = NULL;
  file = fopen(path, mode);
  if (file != NULL && dup2(fileno(file), fd) != -1)
    ok = 1;
  if (file)
    fclose(file);
  return ok;
}


x86 assembly (clang, -O0):
<file2fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11a1 <file2fd+0x61>
mov    -0x20(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    -0x14(%rbp),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
je     11a1 <file2fd+0x61>
movl   $0x1,-0x18(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b5 <file2fd+0x75>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file2fd>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%ebp
call   1060 <fopen@plt>
test   %rax,%rax
je     1176 <file2fd+0x36>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    %ebp,%esi
call   1040 <dup2@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
setne  %bpl
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1178 <file2fd+0x38>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @file2fd(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* %1)
%5 = icmp eq i32* %4, null
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = tail call i32 @fileno(i32* %4)
%8 = tail call i32 @dup2(i32 %7, i32 %2)
%9 = icmp ne i32 %8, -1
%10 = zext i1 %9 to i32
%11 = tail call i32 @fclose(i32* %4)
br label %12
12: ; preds = %3, %6
%13 = phi i32 [ %10, %6 ], [ 0, %3 ]
ret i32 %13
}