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

/* Variables and functions */
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(int);
int check_fd(int fd) {
  int rc;
  struct stat info;
  char buffer[4];
  if (fd < 0) {
    printf("[FAIL]: openat() failed, rc=%d errno=%d (%s)\n", fd, errno,
           strerror(errno));
    return 1;
  }
  if (fstat(fd, &info) != 0) {
    printf("[FAIL]: fstat() failed, rc=%d errno=%d (%s)\n", fd, errno,
           strerror(errno));
    return 1;
  }
  if (!S_ISDIR(info.st_mode)) {
    errno = 0;
    rc = read(fd, buffer, sizeof(buffer));
    if (rc < 0) {
      printf("[FAIL]: read() failed, rc=%d errno=%d (%s)\n", rc, errno,
             strerror(errno));
      return 1;
    }
  }
  close(fd);
  printf("[OK]\n");
  return 0;
}


x86 assembly (clang, -O0):
<check_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b8 <check_fd+0x58>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    %rax,%rcx
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <check_fd+0x152>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     1211 <check_fd+0xb1>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <check_fd+0x152>
mov    -0x10(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1295 <check_fd+0x135>
mov    0x2db7(%rip),%rax
movl   $0x0,(%rax)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1290 <check_fd+0x130>
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    %rax,%rcx
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <check_fd+0x152>
jmp    1295 <check_fd+0x135>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_fd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
test   %edi,%edi
js     11a8 <check_fd+0x38>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1090 <fstat@plt>
test   %rax,%rax
je     11d4 <check_fd+0x64>
mov    0x2e4a(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xe88(%rip),%rdi
jmp    11bf <check_fd+0x4f>
mov    0x2e31(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xe41(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
jmp    1219 <check_fd+0xa9>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
jne    1204 <check_fd+0x94>
mov    0x2df8(%rip),%r14
movl   $0x0,(%r14)
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1222 <check_fd+0xb2>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xe75(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %eax,%ebp
mov    (%r14),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xe26(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
jmp    11c3 <check_fd+0x53>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [46 x i8] c"[FAIL]: openat() failed, rc=%d errno=%d (%s)\0A\00"
@errno = global i32 0
@.str.1 = [45 x i8] c"[FAIL]: fstat() failed, rc=%d errno=%d (%s)\0A\00"
@.str.2 = [44 x i8] c"[FAIL]: read() failed, rc=%d errno=%d (%s)\0A\00"
@.str.3 = [6 x i8] c"[OK]\0A\00"
define i32 @check_fd(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca [4 x i8]
store i32 %0, i32* %3
%7 = load i32, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %1
%10 = load i32, i32* %3
%11 = load i32, i32* @errno
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11, i8* %13)
store i32 1, i32* %2
br label %47
15: ; preds = %1
%16 = load i32, i32* %3
%17 = call i64 @fstat(i32 %16, %struct.stat* %5)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %15
%20 = load i32, i32* %3
%21 = load i32, i32* @errno
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %21, i8* %23)
store i32 1, i32* %2
br label %47
25: ; preds = %15
%26 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @S_ISDIR(i32 %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %43, label %30
30: ; preds = %25
store i32 0, i32* @errno
%31 = load i32, i32* %3
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 4)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %30
%37 = load i32, i32* %4
%38 = load i32, i32* @errno
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %38, i8* %40)
store i32 1, i32* %2
br label %47
42: ; preds = %30
br label %43
43: ; preds = %42, %25
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %47
47: ; preds = %43, %36, %19, %9
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [46 x i8] c"[FAIL]: openat() failed, rc=%d errno=%d (%s)\0A\00"
@errno = global i32 0
@.str.1 = [45 x i8] c"[FAIL]: fstat() failed, rc=%d errno=%d (%s)\0A\00"
@.str.2 = [44 x i8] c"[FAIL]: read() failed, rc=%d errno=%d (%s)\0A\00"
@str = [5 x i8] c"[OK]\00"
define i32 @check_fd(i32 %0) {
%2 = alloca %struct.stat
%3 = alloca [4 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%6 = icmp slt i32 %0, 0
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %8, i8* %9)
br label %33
11: ; preds = %1
%12 = call i64 @fstat(i32 %0, %struct.stat* %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %15, i8* %16)
br label %33
18: ; preds = %11
%19 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i32 @S_ISDIR(i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %18
store i32 0, i32* @errno
%24 = call i32 @read(i32 %0, i8* %5, i32 4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %27, i8* %28)
br label %33
30: ; preds = %23, %18
%31 = tail call i32 @close(i32 %0)
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %33
33: ; preds = %30, %26, %14, %7
%34 = phi i32 [ 1, %7 ], [ 1, %14 ], [ 0, %30 ], [ 1, %26 ]
ret i32 %34
}