#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 */
scalar_t__ EBADF;
scalar_t__ EFAULT;
int /*<<< orphan*/ O_RDONLY;
int S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int test_fstat(void) {
  struct stat sb;
  int fd;
  fd = open("/tests", 0, O_RDONLY);
  assert(fstat(fd, &sb) == 0);
  assert(S_ISDIR(sb.st_mode));
  assert(fstat(fd, NULL) == -1 && errno == EFAULT);
  assert(fstat(666, &sb) == -1 && errno == EBADF);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<test_fstat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
lea    0xe98(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%edi
call   1030 <S_ISDIR@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <fstat@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0xd(%rbp)
jne    11d3 <test_fstat+0x83>
mov    0x2e10(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    $0x29a,%edi
lea    -0x8(%rbp),%rsi
call   1070 <fstat@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0xe(%rbp)
jne    1218 <test_fstat+0xc8>
mov    0x2dcb(%rip),%rax
mov    (%rax),%rax
mov    0x2db9(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fstat>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
lea    0xe97(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1070 <fstat@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
mov    %eax,%edi
call   1050 <assert@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
sete   %al
mov    0x2e1c(%rip),%r15
mov    (%r15),%rcx
mov    0x2e3a(%rip),%rdx
cmp    (%rdx),%rcx
sete   %cl
and    %al,%cl
movzbl %cl,%edi
call   1050 <assert@plt>
mov    $0x29a,%edi
mov    %r14,%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
sete   %al
mov    (%r15),%rcx
mov    0x2ddd(%rip),%rdx
cmp    (%rdx),%rcx
sete   %cl
and    %al,%cl
movzbl %cl,%edi
call   1050 <assert@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [7 x i8] c"/tests\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EFAULT = global i64 0
@EBADF = global i64 0
define i32 @test_fstat() {
%1 = alloca %struct.stat
%2 = alloca i32
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0, i32 %3)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @fstat(i32 %5, %struct.stat* %1)
%7 = icmp eq i32 %6, 0
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %1, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @S_ISDIR(i32 %11)
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* %2
%15 = call i32 @fstat(i32 %14, %struct.stat* null)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %0
%18 = load i64, i64* @errno
%19 = load i64, i64* @EFAULT
%20 = icmp eq i64 %18, %19
br label %21
21: ; preds = %17, %0
%22 = phi i1 [ false, %0 ], [ %20, %17 ]
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = call i32 @fstat(i32 666, %struct.stat* %1)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %21
%28 = load i64, i64* @errno
%29 = load i64, i64* @EBADF
%30 = icmp eq i64 %28, %29
br label %31
31: ; preds = %27, %21
%32 = phi i1 [ false, %21 ], [ %30, %27 ]
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [7 x i8] c"/tests\00"
@O_RDONLY = global i32 0
@errno = global i64 0
@EFAULT = global i64 0
@EBADF = global i64 0
define i32 @test_fstat() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 0, i32 %3)
%5 = call i32 @fstat(i32 %4, %struct.stat* %1)
%6 = icmp eq i32 %5, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @S_ISDIR(i32 %10)
%12 = tail call i32 @assert(i32 %11)
%13 = tail call i32 @fstat(i32 %4, %struct.stat* null)
%14 = icmp eq i32 %13, -1
%15 = load i64, i64* @errno
%16 = load i64, i64* @EFAULT
%17 = icmp eq i64 %15, %16
%18 = select i1 %14, i1 %17, i1 false
%19 = zext i1 %18 to i32
%20 = tail call i32 @assert(i32 %19)
%21 = call i32 @fstat(i32 666, %struct.stat* %1)
%22 = icmp eq i32 %21, -1
%23 = load i64, i64* @errno
%24 = load i64, i64* @EBADF
%25 = icmp eq i64 %23, %24
%26 = select i1 %22, i1 %25, i1 false
%27 = zext i1 %26 to i32
%28 = tail call i32 @assert(i32 %27)
%29 = tail call i32 @close(i32 %4)
ret i32 0
}