#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
scalar_t__ EBADF;
int /*<<< orphan*/ _exit(int);
scalar_t__ errno;
int fstat(int, struct stat *);
int open(char const *, int);
void fix_single_std_fd(int fd, const char *file, int flags) {
  struct stat s;
  if (fstat(fd, &s) == -1 && errno == EBADF) {
    int nfd = open(file, flags);
    if (nfd != fd || fstat(fd, &s) != 0)
      _exit(1);
  }
}


x86 assembly (clang, -O0):
<fix_single_std_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11b0 <fix_single_std_fd+0x80>
mov    0x2e82(%rip),%rax
mov    (%rax),%rax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%rax
jne    11b0 <fix_single_std_fd+0x80>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    11a1 <fix_single_std_fd+0x71>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0x0,%eax
je     11ab <fix_single_std_fd+0x7b>
mov    $0x1,%edi
call   1030 <_exit@plt>
jmp    11b0 <fix_single_std_fd+0x80>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fix_single_std_fd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1184 <fix_single_std_fd+0x54>
mov    0x2e8a(%rip),%rax
mov    (%rax),%rax
mov    0x2e78(%rip),%rcx
cmp    (%rcx),%rax
jne    1184 <fix_single_std_fd+0x54>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1040 <open@plt>
cmp    %ebx,%eax
jne    118d <fix_single_std_fd+0x5d>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1050 <fstat@plt>
test   %eax,%eax
jne    118d <fix_single_std_fd+0x5d>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EBADF = global i64 0
define void @fix_single_std_fd(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %4
%10 = call i32 @fstat(i32 %9, %struct.stat* %7)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %30
12: ; preds = %3
%13 = load i64, i64* @errno
%14 = load i64, i64* @EBADF
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %30
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = load i32, i32* %4
%22 = icmp ne i32 %20, %21
br i1 %22, label %27, label %23
23: ; preds = %16
%24 = load i32, i32* %4
%25 = call i32 @fstat(i32 %24, %struct.stat* %7)
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23, %16
%28 = call i32 @_exit(i32 1)
unreachable
29: ; preds = %23
br label %30
30: ; preds = %29, %12, %3
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EBADF = global i64 0
define void @fix_single_std_fd(i32 %0, i8* nocapture %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i32 @fstat(i32 %0, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %20
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @EBADF
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %20
12: ; preds = %8
%13 = tail call i32 @open(i8* %1, i32 %2)
%14 = icmp eq i32 %13, %0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @fstat(i32 %0, %struct.stat* %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15, %12
%19 = tail call i32 @_exit(i32 1)
unreachable
20: ; preds = %15, %8, %3
ret void
}