#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 u_char;
struct ber {
  int dummy;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ ERANGE;
int ber_getc(struct ber *, int *);
int /*<<< orphan*/ errno;
ssize_t get_id(struct ber *b, unsigned long *tag, int *class, int *cstruct) {
  u_char u;
  size_t i = 0;
  unsigned long t = 0;
  if (ber_getc(b, &u) == -1)
    return -1;
  *class = (u >> 6) & 0x3;
  *cstruct = (u & 0x20) == 0x20;
  if ((u & 0x1f) != 0x1f) {
    *tag = u & 0x1f;
    return 1;
  }
  do {
    if (ber_getc(b, &u) == -1)
      return -1;
    t = (t << 7) | (u & ~0x80);
    i++;
  } while (u & 0x80);
  if (i > sizeof(unsigned long)) {
    errno = ERANGE;
    return -1;
  }
  *tag = t;
  return i + 1;
}


x86 assembly (clang, -O0):
<get_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1030 <ber_getc@plt>
cmp    $0xffffffff,%eax
jne    115a <get_id+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <get_id+0x139>
mov    -0x2c(%rbp),%ecx
sar    $0x6,%ecx
and    $0x3,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
and    $0x20,%eax
cmp    $0x20,%eax
sete   %al
and    $0x1,%al
movzbl %al,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
and    $0x1f,%eax
cmp    $0x1f,%eax
je     11ab <get_id+0x9b>
mov    -0x2c(%rbp),%eax
and    $0x1f,%eax
movslq %eax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1249 <get_id+0x139>
jmp    11b0 <get_id+0xa0>
mov    -0x10(%rbp),%rdi
lea    -0x2c(%rbp),%rsi
call   1030 <ber_getc@plt>
cmp    $0xffffffff,%eax
jne    11d2 <get_id+0xc2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <get_id+0x139>
mov    -0x40(%rbp),%rax
shl    $0x7,%rax
mov    -0x2c(%rbp),%ecx
and    $0xffffff7f,%ecx
movslq %ecx,%rcx
or     %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    11b0 <get_id+0xa0>
cmpq   $0x8,-0x38(%rbp)
jbe    1233 <get_id+0x123>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ecx
mov    0x2db3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <get_id+0x139>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_id>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,%r13
lea    0x4(%rsp),%rsi
call   1030 <ber_getc@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11d0 <get_id+0xc0>
mov    0x4(%rsp),%eax
mov    %eax,%ecx
shr    $0x6,%ecx
and    $0x3,%ecx
mov    %ecx,0x0(%rbp)
mov    %eax,%ecx
shr    $0x5,%ecx
and    $0x1,%ecx
mov    %ecx,(%r12)
and    $0x1f,%eax
cmp    $0x1f,%eax
jne    11bc <get_id+0xac>
xor    %ebp,%ebp
lea    0x4(%rsp),%r12
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <ber_getc@plt>
cmp    $0xffffffff,%eax
je     11d0 <get_id+0xc0>
mov    %rbx,%rax
shl    $0x7,%rax
movslq 0x4(%rsp),%rcx
mov    %rcx,%rbx
and    $0xffffffffffffff7f,%rbx
or     %rax,%rbx
add    $0x1,%rbp
test   $0x80,%cl
jne    1170 <get_id+0x60>
cmp    $0x9,%rbp
jb     11c7 <get_id+0xb7>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    0x2e20(%rip),%rcx
mov    %eax,(%rcx)
jmp    11d0 <get_id+0xc0>
mov    %rax,(%r14)
mov    $0x1,%r15d
jmp    11d0 <get_id+0xc0>
mov    %rbx,(%r14)
add    $0x1,%ebp
mov    %ebp,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ber = type { i32 }
@ERANGE = global i32 0
@errno = global i32 0
define i32 @get_id(%struct.ber* %0, i64* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca %struct.ber*
%7 = alloca i64*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i64
store %struct.ber* %0, %struct.ber** %6
store i64* %1, i64** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
store i64 0, i64* %11
store i64 0, i64* %12
%13 = load %struct.ber*, %struct.ber** %6
%14 = call i32 @ber_getc(%struct.ber* %13, i32* %10)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 -1, i32* %5
br label %65
17: ; preds = %4
%18 = load i32, i32* %10
%19 = ashr i32 %18, 6
%20 = and i32 %19, 3
%21 = load i32*, i32** %8
store i32 %20, i32* %21
%22 = load i32, i32* %10
%23 = and i32 %22, 32
%24 = icmp eq i32 %23, 32
%25 = zext i1 %24 to i32
%26 = load i32*, i32** %9
store i32 %25, i32* %26
%27 = load i32, i32* %10
%28 = and i32 %27, 31
%29 = icmp ne i32 %28, 31
br i1 %29, label %30, label %35
30: ; preds = %17
%31 = load i32, i32* %10
%32 = and i32 %31, 31
%33 = sext i32 %32 to i64
%34 = load i64*, i64** %7
store i64 %33, i64* %34
store i32 1, i32* %5
br label %65
35: ; preds = %17
br label %36
36: ; preds = %50, %35
%37 = load %struct.ber*, %struct.ber** %6
%38 = call i32 @ber_getc(%struct.ber* %37, i32* %10)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 -1, i32* %5
br label %65
41: ; preds = %36
%42 = load i64, i64* %12
%43 = shl i64 %42, 7
%44 = load i32, i32* %10
%45 = and i32 %44, -129
%46 = sext i32 %45 to i64
%47 = or i64 %43, %46
store i64 %47, i64* %12
%48 = load i64, i64* %11
%49 = add i64 %48, 1
store i64 %49, i64* %11
br label %50
50: ; preds = %41
%51 = load i32, i32* %10
%52 = and i32 %51, 128
%53 = icmp ne i32 %52, 0
br i1 %53, label %36, label %54
54: ; preds = %50
%55 = load i64, i64* %11
%56 = icmp ugt i64 %55, 8
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = load i32, i32* @ERANGE
store i32 %58, i32* @errno
store i32 -1, i32* %5
br label %65
59: ; preds = %54
%60 = load i64, i64* %12
%61 = load i64*, i64** %7
store i64 %60, i64* %61
%62 = load i64, i64* %11
%63 = add i64 %62, 1
%64 = trunc i64 %63 to i32
store i32 %64, i32* %5
br label %65
65: ; preds = %59, %57, %40, %30, %16
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
%struct.ber = type { i32 }
@ERANGE = global i32 0
@errno = global i32 0
define i32 @get_id(%struct.ber* %0, i64* nocapture writeonly %1, i32* nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = call i32 @ber_getc(%struct.ber* %0, i32* %5)
%8 = icmp eq i32 %7, -1
br i1 %8, label %40, label %9
9: ; preds = %4
%10 = load i32, i32* %5
%11 = lshr i32 %10, 6
%12 = and i32 %11, 3
store i32 %12, i32* %2
%13 = lshr i32 %10, 5
%14 = and i32 %13, 1
store i32 %14, i32* %3
%15 = and i32 %10, 31
%16 = icmp eq i32 %15, 31
br i1 %16, label %19, label %17
17: ; preds = %9
%18 = zext i32 %15 to i64
store i64 %18, i64* %1
br label %40
19: ; preds = %9, %24
%20 = phi i64 [ %30, %24 ], [ 0, %9 ]
%21 = phi i64 [ %29, %24 ], [ 0, %9 ]
%22 = call i32 @ber_getc(%struct.ber* %0, i32* %5)
%23 = icmp eq i32 %22, -1
br i1 %23, label %40, label %24
24: ; preds = %19
%25 = shl i64 %21, 7
%26 = load i32, i32* %5
%27 = and i32 %26, -129
%28 = sext i32 %27 to i64
%29 = or i64 %25, %28
%30 = add i64 %20, 1
%31 = and i32 %26, 128
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %19
33: ; preds = %24
%34 = icmp ugt i64 %30, 8
br i1 %34, label %35, label %37
35: ; preds = %33
%36 = load i32, i32* @ERANGE
store i32 %36, i32* @errno
br label %40
37: ; preds = %33
store i64 %29, i64* %1
%38 = trunc i64 %20 to i32
%39 = add nsw i32 %38, 2
br label %40
40: ; preds = %19, %4, %37, %35, %17
%41 = phi i32 [ 1, %17 ], [ -1, %35 ], [ %39, %37 ], [ -1, %4 ], [ -1, %19 ]
ret i32 %41
}