#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 */
typedef int /*<<< orphan*/ uint32_t;
struct stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ assert(int);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int /*<<< orphan*/, int, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
uint32_t *map_pci_resource(int fd) {
  void *ptr;
  struct stat st;
  assert(fstat(fd, &st) == 0);
  ptr = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (ptr == MAP_FAILED) {
    return NULL;
  } else {
    return (uint32_t *)ptr;
  }
}


x86 assembly (clang, -O0):
<map_pci_resource>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x20(%rbp),%esi
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
mov    0x2e65(%rip),%rax
or     (%rax),%edx
mov    0x2e74(%rip),%rax
mov    (%rax),%ecx
mov    -0xc(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e33(%rip),%rcx
cmp    (%rcx),%rax
jne    11ab <map_pci_resource+0x7b>
movq   $0x0,-0x8(%rbp)
jmp    11b3 <map_pci_resource+0x83>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_pci_resource>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %rsp,%rsi
call   1050 <fstat@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1040 <assert@plt>
mov    (%rsp),%esi
mov    0x2ea2(%rip),%rax
mov    0x2e73(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%rax
cmove  %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32* @map_pci_resource(i32 %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i64 @fstat(i32 %6, %struct.stat* %5)
%8 = icmp eq i64 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %13, %14
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* %3
%18 = call i8* @mmap(i32* null, i32 %12, i32 %15, i32 %16, i32 %17, i32 0)
store i8* %18, i8** %4
%19 = load i8*, i8** %4
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %23
22: ; preds = %1
store i32* null, i32** %2
br label %26
23: ; preds = %1
%24 = load i8*, i8** %4
%25 = bitcast i8* %24 to i32*
store i32* %25, i32** %2
br label %26
26: ; preds = %23, %22
%27 = load i32*, i32** %2
ret i32* %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
define i32* @map_pci_resource(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @fstat(i32 %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_SHARED
%14 = tail call i8* @mmap(i32* null, i32 %9, i32 %12, i32 %13, i32 %0, i32 0)
%15 = load i8*, i8** @MAP_FAILED
%16 = icmp eq i8* %14, %15
%17 = bitcast i8* %14 to i32*
%18 = select i1 %16, i32* null, i32* %17
ret i32* %18
}