#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;
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_byte(unsigned char bus, unsigned char dev, int func,
                           unsigned cmd, long val) {
  char path[100];
  int fd;
  sprintf(path, "/proc/bus/pci/%02d/%02x.0", bus, dev);
  fd = open(path, O_RDONLY | O_SYNC);
  if (fd > 0) {
    pwrite(fd, &val, 1, cmd);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<pci_config_write_byte>:
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)
lea    -0x80(%rbp),%rdi
mov    -0x1(%rbp),%al
lea    0xe90(%rip),%rsi
movzbl %al,%edx
movzbl -0x2(%rbp),%ecx
call   1060 <sprintf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jle    11cc <pci_config_write_byte+0x8c>
mov    -0x84(%rbp),%edi
mov    -0xc(%rbp),%ecx
lea    -0x18(%rbp),%rsi
mov    $0x1,%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_byte>:
push   %rbp
push   %rbx
sub    $0x78,%rsp
mov    %ecx,%ebx
mov    %esi,%ecx
mov    %edi,%edx
mov    %r8,0x8(%rsp)
lea    0xea8(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <sprintf@plt>
mov    0x2e74(%rip),%rax
mov    0x2e75(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1050 <open@plt>
test   %eax,%eax
jle    11a5 <pci_config_write_byte+0x65>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
mov    %ebx,%ecx
call   1040 <pwrite@plt>
mov    %ebp,%edi
add    $0x78,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <close@plt>
add    $0x78,%rsp
pop    %rbx
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_byte(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 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%14 = load i8, i8* %6
%15 = load i8, i8* %7
%16 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8 zeroext %14, i8 zeroext %15)
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%18 = load i32, i32* @O_RDONLY
%19 = load i32, i32* @O_SYNC
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %17, i32 %20)
store i32 %21, i32* %12
%22 = load i32, i32* %12
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %5
%25 = load i32, i32* %12
%26 = load i32, i32* %9
%27 = call i32 @pwrite(i32 %25, i64* %10, i32 1, i32 %26)
%28 = load i32, i32* %12
%29 = call i32 @close(i32 %28)
br label %30
30: ; preds = %24, %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_byte(i8 zeroext %0, i8 zeroext %1, i32 %2, i32 %3, i64 %4) {
%6 = alloca i64
%7 = alloca [100 x i8]
store i64 %4, i64* %6
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%9 = 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)
%10 = load i32, i32* @O_RDONLY
%11 = load i32, i32* @O_SYNC
%12 = or i32 %11, %10
%13 = call i32 @open(i8* %8, i32 %12)
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %5
%16 = call i32 @pwrite(i32 %13, i64* %6, i32 1, i32 %3)
%17 = tail call i32 @close(i32 %13)
br label %18
18: ; preds = %15, %5
ret void
}