#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 /*<<< orphan*/ dStrHex(char const *, int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int decode_proto_port_vpd(unsigned char *buff, int len) {
  int k, bump, rel_port, desc_len, proto;
  unsigned char *ucp;
  if (len < 4) {
    fprintf(stderr,
            "Protocol-specific port information VPD "
            "page length too short=%d\n",
            len);
    return 97;
  }
  len -= 4;
  ucp = buff + 4;
  for (k = 0; k < len; k += bump, ucp += bump) {
    rel_port = (ucp[0] << 8) + ucp[1];
    printf("Relative port=%d\n", rel_port);
    proto = ucp[2] & 0xf;
    desc_len = (ucp[6] << 8) + ucp[7];
    bump = 8 + desc_len;
    if ((k + bump) > len) {
      fprintf(stderr,
              "Protocol-specific port information VPD "
              "page, short descriptor length=%d, left=%d\n",
              bump, (len - k));
      return 97;
    }
    if (desc_len > 0) {
      switch (proto) {
      case 6:
      default:
        fprintf(stderr, "Unexpected proto=%d\n", proto);
        dStrHex((const char *)ucp, bump, 1);
        break;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<decode_proto_port_vpd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x4,-0x14(%rbp)
jge    116f <decode_proto_port_vpd+0x3f>
mov    0x2e90(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xea4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x61,-0x4(%rbp)
jmp    12a2 <decode_proto_port_vpd+0x172>
mov    -0x14(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    129b <decode_proto_port_vpd+0x16b>
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
shl    $0x8,%eax
mov    -0x30(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
add    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe89(%rip),%rdi
call   1040 <printf@plt>
mov    -0x30(%rbp),%rax
movzbl 0x2(%rax),%eax
and    $0xf,%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
movzbl 0x6(%rax),%eax
shl    $0x8,%eax
mov    -0x30(%rbp),%rcx
movzbl 0x7(%rcx),%ecx
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1227 <decode_proto_port_vpd+0xf7>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    -0x14(%rbp),%ecx
sub    -0x18(%rbp),%ecx
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x61,-0x4(%rbp)
jmp    12a2 <decode_proto_port_vpd+0x172>
cmpl   $0x0,-0x24(%rbp)
jle    1277 <decode_proto_port_vpd+0x147>
mov    -0x28(%rbp),%eax
sub    $0x6,%eax
jne    1247 <decode_proto_port_vpd+0x117>
jmp    1242 <decode_proto_port_vpd+0x112>
jmp    1247 <decode_proto_port_vpd+0x117>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    $0x1,%edx
call   1030 <dStrHex@plt>
jmp    1277 <decode_proto_port_vpd+0x147>
jmp    127c <decode_proto_port_vpd+0x14c>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    118b <decode_proto_port_vpd+0x5b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_proto_port_vpd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
cmp    $0x3,%esi
jg     1162 <decode_proto_port_vpd+0x32>
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
lea    0xead(%rip),%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    121a <decode_proto_port_vpd+0xea>
xor    %eax,%eax
cmp    $0x4,%r12d
je     121f <decode_proto_port_vpd+0xef>
mov    %rdi,%rbx
add    $0xfffffffc,%r12d
add    $0x4,%rbx
lea    0xec1(%rip),%r14
xor    %r13d,%r13d
jmp    119b <decode_proto_port_vpd+0x6b>
cs nopw 0x0(%rax,%rax,1)
nop
add    %rbp,%rbx
mov    %r15d,%r13d
cmp    %r15d,%r12d
jle    11f7 <decode_proto_port_vpd+0xc7>
movzwl (%rbx),%esi
shl    $0x10,%esi
bswap  %esi
mov    %r14,%rdi
call   1040 <printf@plt>
movzwl 0x6(%rbx),%eax
shl    $0x30,%rax
bswap  %rax
lea    0x8(%rax),%rbp
lea    0x0(%rbp,%r13,1),%r15d
cmp    %r12d,%r15d
jg     11fb <decode_proto_port_vpd+0xcb>
test   %eax,%eax
je     1190 <decode_proto_port_vpd+0x60>
movzbl 0x2(%rbx),%edx
and    $0xf,%edx
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xec6(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   1030 <dStrHex@plt>
jmp    1190 <decode_proto_port_vpd+0x60>
xor    %eax,%eax
jmp    121f <decode_proto_port_vpd+0xef>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
sub    %r13d,%r12d
lea    0xe45(%rip),%rsi
mov    %ebp,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x61,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [65 x i8] c"Protocol-specific port information VPD page length too short=%d\0A\00"
@.str.1 = [18 x i8] c"Relative port=%d\0A\00"
@.str.2 = [82 x i8] c"Protocol-specific port information VPD page, short descriptor length=%d, left=%d\0A\00"
@.str.3 = [21 x i8] c"Unexpected proto=%d\0A\00"
define i32 @decode_proto_port_vpd(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 4
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* %5
%17 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 97, i32* %3
br label %93
18: ; preds = %2
%19 = load i32, i32* %5
%20 = sub nsw i32 %19, 4
store i32 %20, i32* %5
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 4
store i8* %22, i8** %11
store i32 0, i32* %6
br label %23
23: ; preds = %84, %18
%24 = load i32, i32* %6
%25 = load i32, i32* %5
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %92
27: ; preds = %23
%28 = load i8*, i8** %11
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
%31 = zext i8 %30 to i32
%32 = shl i32 %31, 8
%33 = load i8*, i8** %11
%34 = getelementptr inbounds i8, i8* %33, i64 1
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = add nsw i32 %32, %36
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i8*, i8** %11
%41 = getelementptr inbounds i8, i8* %40, i64 2
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = and i32 %43, 15
store i32 %44, i32* %10
%45 = load i8*, i8** %11
%46 = getelementptr inbounds i8, i8* %45, i64 6
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = shl i32 %48, 8
%50 = load i8*, i8** %11
%51 = getelementptr inbounds i8, i8* %50, i64 7
%52 = load i8, i8* %51
%53 = zext i8 %52 to i32
%54 = add nsw i32 %49, %53
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = add nsw i32 8, %55
store i32 %56, i32* %7
%57 = load i32, i32* %6
%58 = load i32, i32* %7
%59 = add nsw i32 %57, %58
%60 = load i32, i32* %5
%61 = icmp sgt i32 %59, %60
br i1 %61, label %62, label %69
62: ; preds = %27
%63 = load i32, i32* @stderr
%64 = load i32, i32* %7
%65 = load i32, i32* %5
%66 = load i32, i32* %6
%67 = sub nsw i32 %65, %66
%68 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.2, i64 0, i64 0), i32 %64, i32 %67)
store i32 97, i32* %3
br label %93
69: ; preds = %27
%70 = load i32, i32* %9
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %83
72: ; preds = %69
%73 = load i32, i32* %10
switch i32 %73, label %75 [
i32 6, label %74
]
74: ; preds = %72
br label %75
75: ; preds = %72, %74
%76 = load i32, i32* @stderr
%77 = load i32, i32* %10
%78 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %77)
%79 = load i8*, i8** %11
%80 = load i32, i32* %7
%81 = call i32 @dStrHex(i8* %79, i32 %80, i32 1)
br label %82
82: ; preds = %75
br label %83
83: ; preds = %82, %69
br label %84
84: ; preds = %83
%85 = load i32, i32* %7
%86 = load i32, i32* %6
%87 = add nsw i32 %86, %85
store i32 %87, i32* %6
%88 = load i32, i32* %7
%89 = load i8*, i8** %11
%90 = sext i32 %88 to i64
%91 = getelementptr inbounds i8, i8* %89, i64 %90
store i8* %91, i8** %11
br label %23
92: ; preds = %23
store i32 0, i32* %3
br label %93
93: ; preds = %92, %62, %14
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [65 x i8] c"Protocol-specific port information VPD page length too short=%d\0A\00"
@.str.1 = [18 x i8] c"Relative port=%d\0A\00"
@.str.2 = [82 x i8] c"Protocol-specific port information VPD page, short descriptor length=%d, left=%d\0A\00"
@.str.3 = [21 x i8] c"Unexpected proto=%d\0A\00"
define i32 @decode_proto_port_vpd(i8* %0, i32 %1) {
%3 = icmp slt i32 %1, 4
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %52
7: ; preds = %2
%8 = add nsw i32 %1, -4
%9 = icmp eq i32 %1, 4
br i1 %9, label %52, label %10
10: ; preds = %7
%11 = getelementptr inbounds i8, i8* %0, i64 4
br label %12
12: ; preds = %10, %48
%13 = phi i8* [ %50, %48 ], [ %11, %10 ]
%14 = phi i32 [ %36, %48 ], [ 0, %10 ]
%15 = load i8, i8* %13
%16 = zext i8 %15 to i32
%17 = shl nuw nsw i32 %16, 8
%18 = getelementptr inbounds i8, i8* %13, i64 1
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = or i32 %17, %20
%22 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds i8, i8* %13, i64 2
%24 = load i8, i8* %23
%25 = and i8 %24, 15
%26 = zext i8 %25 to i32
%27 = getelementptr inbounds i8, i8* %13, i64 6
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = shl nuw nsw i32 %29, 8
%31 = getelementptr inbounds i8, i8* %13, i64 7
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = or i32 %30, %33
%35 = add nuw nsw i32 %34, 8
%36 = add nuw nsw i32 %35, %14
%37 = icmp sgt i32 %36, %8
br i1 %37, label %38, label %42
38: ; preds = %12
%39 = load i32, i32* @stderr
%40 = sub nsw i32 %8, %14
%41 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 %40)
br label %52
42: ; preds = %12
%43 = icmp eq i32 %34, 0
br i1 %43, label %48, label %44
44: ; preds = %42
%45 = load i32, i32* @stderr
%46 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%47 = tail call i32 @dStrHex(i8* %13, i32 %35, i32 1)
br label %48
48: ; preds = %42, %44
%49 = zext i32 %35 to i64
%50 = getelementptr inbounds i8, i8* %13, i64 %49
%51 = icmp sgt i32 %8, %36
br i1 %51, label %12, label %52
52: ; preds = %48, %7, %38, %4
%53 = phi i32 [ 97, %4 ], [ 97, %38 ], [ 0, %7 ], [ 0, %48 ]
ret i32 %53
}