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

/* Variables and functions */
int pread(int, int *, int, int);
int vpd_readtag(int fd, int off, int *len) {
  uint8_t tag, tbuf[2];
  if (pread(fd, &tag, 1, off) != 1)
    return -1;
  if (tag == 0 || tag == 0xFF || tag == 0x7F)
    return -1;
  if (tag & 0x80) {
    if (pread(fd, tbuf, 2, off + 1) != 2)
      return -1;
    *len = tbuf[0] + (tbuf[1] << 8);
    return tag;
  }
  *len = tag & 0xF;
  return tag & ~0xF;
}


x86 assembly (clang, -O0):
<vpd_readtag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%ecx
lea    -0x1c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <pread@plt>
cmp    $0x1,%eax
je     114b <vpd_readtag+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <vpd_readtag+0xd6>
cmpl   $0x0,-0x1c(%rbp)
je     116c <vpd_readtag+0x5c>
cmpl   $0xff,-0x1c(%rbp)
je     116c <vpd_readtag+0x5c>
cmpl   $0x7f,-0x1c(%rbp)
jne    1178 <vpd_readtag+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <vpd_readtag+0xd6>
mov    -0x1c(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     11d1 <vpd_readtag+0xc1>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    $0x2,%edx
call   1030 <pread@plt>
cmp    $0x2,%eax
je     11b5 <vpd_readtag+0xa5>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <vpd_readtag+0xd6>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%eax
shl    $0x8,%eax
add    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <vpd_readtag+0xd6>
mov    -0x1c(%rbp),%ecx
and    $0xf,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
and    $0xfffffff0,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vpd_readtag>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x4(%rsp),%rsi
mov    $0x1,%edx
mov    %ebx,%ecx
call   1030 <pread@plt>
mov    $0xffffffff,%r15d
cmp    $0x1,%eax
jne    1199 <vpd_readtag+0x89>
mov    0x4(%rsp),%r12d
test   %r12d,%r12d
je     1199 <vpd_readtag+0x89>
cmp    $0x7f,%r12d
je     1199 <vpd_readtag+0x89>
cmp    $0xff,%r12d
je     1199 <vpd_readtag+0x89>
test   $0x80,%r12b
jne    116d <vpd_readtag+0x5d>
mov    %r12d,%eax
and    $0xf,%eax
mov    %eax,(%r14)
and    $0xfffffff0,%r12d
jmp    1196 <vpd_readtag+0x86>
add    $0x1,%ebx
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
mov    %ebx,%ecx
call   1030 <pread@plt>
cmp    $0x2,%eax
jne    1199 <vpd_readtag+0x89>
mov    0xc(%rsp),%eax
shl    $0x8,%eax
add    0x8(%rsp),%eax
mov    %eax,(%r14)
mov    %r12d,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @vpd_readtag(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca [2 x i32]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
%10 = load i32, i32* %5
%11 = load i32, i32* %6
%12 = call i32 @pread(i32 %10, i32* %8, i32 1, i32 %11)
%13 = icmp ne i32 %12, 1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %52
15: ; preds = %3
%16 = load i32, i32* %8
%17 = icmp eq i32 %16, 0
br i1 %17, label %24, label %18
18: ; preds = %15
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, 255
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i32, i32* %8
%23 = icmp eq i32 %22, 127
br i1 %23, label %24, label %25
24: ; preds = %21, %18, %15
store i32 -1, i32* %4
br label %52
25: ; preds = %21
%26 = load i32, i32* %8
%27 = and i32 %26, 128
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %46
29: ; preds = %25
%30 = load i32, i32* %5
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%32 = load i32, i32* %6
%33 = add nsw i32 %32, 1
%34 = call i32 @pread(i32 %30, i32* %31, i32 2, i32 %33)
%35 = icmp ne i32 %34, 2
br i1 %35, label %36, label %37
36: ; preds = %29
store i32 -1, i32* %4
br label %52
37: ; preds = %29
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%39 = load i32, i32* %38
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%41 = load i32, i32* %40
%42 = shl i32 %41, 8
%43 = add nsw i32 %39, %42
%44 = load i32*, i32** %7
store i32 %43, i32* %44
%45 = load i32, i32* %8
store i32 %45, i32* %4
br label %52
46: ; preds = %25
%47 = load i32, i32* %8
%48 = and i32 %47, 15
%49 = load i32*, i32** %7
store i32 %48, i32* %49
%50 = load i32, i32* %8
%51 = and i32 %50, -16
store i32 %51, i32* %4
br label %52
52: ; preds = %46, %37, %36, %24, %14
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
define i32 @vpd_readtag(i32 %0, i32 %1, i32* nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca [2 x i32]
%6 = bitcast i32* %4 to i8*
%7 = bitcast [2 x i32]* %5 to i8*
%8 = call i32 @pread(i32 %0, i32* %4, i32 1, i32 %1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %29
10: ; preds = %3
%11 = load i32, i32* %4
switch i32 %11, label %12 [
i32 255, label %29
i32 127, label %29
i32 0, label %29
]
12: ; preds = %10
%13 = and i32 %11, 128
%14 = icmp eq i32 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%17 = add nsw i32 %1, 1
%18 = call i32 @pread(i32 %0, i32* %16, i32 2, i32 %17)
%19 = icmp eq i32 %18, 2
br i1 %19, label %20, label %29
20: ; preds = %15
%21 = load i32, i32* %16
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%23 = load i32, i32* %22
%24 = shl i32 %23, 8
%25 = add nsw i32 %24, %21
store i32 %25, i32* %2
br label %29
26: ; preds = %12
%27 = and i32 %11, 15
store i32 %27, i32* %2
%28 = and i32 %11, -16
br label %29
29: ; preds = %15, %10, %10, %10, %3, %26, %20
%30 = phi i32 [ %11, %20 ], [ %28, %26 ], [ -1, %3 ], [ -1, %10 ], [ -1, %10 ], [ -1, %10 ], [ -1, %15 ]
ret i32 %30
}