#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 st_nlink;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fstat(int, struct stat *);
int /*<<< orphan*/ printf(char *, int, ...);
int check_fd_fine(FILE *fp) {
  struct stat _stat;
  int ret = -1;
  int fd = fileno(fp);
  if (fd > 1) {
    if (!fstat(fd, &_stat)) {
      if (_stat.st_nlink >= 1) {
        return 0;
      }
      { printf("file removed fd: %d st_nlink:%d\n", fd, _stat.st_nlink); };
      ;
    }
  }
  { printf("failure fd: %d\n", fd); };
  ;
  return ret;
}


x86 assembly (clang, -O0):
<check_fd_fine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jle    119d <check_fd_fine+0x6d>
mov    -0x20(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0x0,%eax
jne    1198 <check_fd_fine+0x68>
cmpl   $0x1,-0x18(%rbp)
jl     1184 <check_fd_fine+0x54>
movl   $0x0,-0x4(%rbp)
jmp    11b4 <check_fd_fine+0x84>
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    119d <check_fd_fine+0x6d>
mov    -0x20(%rbp),%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_fd_fine>:
push   %rbx
sub    $0x10,%rsp
call   1040 <fileno@plt>
mov    %eax,%ebx
cmp    $0x2,%eax
jl     1151 <check_fd_fine+0x21>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1050 <fstat@plt>
test   %eax,%eax
je     116c <check_fd_fine+0x3c>
lea    0xec9(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x8(%rsp),%edx
xor    %eax,%eax
test   %edx,%edx
jg     1166 <check_fd_fine+0x36>
lea    0xe83(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1151 <check_fd_fine+0x21>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [33 x i8] c"file removed fd: %d st_nlink:%d\0A\00"
@.str.1 = [16 x i8] c"failure fd: %d\0A\00"
define i32 @check_fd_fine(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 -1, i32* %5
%7 = load i32*, i32** %3
%8 = call i32 @fileno(i32* %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %26
11: ; preds = %1
%12 = load i32, i32* %6
%13 = call i32 @fstat(i32 %12, %struct.stat* %4)
%14 = icmp ne i32 %13, 0
br i1 %14, label %25, label %15
15: ; preds = %11
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = icmp sge i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 0, i32* %2
br label %30
20: ; preds = %15
%21 = load i32, i32* %6
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %21, i32 %23)
br label %25
25: ; preds = %20, %11
br label %26
26: ; preds = %25, %1
%27 = load i32, i32* %6
%28 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %5
store i32 %29, i32* %2
br label %30
30: ; preds = %26, %19
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [33 x i8] c"file removed fd: %d st_nlink:%d\0A\00"
@.str.1 = [16 x i8] c"failure fd: %d\0A\00"
define i32 @check_fd_fine(i32* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 @fileno(i32* %0)
%5 = icmp sgt i32 %4, 1
br i1 %5, label %6, label %15
6: ; preds = %1
%7 = call i32 @fstat(i32 %4, %struct.stat* %2)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = icmp sgt i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %9
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %11)
br label %15
15: ; preds = %6, %13, %1
%16 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %4)
br label %17
17: ; preds = %9, %15
%18 = phi i32 [ -1, %15 ], [ 0, %9 ]
ret i32 %18
}