#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 */
struct pci_device {
  int domain;
  int bus;
  int dev;
  int func;
};
struct flock {
  int /*<<< orphan*/ l_len;
  int /*<<< orphan*/ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
scalar_t__ ftruncate(int, int);
scalar_t__ getpid();
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, int);
int shm_open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int, int);
int /*<<< orphan*/ stderr;
int pci_device_claim(struct pci_device *dev) {
  int dev_fd;
  char shm_name[64];
  int pid;
  void *dev_map;
  struct flock pcidev_lock = {
      .l_type = F_WRLCK,
      .l_whence = SEEK_SET,
      .l_start = 0,
      .l_len = 0,
  };
  sprintf(shm_name, "%04x:%02x:%02x.%1u", dev->domain, dev->bus, dev->dev,
          dev->func);
  dev_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
  if (dev_fd == -1) {
    fprintf(stderr, "could not shm_open %s\n", shm_name);
    return -1;
  }
  if (ftruncate(dev_fd, sizeof(int)) != 0) {
    fprintf(stderr, "could not truncate shm %s\n", shm_name);
    close(dev_fd);
    return -1;
  }
  dev_map =
      mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
  if (dev_map == NULL) {
    fprintf(stderr, "could not mmap shm %s\n", shm_name);
    close(dev_fd);
    return -1;
  }
  if (fcntl(dev_fd, F_SETLK, &pcidev_lock) != 0) {
    pid = *(int *)dev_map;
    fprintf(stderr,
            "Cannot create lock on device %s, probably"
            " process %d has claimed it\n",
            shm_name, pid);
    munmap(dev_map, sizeof(int));
    close(dev_fd);
    return -1;
  }
  *(int *)dev_map = (int)getpid();
  munmap(dev_map, sizeof(int));
  return 0;
}


x86 assembly (clang, -O0):
<pci_device_claim>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x80(%rbp)
movl   $0x0,-0x7c(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8d
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%r9d
lea    0xe13(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x60(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   10b0 <shm_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1246 <pci_device_claim+0xb6>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1386 <pci_device_claim+0x1f6>
mov    -0x14(%rbp),%edi
mov    $0x4,%esi
call   1060 <ftruncate@plt>
cmp    $0x0,%rax
je     128c <pci_device_claim+0xfc>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    0xdb9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1386 <pci_device_claim+0x1f6>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
mov    0x2d0c(%rip),%rax
or     (%rax),%edx
mov    0x2d3b(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    12fa <pci_device_claim+0x16a>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
lea    0xd66(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1386 <pci_device_claim+0x1f6>
mov    -0x14(%rbp),%edi
mov    0x2cc4(%rip),%rax
mov    (%rax),%esi
lea    -0x80(%rbp),%rdx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
je     1362 <pci_device_claim+0x1d2>
mov    -0x70(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rdx
mov    -0x64(%rbp),%ecx
lea    0xd23(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x70(%rbp),%rdi
mov    $0x4,%esi
call   1090 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1386 <pci_device_claim+0x1f6>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%ecx
mov    -0x70(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x70(%rbp),%rdi
mov    $0x4,%esi
call   1090 <munmap@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_device_claim>:
push   %r14
push   %rbx
sub    $0x58,%rsp
movq   $0x0,(%rsp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    (%rdi),%edx
mov    0x4(%rdi),%ecx
mov    0x8(%rdi),%r8d
mov    0xc(%rdi),%r9d
lea    0xe33(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   10a0 <sprintf@plt>
mov    0x2def(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x180,%edx
call   10b0 <shm_open@plt>
cmp    $0xffffffff,%eax
je     1236 <pci_device_claim+0xa6>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x4,%esi
call   1060 <ftruncate@plt>
test   %rax,%rax
je     1261 <pci_device_claim+0xd1>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1252 <pci_device_claim+0xc2>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d88(%rip),%rax
mov    0x2d39(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d66(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     12df <pci_device_claim+0x14f>
mov    %rax,%r14
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
je     12f4 <pci_device_claim+0x164>
mov    (%r14),%ecx
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
lea    0xd9b(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %r14,%rdi
mov    $0x4,%esi
call   1090 <munmap@plt>
jmp    122d <pci_device_claim+0x9d>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
jmp    1221 <pci_device_claim+0x91>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,(%r14)
mov    %r14,%rdi
mov    $0x4,%esi
call   1090 <munmap@plt>
jmp    1257 <pci_device_claim+0xc7>

LLVM IR (-O0):
%struct.pci_device = type { i32, i32, i32, i32 }
%struct.flock = type { i32, i32, i32, i32 }
@SEEK_SET = global i32 0
@F_WRLCK = global i32 0
@.str = [19 x i8] c"%04x:%02x:%02x.%1u\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"could not shm_open %s\0A\00"
@.str.2 = [27 x i8] c"could not truncate shm %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [23 x i8] c"could not mmap shm %s\0A\00"
@F_SETLK = global i32 0
@.str.4 = [69 x i8] c"Cannot create lock on device %s, probably process %d has claimed it\0A\00"
define i32 @pci_device_claim(%struct.pci_device* %0) {
%2 = alloca i32
%3 = alloca %struct.pci_device*
%4 = alloca i32
%5 = alloca [64 x i8]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.flock
store %struct.pci_device* %0, %struct.pci_device** %3
%9 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 0
store i32 0, i32* %9
%10 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 1
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 2
%12 = load i32, i32* @SEEK_SET
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.flock, %struct.flock* %8, i32 0, i32 3
%14 = load i32, i32* @F_WRLCK
store i32 %14, i32* %13
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%16 = load %struct.pci_device*, %struct.pci_device** %3
%17 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load %struct.pci_device*, %struct.pci_device** %3
%20 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %19, i32 0, i32 1
%21 = load i32, i32* %20
%22 = load %struct.pci_device*, %struct.pci_device** %3
%23 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %22, i32 0, i32 2
%24 = load i32, i32* %23
%25 = load %struct.pci_device*, %struct.pci_device** %3
%26 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %25, i32 0, i32 3
%27 = load i32, i32* %26
%28 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %21, i32 %24, i32 %27)
%29 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = call i32 @shm_open(i8* %29, i32 %32, i32 384)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %1
%37 = load i32, i32* @stderr
%38 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %38)
store i32 -1, i32* %2
br label %89
40: ; preds = %1
%41 = load i32, i32* %4
%42 = call i64 @ftruncate(i32 %41, i32 4)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i32, i32* @stderr
%46 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%47 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %4
%49 = call i32 @close(i32 %48)
store i32 -1, i32* %2
br label %89
50: ; preds = %40
%51 = load i32, i32* @PROT_READ
%52 = load i32, i32* @PROT_WRITE
%53 = or i32 %51, %52
%54 = load i32, i32* @MAP_SHARED
%55 = load i32, i32* %4
%56 = call i8* @mmap(i32* null, i32 4, i32 %53, i32 %54, i32 %55, i32 0)
store i8* %56, i8** %7
%57 = load i8*, i8** %7
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %65
59: ; preds = %50
%60 = load i32, i32* @stderr
%61 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%62 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %61)
%63 = load i32, i32* %4
%64 = call i32 @close(i32 %63)
store i32 -1, i32* %2
br label %89
65: ; preds = %50
%66 = load i32, i32* %4
%67 = load i32, i32* @F_SETLK
%68 = call i64 @fcntl(i32 %66, i32 %67, %struct.flock* %8)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %82
70: ; preds = %65
%71 = load i8*, i8** %7
%72 = bitcast i8* %71 to i32*
%73 = load i32, i32* %72
store i32 %73, i32* %6
%74 = load i32, i32* @stderr
%75 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%76 = load i32, i32* %6
%77 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.4, i64 0, i64 0), i8* %75, i32 %76)
%78 = load i8*, i8** %7
%79 = call i32 @munmap(i8* %78, i32 4)
%80 = load i32, i32* %4
%81 = call i32 @close(i32 %80)
store i32 -1, i32* %2
br label %89
82: ; preds = %65
%83 = call i64 (...) @getpid()
%84 = trunc i64 %83 to i32
%85 = load i8*, i8** %7
%86 = bitcast i8* %85 to i32*
store i32 %84, i32* %86
%87 = load i8*, i8** %7
%88 = call i32 @munmap(i8* %87, i32 4)
store i32 0, i32* %2
br label %89
89: ; preds = %82, %70, %59, %44, %36
%90 = load i32, i32* %2
ret i32 %90
}

LLVM IR (-O2):
%struct.pci_device = type { i32, i32, i32, i32 }
%struct.flock = type { i32, i32, i32, i32 }
@SEEK_SET = global i32 0
@F_WRLCK = global i32 0
@.str = [19 x i8] c"%04x:%02x:%02x.%1u\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"could not shm_open %s\0A\00"
@.str.2 = [27 x i8] c"could not truncate shm %s\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.3 = [23 x i8] c"could not mmap shm %s\0A\00"
@F_SETLK = global i32 0
@.str.4 = [69 x i8] c"Cannot create lock on device %s, probably process %d has claimed it\0A\00"
define i32 @pci_device_claim(%struct.pci_device* nocapture %0) {
%2 = alloca [64 x i8]
%3 = alloca %struct.flock
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.flock* %3 to i8*
%6 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 0, i32* %6
%7 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 2
%9 = load i32, i32* @SEEK_SET
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 3
%11 = load i32, i32* @F_WRLCK
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 0
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 1
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 2
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.pci_device, %struct.pci_device* %0, i64 0, i32 3
%19 = load i32, i32* %18
%20 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %15, i32 %17, i32 %19)
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %22, %21
%24 = call i32 @shm_open(i8* %4, i32 %23, i32 384)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %1
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %63
29: ; preds = %1
%30 = call i64 @ftruncate(i32 %24, i32 4)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%35 = call i32 @close(i32 %24)
br label %63
36: ; preds = %29
%37 = load i32, i32* @PROT_READ
%38 = load i32, i32* @PROT_WRITE
%39 = or i32 %38, %37
%40 = load i32, i32* @MAP_SHARED
%41 = call i8* @mmap(i32* null, i32 4, i32 %39, i32 %40, i32 %24, i32 0)
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %47
43: ; preds = %36
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%46 = call i32 @close(i32 %24)
br label %63
47: ; preds = %36
%48 = load i32, i32* @F_SETLK
%49 = call i64 @fcntl(i32 %24, i32 %48, %struct.flock* %3)
%50 = icmp eq i64 %49, 0
br i1 %50, label %58, label %51
51: ; preds = %47
%52 = bitcast i8* %41 to i32*
%53 = load i32, i32* %52
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.4, i64 0, i64 0), i8* %4, i32 %53)
%56 = call i32 @munmap(i8* %41, i32 4)
%57 = call i32 @close(i32 %24)
br label %63
58: ; preds = %47
%59 = call i64 (...) @getpid()
%60 = trunc i64 %59 to i32
%61 = bitcast i8* %41 to i32*
store i32 %60, i32* %61
%62 = call i32 @munmap(i8* %41, i32 4)
br label %63
63: ; preds = %58, %51, %43, %32, %26
%64 = phi i32 [ -1, %26 ], [ -1, %32 ], [ -1, %43 ], [ -1, %51 ], [ 0, %58 ]
ret i32 %64
}