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

/* Variables and functions */
int /*<<< orphan*/ O_DIRECTORY;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
int fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int r, fd;
  struct stat buf;
  fd = open(".", O_DIRECTORY);
  if (fd == -1) {
    printf("Opening CWD returned error %d\n", errno);
    return -1;
  }
  r = fstat(fd, &buf);
  if (r == -1) {
    printf("fstat on directory fd returned error %d\n", errno);
    return -1;
  }
  close(fd);
  if (S_ISDIR(buf.st_mode))
    printf("fstat returned the fd type as S_IFDIR\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ab <main+0x5b>
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <main+0xc6>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <fstat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e7 <main+0x97>
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1216 <main+0xc6>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     120f <main+0xbf>
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11b7 <main+0x57>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     11c9 <main+0x69>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11e5 <main+0x85>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
jmp    11e5 <main+0x85>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    0xe3b(%rip),%rdi
jmp    11d9 <main+0x79>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@O_DIRECTORY = global i32 0
@.str.1 = [31 x i8] c"Opening CWD returned error %d\0A\00"
@errno = global i32 0
@.str.2 = [41 x i8] c"fstat on directory fd returned error %d\0A\00"
@.str.3 = [39 x i8] c"fstat returned the fd type as S_IFDIR\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_DIRECTORY
%10 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %14)
store i32 -1, i32* %3
br label %34
16: ; preds = %2
%17 = load i32, i32* %7
%18 = call i32 @fstat(i32 %17, %struct.stat* %8)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i32 -1, i32* %3
br label %34
24: ; preds = %16
%25 = load i32, i32* %7
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i64 @S_ISDIR(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %24
store i32 0, i32* %3
br label %34
34: ; preds = %33, %21, %13
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c".\00"
@O_DIRECTORY = global i32 0
@.str.1 = [31 x i8] c"Opening CWD returned error %d\0A\00"
@errno = global i32 0
@.str.2 = [41 x i8] c"fstat on directory fd returned error %d\0A\00"
@str = [38 x i8] c"fstat returned the fd type as S_IFDIR\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_DIRECTORY
%6 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %25
11: ; preds = %2
%12 = call i32 @fstat(i32 %6, %struct.stat* %3)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %15)
br label %25
17: ; preds = %11
%18 = tail call i32 @close(i32 %6)
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i64 @S_ISDIR(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %17
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %17, %23, %14, %8
%26 = phi i32 [ -1, %8 ], [ -1, %14 ], [ 0, %23 ], [ 0, %17 ]
ret i32 %26
}