#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;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ assert(int);
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int);
int /*<<< orphan*/ stderr;
int write(int, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  char name[1000];
  uint32_t buf[(128 / sizeof(uint32_t))];
  int fd;
  if (argc != 4) {
    fprintf(stderr, "Usage: csave <bus> <device> <function>\n");
    return 1;
  }
  sprintf(name, "/sys/bus/pci/devices/0000:%02d:%02d.%d/config", atoi(argv[1]),
          atoi(argv[2]), atoi(argv[3]));
  assert((fd = open(name, O_RDONLY)) != -1);
  assert(read(fd, buf, 128) == 128);
  assert(write(1, buf, 128) == 128);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4a0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11b4 <main+0x44>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    129a <main+0x12a>
lea    -0x400(%rbp),%rax
mov    %rax,-0x498(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x48c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x488(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    -0x498(%rbp),%rdi
mov    -0x48c(%rbp),%edx
mov    -0x488(%rbp),%ecx
mov    %eax,%r8d
lea    0xe16(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x400(%rbp),%rdi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x484(%rbp)
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x484(%rbp),%edi
lea    -0x480(%rbp),%rsi
mov    $0x80,%edx
call   1040 <read@plt>
cmp    $0x80,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
lea    -0x480(%rbp),%rsi
mov    $0x1,%edi
mov    $0x80,%edx
call   1030 <write@plt>
cmp    $0x80,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x470,%rsp
cmp    $0x4,%edi
jne    122f <main+0xbf>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
mov    0x18(%rbx),%rdi
call   1080 <atoi@plt>
lea    0xe7a(%rip),%rsi
lea    0x80(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14d,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
call   1090 <sprintf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1050 <assert@plt>
mov    %rsp,%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1040 <read@plt>
xor    %edi,%edi
cmp    $0x80,%eax
sete   %dil
call   1050 <assert@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    $0x80,%edx
call   1030 <write@plt>
xor    %edi,%edi
cmp    $0x80,%eax
sete   %dil
call   1050 <assert@plt>
xor    %eax,%eax
jmp    1249 <main+0xd9>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x470,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: csave <bus> <device> <function>\0A\00"
@.str.1 = [46 x i8] c"/sys/bus/pci/devices/0000:%02d:%02d.%d/config\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1000 x i8]
%7 = alloca [32 x i32]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 4
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %46
14: ; preds = %2
%15 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 3
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
%28 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %19, i32 %23, i32 %27)
%29 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %8
%32 = icmp ne i32 %31, -1
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i32, i32* %8
%36 = getelementptr inbounds [32 x i32], [32 x i32]* %7, i64 0, i64 0
%37 = call i32 @read(i32 %35, i32* %36, i32 128)
%38 = icmp eq i32 %37, 128
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = getelementptr inbounds [32 x i32], [32 x i32]* %7, i64 0, i64 0
%42 = call i32 @write(i32 1, i32* %41, i32 128)
%43 = icmp eq i32 %42, 128
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
store i32 0, i32* %3
br label %46
46: ; preds = %14, %11
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Usage: csave <bus> <device> <function>\0A\00"
@.str.1 = [46 x i8] c"/sys/bus/pci/devices/0000:%02d:%02d.%d/config\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1000 x i8]
%4 = alloca [32 x i32]
%5 = getelementptr inbounds [1000 x i8], [1000 x i8]* %3, i64 0, i64 0
%6 = bitcast [32 x i32]* %4 to i8*
%7 = icmp eq i32 %0, 4
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %36
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = getelementptr inbounds i8*, i8** %1, i64 3
%19 = load i8*, i8** %18
%20 = tail call i32 @atoi(i8* %19)
%21 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %14, i32 %17, i32 %20)
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %5, i32 %22)
%24 = icmp ne i32 %23, -1
%25 = zext i1 %24 to i32
%26 = tail call i32 @assert(i32 %25)
%27 = getelementptr inbounds [32 x i32], [32 x i32]* %4, i64 0, i64 0
%28 = call i32 @read(i32 %23, i32* %27, i32 128)
%29 = icmp eq i32 %28, 128
%30 = zext i1 %29 to i32
%31 = tail call i32 @assert(i32 %30)
%32 = call i32 @write(i32 1, i32* %27, i32 128)
%33 = icmp eq i32 %32, 128
%34 = zext i1 %33 to i32
%35 = tail call i32 @assert(i32 %34)
br label %36
36: ; preds = %11, %8
%37 = phi i32 [ 1, %8 ], [ 0, %11 ]
ret i32 %37
}