#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 uint32_t;
struct pci_device {
  int dummy;
};

/* Variables and functions */
int pci_device_cfg_read_u32(struct pci_device *, int *, int);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int pci_device_get_serial_number(struct pci_device *dev, char *sn, int len) {
  int err;
  uint32_t pos, header = 0;
  uint32_t i, buf[2];
  if (len < 17)
    return -1;
  err = pci_device_cfg_read_u32(dev, &header, 256);
  if (err || !header)
    return -1;
  pos = 256;
  while (1) {
    if ((header & 0x0000ffff) == 0x03) {
      if (pos) {
        pos += 4;
        for (i = 0; i < 2; i++) {
          err = pci_device_cfg_read_u32(dev, &buf[i], pos + 4 * i);
          if (err)
            return -1;
        }
        sprintf(sn, "%08x%08x", buf[1], buf[0]);
        return 0;
      }
    }
    pos = (header >> 20) & 0xffc;
    if (pos < 256)
      return -1;
    err = pci_device_cfg_read_u32(dev, &header, pos);
    if (err)
      return -1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<pci_device_get_serial_number>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x11,-0x1c(%rbp)
jge    1150 <pci_device_get_serial_number+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <pci_device_get_serial_number+0x16c>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x100,%edx
call   1030 <pci_device_cfg_read_u32@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1179 <pci_device_get_serial_number+0x59>
cmpl   $0x0,-0x28(%rbp)
jne    1185 <pci_device_get_serial_number+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <pci_device_get_serial_number+0x16c>
movl   $0x100,-0x24(%rbp)
mov    -0x28(%rbp),%eax
and    $0xffff,%eax
cmp    $0x3,%eax
jne    1237 <pci_device_get_serial_number+0x117>
cmpl   $0x0,-0x24(%rbp)
je     1232 <pci_device_get_serial_number+0x112>
mov    -0x24(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x2,-0x2c(%rbp)
jge    1210 <pci_device_get_serial_number+0xf0>
mov    -0x10(%rbp),%rdi
movslq -0x2c(%rbp),%rax
lea    -0x34(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
mov    -0x2c(%rbp),%eax
shl    $0x2,%eax
add    %eax,%edx
call   1030 <pci_device_cfg_read_u32@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11fd <pci_device_get_serial_number+0xdd>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <pci_device_get_serial_number+0x16c>
jmp    1202 <pci_device_get_serial_number+0xe2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b7 <pci_device_get_serial_number+0x97>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x34(%rbp),%ecx
lea    0xddf(%rip),%rsi
call   1040 <sprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    128c <pci_device_get_serial_number+0x16c>
jmp    1237 <pci_device_get_serial_number+0x117>
mov    -0x28(%rbp),%eax
sar    $0x14,%eax
and    $0xffc,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x100,-0x24(%rbp)
jge    125e <pci_device_get_serial_number+0x13e>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <pci_device_get_serial_number+0x16c>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    -0x28(%rbp),%rsi
call   1030 <pci_device_cfg_read_u32@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1287 <pci_device_get_serial_number+0x167>
movl   $0xffffffff,-0x4(%rbp)
jmp    128c <pci_device_get_serial_number+0x16c>
jmp    118c <pci_device_get_serial_number+0x6c>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pci_device_get_serial_number>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0x4(%rsp)
mov    $0xffffffff,%r15d
cmp    $0x11,%edx
jl     11f1 <pci_device_get_serial_number+0xd1>
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x4(%rsp),%rsi
mov    $0x100,%edx
call   1030 <pci_device_cfg_read_u32@plt>
test   %eax,%eax
jne    11f1 <pci_device_get_serial_number+0xd1>
mov    0x4(%rsp),%eax
test   %eax,%eax
je     11f1 <pci_device_get_serial_number+0xd1>
mov    $0x104,%ebp
cmp    $0x3,%ax
je     11ae <pci_device_get_serial_number+0x8e>
lea    0x4(%rsp),%r12
nopl   0x0(%rax)
shr    $0x14,%eax
mov    %eax,%ebp
and    $0xfffffffc,%ebp
cmp    $0x100,%ebp
jb     11f1 <pci_device_get_serial_number+0xd1>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <pci_device_cfg_read_u32@plt>
test   %eax,%eax
jne    11f1 <pci_device_get_serial_number+0xd1>
mov    0x4(%rsp),%eax
cmp    $0x3,%ax
jne    1180 <pci_device_get_serial_number+0x60>
add    $0x4,%ebp
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1030 <pci_device_cfg_read_u32@plt>
test   %eax,%eax
jne    11f1 <pci_device_get_serial_number+0xd1>
lea    0xc(%rsp),%rsi
add    $0x4,%ebp
mov    %rbx,%rdi
mov    %ebp,%edx
call   1030 <pci_device_cfg_read_u32@plt>
test   %eax,%eax
jne    11f1 <pci_device_get_serial_number+0xd1>
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%edx
lea    0xe1a(%rip),%rsi
mov    %r14,%rdi
call   1040 <sprintf@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pci_device = type { i32 }
@.str = [9 x i8] c"%08x%08x\00"
define i32 @pci_device_get_serial_number(%struct.pci_device* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.pci_device*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [2 x i32]
store %struct.pci_device* %0, %struct.pci_device** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %10
%13 = load i32, i32* %7
%14 = icmp slt i32 %13, 17
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %79
16: ; preds = %3
%17 = load %struct.pci_device*, %struct.pci_device** %5
%18 = call i32 @pci_device_cfg_read_u32(%struct.pci_device* %17, i32* %10, i32 256)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp ne i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* %10
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %21, %16
store i32 -1, i32* %4
br label %79
25: ; preds = %21
store i32 256, i32* %9
br label %26
26: ; preds = %25, %78
%27 = load i32, i32* %10
%28 = and i32 %27, 65535
%29 = icmp eq i32 %28, 3
br i1 %29, label %30, label %64
30: ; preds = %26
%31 = load i32, i32* %9
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %63
33: ; preds = %30
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 4
store i32 %35, i32* %9
store i32 0, i32* %11
br label %36
36: ; preds = %53, %33
%37 = load i32, i32* %11
%38 = icmp slt i32 %37, 2
br i1 %38, label %39, label %56
39: ; preds = %36
%40 = load %struct.pci_device*, %struct.pci_device** %5
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 %42
%44 = load i32, i32* %9
%45 = load i32, i32* %11
%46 = mul nsw i32 4, %45
%47 = add nsw i32 %44, %46
%48 = call i32 @pci_device_cfg_read_u32(%struct.pci_device* %40, i32* %43, i32 %47)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %39
store i32 -1, i32* %4
br label %79
52: ; preds = %39
br label %53
53: ; preds = %52
%54 = load i32, i32* %11
%55 = add nsw i32 %54, 1
store i32 %55, i32* %11
br label %36
56: ; preds = %36
%57 = load i8*, i8** %6
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 1
%59 = load i32, i32* %58
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %12, i64 0, i64 0
%61 = load i32, i32* %60
%62 = call i32 @sprintf(i8* %57, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %59, i32 %61)
store i32 0, i32* %4
br label %79
63: ; preds = %30
br label %64
64: ; preds = %63, %26
%65 = load i32, i32* %10
%66 = ashr i32 %65, 20
%67 = and i32 %66, 4092
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = icmp slt i32 %68, 256
br i1 %69, label %70, label %71
70: ; preds = %64
store i32 -1, i32* %4
br label %79
71: ; preds = %64
%72 = load %struct.pci_device*, %struct.pci_device** %5
%73 = load i32, i32* %9
%74 = call i32 @pci_device_cfg_read_u32(%struct.pci_device* %72, i32* %10, i32 %73)
store i32 %74, i32* %8
%75 = load i32, i32* %8
%76 = icmp ne i32 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %71
store i32 -1, i32* %4
br label %79
78: ; preds = %71
br label %26
79: ; preds = %77, %70, %56, %51, %24, %15
%80 = load i32, i32* %4
ret i32 %80
}

LLVM IR (-O2):
%struct.pci_device = type { i32 }
@.str = [9 x i8] c"%08x%08x\00"
define i32 @pci_device_get_serial_number(%struct.pci_device* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%7 = bitcast [2 x i32]* %5 to i8*
%8 = icmp slt i32 %2, 17
br i1 %8, label %46, label %9
9: ; preds = %3
%10 = call i32 @pci_device_cfg_read_u32(%struct.pci_device* %0, i32* %4, i32 256)
%11 = icmp eq i32 %10, 0
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 0
%14 = select i1 %11, i1 %13, i1 false
br i1 %14, label %15, label %46
15: ; preds = %9
%16 = and i32 %12, 65535
%17 = icmp eq i32 %16, 3
br i1 %17, label %24, label %38
18: ; preds = %43
%19 = load i32, i32* %4
%20 = and i32 %19, 65535
%21 = icmp eq i32 %20, 3
br i1 %21, label %22, label %38
22: ; preds = %18
%23 = add nuw nsw i32 %41, 4
br label %24
24: ; preds = %22, %15
%25 = phi i32 [ 260, %15 ], [ %23, %22 ]
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%27 = call i32 @pci_device_cfg_read_u32(%struct.pci_device* %0, i32* %26, i32 %25)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %46
29: ; preds = %24
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%31 = add nuw nsw i32 %25, 4
%32 = call i32 @pci_device_cfg_read_u32(%struct.pci_device* %0, i32* %30, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %46
34: ; preds = %29
%35 = load i32, i32* %30
%36 = load i32, i32* %26
%37 = call i32 @sprintf(i8* dereferenceable(1) %1, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %35, i32 %36)
br label %46
38: ; preds = %15, %18
%39 = phi i32 [ %19, %18 ], [ %12, %15 ]
%40 = lshr i32 %39, 20
%41 = and i32 %40, 4092
%42 = icmp ult i32 %41, 256
br i1 %42, label %46, label %43
43: ; preds = %38
%44 = call i32 @pci_device_cfg_read_u32(%struct.pci_device* %0, i32* %4, i32 %41)
%45 = icmp eq i32 %44, 0
br i1 %45, label %18, label %46
46: ; preds = %43, %38, %24, %29, %9, %3, %34
%47 = phi i32 [ 0, %34 ], [ -1, %3 ], [ -1, %9 ], [ -1, %29 ], [ -1, %24 ], [ -1, %38 ], [ -1, %43 ]
ret i32 %47
}