#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*/ EINVAL;
int /*<<< orphan*/ ERANGE;
int ber_getc(struct ber *, int *);
int /*<<< orphan*/ errno;
ssize_t get_len(struct ber *b, ssize_t *len) {
  u_char u, n;
  ssize_t s, r;
  if (ber_getc(b, &u) == -1)
    return -1;
  if ((u & 0x80) == 0) {
    *len = u;
    return 1;
  }
  n = u & ~0x80;
  if (sizeof(ssize_t) < n) {
    errno = ERANGE;
    return -1;
  }
  r = n + 1;
  for (s = 0; n > 0; n--) {
    if (ber_getc(b, &u) == -1)
      return -1;
    s = (s << 8) | u;
  }
  if (s < 0) {
    errno = ERANGE;
    return -1;
  }
  if (s == 0) {
    errno = EINVAL;
    return -1;
  }
  *len = s;
  return r;
}


x86 assembly (clang, -O0):
<get_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <ber_getc@plt>
cmp    $0xffffffff,%eax
jne    1142 <get_len+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <get_len+0x148>
mov    -0x1c(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1168 <get_len+0x58>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    1258 <get_len+0x148>
mov    -0x1c(%rbp),%eax
and    $0xffffff7f,%eax
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rcx
mov    $0x4,%eax
cmp    %rcx,%rax
jae    11a3 <get_len+0x93>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e43(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <get_len+0x148>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11f9 <get_len+0xe9>
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <ber_getc@plt>
cmp    $0xffffffff,%eax
jne    11df <get_len+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <get_len+0x148>
mov    -0x24(%rbp),%eax
shl    $0x8,%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    11b3 <get_len+0xa3>
cmpl   $0x0,-0x24(%rbp)
jge    1221 <get_len+0x111>
mov    0x2dde(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <get_len+0x148>
cmpl   $0x0,-0x24(%rbp)
jne    1249 <get_len+0x139>
mov    0x2d9e(%rip),%rax
mov    (%rax),%ecx
mov    0x2d9d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1258 <get_len+0x148>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_len>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1030 <ber_getc@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     11b3 <get_len+0xa3>
mov    (%rsp),%ebp
test   $0x80,%bpl
jne    1148 <get_len+0x38>
mov    %ebp,(%r14)
mov    $0x1,%r12d
jmp    11b3 <get_len+0xa3>
and    $0xffffff7f,%ebp
cmp    $0x5,%ebp
jb     115c <get_len+0x4c>
mov    0x2e8e(%rip),%rax
jmp    11a8 <get_len+0x98>
test   %ebp,%ebp
je     11a1 <get_len+0x91>
lea    0x1(%rbp),%eax
mov    %eax,0x4(%rsp)
add    $0x1,%ebp
xor    %r13d,%r13d
mov    %rsp,%r15
mov    %rbx,%rdi
mov    %r15,%rsi
call   1030 <ber_getc@plt>
cmp    $0xffffffff,%eax
je     11b3 <get_len+0xa3>
shl    $0x8,%r13d
or     (%rsp),%r13d
add    $0xffffffff,%ebp
cmp    $0x1,%ebp
jg     1170 <get_len+0x60>
test   %r13d,%r13d
js     1153 <get_len+0x43>
je     11a1 <get_len+0x91>
mov    %r13d,(%r14)
mov    0x4(%rsp),%r12d
jmp    11b3 <get_len+0xa3>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    0x2e27(%rip),%rcx
mov    %eax,(%rcx)
mov    %r12d,%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
@EINVAL = global i32 0
define i32 @get_len(%struct.ber* %0, i32* %1) {
%3 = alloca i32
%4 = alloca %struct.ber*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store %struct.ber* %0, %struct.ber** %4
store i32* %1, i32** %5
%10 = load %struct.ber*, %struct.ber** %4
%11 = call i32 @ber_getc(%struct.ber* %10, i32* %6)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %62
14: ; preds = %2
%15 = load i32, i32* %6
%16 = and i32 %15, 128
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i32, i32* %6
%20 = load i32*, i32** %5
store i32 %19, i32* %20
store i32 1, i32* %3
br label %62
21: ; preds = %14
%22 = load i32, i32* %6
%23 = and i32 %22, -129
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = sext i32 %24 to i64
%26 = icmp ult i64 4, %25
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i32, i32* @ERANGE
store i32 %28, i32* @errno
store i32 -1, i32* %3
br label %62
29: ; preds = %21
%30 = load i32, i32* %7
%31 = add nsw i32 %30, 1
store i32 %31, i32* %9
store i32 0, i32* %8
br label %32
32: ; preds = %45, %29
%33 = load i32, i32* %7
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %48
35: ; preds = %32
%36 = load %struct.ber*, %struct.ber** %4
%37 = call i32 @ber_getc(%struct.ber* %36, i32* %6)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %3
br label %62
40: ; preds = %35
%41 = load i32, i32* %8
%42 = shl i32 %41, 8
%43 = load i32, i32* %6
%44 = or i32 %42, %43
store i32 %44, i32* %8
br label %45
45: ; preds = %40
%46 = load i32, i32* %7
%47 = add nsw i32 %46, -1
store i32 %47, i32* %7
br label %32
48: ; preds = %32
%49 = load i32, i32* %8
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = load i32, i32* @ERANGE
store i32 %52, i32* @errno
store i32 -1, i32* %3
br label %62
53: ; preds = %48
%54 = load i32, i32* %8
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = load i32, i32* @EINVAL
store i32 %57, i32* @errno
store i32 -1, i32* %3
br label %62
58: ; preds = %53
%59 = load i32, i32* %8
%60 = load i32*, i32** %5
store i32 %59, i32* %60
%61 = load i32, i32* %9
store i32 %61, i32* %3
br label %62
62: ; preds = %58, %56, %51, %39, %27, %18, %13
%63 = load i32, i32* %3
ret i32 %63
}

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