#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 */

/* Variables and functions */
int O_RDONLY;
int O_SYNC;
long bswap_16(long);
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ pwrite(int, long *, int, unsigned int);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char, unsigned char);
void pci_config_write_word(unsigned char bus, unsigned char dev, int func,
                           unsigned cmd, long val) {
  char path[100];
  int fd;
  val = bswap_16(val);
  sprintf(path, "/proc/bus/pci/%02d/%02x.0", bus, dev);
  fd = open(path, O_RDONLY | O_SYNC);
  if (fd > 0) {
    pwrite(fd, &val, 2, cmd);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<pci_config_write_word>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %sil,%al
mov    %dil,%sil
mov    %sil,-0x1(%rbp)
mov    %al,-0x2(%rbp)
mov    %edx,-0x8(%rbp)
mov    %ecx,-0xc(%rbp)
mov    %r8,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <bswap_16@plt>
mov    %rax,-0x18(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x1(%rbp),%al
lea    0xe73(%rip),%rsi
movzbl %al,%edx
movzbl -0x2(%rbp),%ecx
call   1070 <sprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e3b(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jle    11e9 <pci_config_write_word+0x99>
mov    -0x84(%rbp),%edi
mov    -0xc(%rbp),%ecx
lea    -0x18(%rbp),%rsi
mov    $0x2,%edx
call   1040 <pwrite@plt>
mov    -0x84(%rbp),%edi
call   1030 <close@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_config_write_word>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %ecx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
mov    %r8,%rdi
call   1060 <bswap_16@plt>
mov    %rax,0x8(%rsp)
lea    0xe8b(%rip),%rsi
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1070 <sprintf@plt>
mov    0x2e53(%rip),%rax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
call   1050 <open@plt>
test   %eax,%eax
jle    11cb <pci_config_write_word+0x7b>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x2,%edx
mov    %r14d,%ecx
call   1040 <pwrite@plt>
mov    %ebp,%edi
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <close@plt>
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"/proc/bus/pci/%02d/%02x.0\00"
@O_RDONLY = global i32 0
@O_SYNC = global i32 0
define void @pci_config_write_word(i8 zeroext %0, i8 zeroext %1, i32 %2, i32 %3, i64 %4) {
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca [100 x i8]
%12 = alloca i32
store i8 %0, i8* %6
store i8 %1, i8* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i64 %4, i64* %10
%13 = load i64, i64* %10
%14 = call i64 @bswap_16(i64 %13)
store i64 %14, i64* %10
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%16 = load i8, i8* %6
%17 = load i8, i8* %7
%18 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8 zeroext %16, i8 zeroext %17)
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%20 = load i32, i32* @O_RDONLY
%21 = load i32, i32* @O_SYNC
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %19, i32 %22)
store i32 %23, i32* %12
%24 = load i32, i32* %12
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %5
%27 = load i32, i32* %12
%28 = load i32, i32* %9
%29 = call i32 @pwrite(i32 %27, i64* %10, i32 2, i32 %28)
%30 = load i32, i32* %12
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %26, %5
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"/proc/bus/pci/%02d/%02x.0\00"
@O_RDONLY = global i32 0
@O_SYNC = global i32 0
define void @pci_config_write_word(i8 zeroext %0, i8 zeroext %1, i32 %2, i32 %3, i64 %4) {
%6 = alloca i64
%7 = alloca [100 x i8]
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%9 = tail call i64 @bswap_16(i64 %4)
store i64 %9, i64* %6
%10 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8 zeroext %0, i8 zeroext %1)
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_SYNC
%13 = or i32 %12, %11
%14 = call i32 @open(i8* %8, i32 %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %5
%17 = call i32 @pwrite(i32 %14, i64* %6, i32 2, i32 %3)
%18 = tail call i32 @close(i32 %14)
br label %19
19: ; preds = %16, %5
ret void
}