#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 /*<<< orphan*/ bech32_decode(char *, int *, size_t *, char const *);
int /*<<< orphan*/ convert_bits(int *, size_t *, int, int *, size_t, int,
                                int /*<<< orphan*/);
scalar_t__ strncmp(char const *, char *, int);
int segwit_addr_decode(int *witver, uint8_t *witdata, size_t *witdata_len,
                       const char *hrp, const char *addr) {
  uint8_t data[84];
  char hrp_actual[84];
  size_t data_len;
  if (!bech32_decode(hrp_actual, data, &data_len, addr))
    return 0;
  if (data_len == 0 || data_len > 65)
    return 0;
  if (strncmp(hrp, hrp_actual, 84) != 0)
    return 0;
  if (data[0] > 16)
    return 0;
  *witdata_len = 0;
  if (!convert_bits(witdata, witdata_len, 8, data + 1, data_len - 1, 5, 0))
    return 0;
  if (*witdata_len < 2 || *witdata_len > 40)
    return 0;
  if (data[0] == 0 && *witdata_len != 20 && *witdata_len != 32)
    return 0;
  *witver = data[0];
  return 1;
}


x86 assembly (clang, -O0):
<segwit_addr_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
lea    -0x1e0(%rbp),%rdi
lea    -0x180(%rbp),%rsi
mov    -0x30(%rbp),%rcx
lea    -0x1e8(%rbp),%rdx
call   1050 <bech32_decode@plt>
cmp    $0x0,%eax
jne    1182 <segwit_addr_decode+0x52>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <segwit_addr_decode+0x185>
cmpq   $0x0,-0x1e8(%rbp)
je     119e <segwit_addr_decode+0x6e>
cmpq   $0x41,-0x1e8(%rbp)
41
jbe    11aa <segwit_addr_decode+0x7a>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <segwit_addr_decode+0x185>
mov    -0x28(%rbp),%rdi
lea    -0x1e0(%rbp),%rsi
mov    $0x54,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     11d5 <segwit_addr_decode+0xa5>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <segwit_addr_decode+0x185>
cmpl   $0x10,-0x180(%rbp)
jle    11ee <segwit_addr_decode+0xbe>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <segwit_addr_decode+0x185>
mov    -0x20(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x180(%rbp),%rcx
add    $0x4,%rcx
mov    -0x1e8(%rbp),%r8
sub    $0x1,%r8
mov    $0x8,%edx
mov    $0x5,%r9d
xor    %eax,%eax
movl   $0x0,(%rsp)
call   1040 <convert_bits@plt>
cmp    $0x0,%eax
jne    1245 <segwit_addr_decode+0x115>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <segwit_addr_decode+0x185>
mov    -0x20(%rbp),%rax
cmpq   $0x2,(%rax)
jb     1261 <segwit_addr_decode+0x131>
mov    -0x20(%rbp),%rax
cmpq   $0x28,(%rax)
jbe    126d <segwit_addr_decode+0x13d>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <segwit_addr_decode+0x185>
cmpl   $0x0,-0x180(%rbp)
jne    12a2 <segwit_addr_decode+0x172>
mov    -0x20(%rbp),%rax
cmpq   $0x14,(%rax)
je     12a2 <segwit_addr_decode+0x172>
mov    -0x20(%rbp),%rax
cmpq   $0x20,(%rax)
je     12a2 <segwit_addr_decode+0x172>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <segwit_addr_decode+0x185>
mov    -0x180(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<segwit_addr_decode>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1c0,%rsp
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0x10(%rsp),%rdi
lea    0x70(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %r8,%rcx
call   1050 <bech32_decode@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     11ec <segwit_addr_decode+0xbc>
mov    0x8(%rsp),%rax
add    $0xffffffffffffffbe,%rax
cmp    $0xffffffffffffffbf,%rax
jb     11ec <segwit_addr_decode+0xbc>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x54,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11ec <segwit_addr_decode+0xbc>
cmpl   $0x10,0x70(%rsp)
jg     11ec <segwit_addr_decode+0xbc>
movq   $0x0,(%r15)
lea    0x74(%rsp),%rcx
mov    0x8(%rsp),%r8
add    $0xffffffffffffffff,%r8
movl   $0x0,(%rsp)
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x8,%edx
mov    $0x5,%r9d
call   1040 <convert_bits@plt>
test   %eax,%eax
je     11ec <segwit_addr_decode+0xbc>
mov    (%r15),%rcx
lea    -0x29(%rcx),%rax
cmp    $0xffffffffffffffd9,%rax
jb     11ec <segwit_addr_decode+0xbc>
mov    0x70(%rsp),%eax
test   %eax,%eax
je     11fe <segwit_addr_decode+0xce>
mov    %eax,(%r14)
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x1c0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
cmp    $0x20,%rcx
je     11e4 <segwit_addr_decode+0xb4>
cmp    $0x14,%rcx
je     11e4 <segwit_addr_decode+0xb4>
jmp    11ec <segwit_addr_decode+0xbc>

LLVM IR (-O0):
define i32 @segwit_addr_decode(i32* %0, i32* %1, i64* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i64*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca [84 x i32]
%13 = alloca [84 x i8]
%14 = alloca i64
store i32* %0, i32** %7
store i32* %1, i32** %8
store i64* %2, i64** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%15 = getelementptr inbounds [84 x i8], [84 x i8]* %13, i64 0, i64 0
%16 = getelementptr inbounds [84 x i32], [84 x i32]* %12, i64 0, i64 0
%17 = load i8*, i8** %11
%18 = call i32 @bech32_decode(i8* %15, i32* %16, i64* %14, i8* %17)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %5
store i32 0, i32* %6
br label %76
21: ; preds = %5
%22 = load i64, i64* %14
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = load i64, i64* %14
%26 = icmp ugt i64 %25, 65
br i1 %26, label %27, label %28
27: ; preds = %24, %21
store i32 0, i32* %6
br label %76
28: ; preds = %24
%29 = load i8*, i8** %10
%30 = getelementptr inbounds [84 x i8], [84 x i8]* %13, i64 0, i64 0
%31 = call i64 @strncmp(i8* %29, i8* %30, i32 84)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
store i32 0, i32* %6
br label %76
34: ; preds = %28
%35 = getelementptr inbounds [84 x i32], [84 x i32]* %12, i64 0, i64 0
%36 = load i32, i32* %35
%37 = icmp sgt i32 %36, 16
br i1 %37, label %38, label %39
38: ; preds = %34
store i32 0, i32* %6
br label %76
39: ; preds = %34
%40 = load i64*, i64** %9
store i64 0, i64* %40
%41 = load i32*, i32** %8
%42 = load i64*, i64** %9
%43 = getelementptr inbounds [84 x i32], [84 x i32]* %12, i64 0, i64 0
%44 = getelementptr inbounds i32, i32* %43, i64 1
%45 = load i64, i64* %14
%46 = sub i64 %45, 1
%47 = call i32 @convert_bits(i32* %41, i64* %42, i32 8, i32* %44, i64 %46, i32 5, i32 0)
%48 = icmp ne i32 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %39
store i32 0, i32* %6
br label %76
50: ; preds = %39
%51 = load i64*, i64** %9
%52 = load i64, i64* %51
%53 = icmp ult i64 %52, 2
br i1 %53, label %58, label %54
54: ; preds = %50
%55 = load i64*, i64** %9
%56 = load i64, i64* %55
%57 = icmp ugt i64 %56, 40
br i1 %57, label %58, label %59
58: ; preds = %54, %50
store i32 0, i32* %6
br label %76
59: ; preds = %54
%60 = getelementptr inbounds [84 x i32], [84 x i32]* %12, i64 0, i64 0
%61 = load i32, i32* %60
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %72
63: ; preds = %59
%64 = load i64*, i64** %9
%65 = load i64, i64* %64
%66 = icmp ne i64 %65, 20
br i1 %66, label %67, label %72
67: ; preds = %63
%68 = load i64*, i64** %9
%69 = load i64, i64* %68
%70 = icmp ne i64 %69, 32
br i1 %70, label %71, label %72
71: ; preds = %67
store i32 0, i32* %6
br label %76
72: ; preds = %67, %63, %59
%73 = getelementptr inbounds [84 x i32], [84 x i32]* %12, i64 0, i64 0
%74 = load i32, i32* %73
%75 = load i32*, i32** %7
store i32 %74, i32* %75
store i32 1, i32* %6
br label %76
76: ; preds = %72, %71, %58, %49, %38, %33, %27, %20
%77 = load i32, i32* %6
ret i32 %77
}

LLVM IR (-O2):
define i32 @segwit_addr_decode(i32* nocapture writeonly %0, i32* %1, i64* %2, i8* %3, i8* %4) {
%6 = alloca [84 x i32]
%7 = alloca [84 x i8]
%8 = alloca i64
%9 = bitcast [84 x i32]* %6 to i8*
%10 = getelementptr inbounds [84 x i8], [84 x i8]* %7, i64 0, i64 0
%11 = bitcast i64* %8 to i8*
%12 = getelementptr inbounds [84 x i32], [84 x i32]* %6, i64 0, i64 0
%13 = call i32 @bech32_decode(i8* %10, i32* %12, i64* %8, i8* %4)
%14 = icmp eq i32 %13, 0
br i1 %14, label %41, label %15
15: ; preds = %5
%16 = load i64, i64* %8
%17 = add i64 %16, -66
%18 = icmp ult i64 %17, -65
br i1 %18, label %41, label %19
19: ; preds = %15
%20 = call i64 @strncmp(i8* %3, i8* %10, i32 84)
%21 = icmp ne i64 %20, 0
%22 = load i32, i32* %12
%23 = icmp sgt i32 %22, 16
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %41, label %25
25: ; preds = %19
store i64 0, i64* %2
%26 = getelementptr inbounds [84 x i32], [84 x i32]* %6, i64 0, i64 1
%27 = load i64, i64* %8
%28 = add i64 %27, -1
%29 = call i32 @convert_bits(i32* %1, i64* %2, i32 8, i32* %26, i64 %28, i32 5, i32 0)
%30 = icmp eq i32 %29, 0
br i1 %30, label %41, label %31
31: ; preds = %25
%32 = load i64, i64* %2
%33 = add i64 %32, -41
%34 = icmp ult i64 %33, -39
br i1 %34, label %41, label %35
35: ; preds = %31
%36 = load i32, i32* %12
%37 = freeze i32 %36
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %35
switch i64 %32, label %41 [
i64 32, label %40
i64 20, label %40
]
40: ; preds = %39, %39, %35
store i32 %37, i32* %0
br label %41
41: ; preds = %39, %31, %25, %19, %15, %5, %40
%42 = phi i32 [ 1, %40 ], [ 0, %5 ], [ 0, %15 ], [ 0, %19 ], [ 0, %25 ], [ 0, %31 ], [ 0, %39 ]
ret i32 %42
}