#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 */
int /*<<< orphan*/ LOCK_UN;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ flock(int, int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
scalar_t__ munmap(void *, int /*<<< orphan*/);
void close_pci_resource(int fd, uint32_t *addr) {
  flock(fd, LOCK_UN);
  if (addr != NULL) {
    struct stat st;
    assert(fstat(fd, &st) == 0);
    assert(munmap((void *)addr, st.st_size) == 0);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<close_pci_resource>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
call   1060 <flock@plt>
cmpq   $0x0,-0x10(%rbp)
je     11b5 <close_pci_resource+0x65>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1050 <munmap@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<close_pci_resource>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
call   1060 <flock@plt>
test   %r14,%r14
je     119d <close_pci_resource+0x4d>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1070 <fstat@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1040 <assert@plt>
mov    (%rsp),%esi
mov    %r14,%rdi
call   1050 <munmap@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1040 <assert@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <close@plt>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@LOCK_UN = global i32 0
define void @close_pci_resource(i32 %0, i32* %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @LOCK_UN
%5 = tail call i32 @flock(i32 %0, i32 %4)
%6 = icmp eq i32* %1, null
br i1 %6, label %20, label %7
7: ; preds = %2
%8 = bitcast %struct.stat* %3 to i8*
%9 = call i64 @fstat(i32 %0, %struct.stat* %3)
%10 = icmp eq i64 %9, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = bitcast i32* %1 to i8*
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i64 @munmap(i8* %13, i32 %15)
%17 = icmp eq i64 %16, 0
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
br label %20
20: ; preds = %7, %2
%21 = tail call i32 @close(i32 %0)
ret void
}