#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 */
unsigned int readbits(unsigned char **, int *, int);
int read_instruction(unsigned char **ptr, int *bitptr) {
  int instr = -1;
  unsigned int bits = readbits(ptr, bitptr, 1);
  if (bits)
    instr = 0;
  else {
    bits = readbits(ptr, bitptr, 1);
    if (!bits)
      instr = 1;
    else {
      bits = readbits(ptr, bitptr, 2);
      if (bits == 0)
        instr = 2;
      else if (bits == 1)
        instr = 3;
      else if (bits == 2)
        instr = 4;
      else if (bits == 3) {
        bits = readbits(ptr, bitptr, 1);
        if (bits == 0)
          instr = 5;
        else
          instr = 6;
      }
    }
  }
  return instr;
}


x86 assembly (clang, -O0):
<read_instruction>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <readbits@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1152 <read_instruction+0x42>
movl   $0x0,-0x14(%rbp)
jmp    122e <read_instruction+0x11e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <readbits@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    117d <read_instruction+0x6d>
movl   $0x1,-0x14(%rbp)
jmp    1229 <read_instruction+0x119>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1030 <readbits@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11a8 <read_instruction+0x98>
movl   $0x2,-0x14(%rbp)
jmp    1224 <read_instruction+0x114>
cmpl   $0x1,-0x18(%rbp)
jne    11be <read_instruction+0xae>
movl   $0x3,-0x14(%rbp)
jmp    121f <read_instruction+0x10f>
cmpl   $0x2,-0x18(%rbp)
jne    11d4 <read_instruction+0xc4>
movl   $0x4,-0x14(%rbp)
jmp    121a <read_instruction+0x10a>
cmpl   $0x3,-0x18(%rbp)
jne    1215 <read_instruction+0x105>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <readbits@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1209 <read_instruction+0xf9>
movl   $0x5,-0x14(%rbp)
jmp    1210 <read_instruction+0x100>
movl   $0x6,-0x14(%rbp)
jmp    1215 <read_instruction+0x105>
jmp    121a <read_instruction+0x10a>
jmp    121f <read_instruction+0x10f>
jmp    1224 <read_instruction+0x114>
jmp    1229 <read_instruction+0x119>
jmp    122e <read_instruction+0x11e>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_instruction>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x1,%edx
call   1030 <readbits@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     1131 <read_instruction+0x21>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <readbits@plt>
mov    $0x1,%ebp
test   %eax,%eax
je     112a <read_instruction+0x1a>
mov    $0x2,%ebp
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x2,%edx
call   1030 <readbits@plt>
cmp    $0x3,%eax
ja     117d <read_instruction+0x6d>
mov    %eax,%eax
lea    0xe93(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0x3,%ebp
jmp    112a <read_instruction+0x1a>
mov    $0xffffffff,%ebp
jmp    112a <read_instruction+0x1a>
mov    $0x4,%ebp
jmp    112a <read_instruction+0x1a>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <readbits@plt>
cmp    $0x1,%eax
mov    $0x6,%ebp
sbb    $0x0,%ebp
jmp    112a <read_instruction+0x1a>

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

LLVM IR (-O2):
define i32 @read_instruction(i8** %0, i32* %1) {
%3 = tail call i32 @readbits(i8** %0, i32* %1, i32 1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %17
5: ; preds = %2
%6 = tail call i32 @readbits(i8** %0, i32* %1, i32 1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %5
%9 = tail call i32 @readbits(i8** %0, i32* %1, i32 2)
switch i32 %9, label %16 [
i32 0, label %17
i32 1, label %10
i32 2, label %11
i32 3, label %12
]
10: ; preds = %8
br label %17
11: ; preds = %8
br label %17
12: ; preds = %8
%13 = tail call i32 @readbits(i8** %0, i32* %1, i32 1)
%14 = icmp eq i32 %13, 0
%15 = select i1 %14, i32 5, i32 6
br label %17
16: ; preds = %8
br label %17
17: ; preds = %12, %8, %16, %5, %2, %10, %11
%18 = phi i32 [ 3, %10 ], [ 4, %11 ], [ 0, %2 ], [ 1, %5 ], [ 2, %8 ], [ -1, %16 ], [ %15, %12 ]
ret i32 %18
}