#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 bech32_encode(char *, char const *, int *, size_t);
int /*<<< orphan*/ convert_bits(int *, size_t *, int, int const *, size_t, int,
                                int);
int segwit_addr_encode(char *output, const char *hrp, int witver,
                       const uint8_t *witprog, size_t witprog_len) {
  uint8_t data[65];
  size_t datalen = 0;
  if (witver > 16)
    return 0;
  if (witver == 0 && witprog_len != 20 && witprog_len != 32)
    return 0;
  if (witprog_len < 2 || witprog_len > 40)
    return 0;
  data[0] = witver;
  convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1);
  ++datalen;
  return bech32_encode(output, hrp, data, datalen);
}


x86 assembly (clang, -O0):
<segwit_addr_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x148(%rbp)
cmpl   $0x10,-0x1c(%rbp)
jle    115f <segwit_addr_encode+0x3f>
movl   $0x0,-0x4(%rbp)
jmp    1217 <segwit_addr_encode+0xf7>
cmpl   $0x0,-0x1c(%rbp)
jne    118b <segwit_addr_encode+0x6b>
cmpq   $0x14,-0x30(%rbp)
je     118b <segwit_addr_encode+0x6b>
cmpq   $0x20,-0x30(%rbp)
je     118b <segwit_addr_encode+0x6b>
movl   $0x0,-0x4(%rbp)
jmp    1217 <segwit_addr_encode+0xf7>
cmpq   $0x2,-0x30(%rbp)
jb     11a1 <segwit_addr_encode+0x81>
cmpq   $0x28,-0x30(%rbp)
jbe    11ad <segwit_addr_encode+0x8d>
movl   $0x0,-0x4(%rbp)
jmp    1217 <segwit_addr_encode+0xf7>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x140(%rbp)
lea    -0x140(%rbp),%rdi
add    $0x4,%rdi
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    -0x148(%rbp),%rsi
mov    $0x5,%edx
mov    $0x8,%r9d
movl   $0x1,(%rsp)
call   1040 <convert_bits@plt>
mov    -0x148(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x148(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x140(%rbp),%rdx
mov    -0x148(%rbp),%rcx
call   1030 <bech32_encode@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<segwit_addr_encode>:
push   %r14
push   %rbx
sub    $0x118,%rsp
movq   $0x0,0x8(%rsp)
xor    %eax,%eax
cmp    $0x10,%edx
jg     119f <segwit_addr_encode+0x7f>
mov    %rsi,%r14
mov    %rdi,%rbx
test   %edx,%edx
je     1150 <segwit_addr_encode+0x30>
lea    -0x29(%r8),%rsi
cmp    $0xffffffffffffffd9,%rsi
jae    115c <segwit_addr_encode+0x3c>
jmp    119f <segwit_addr_encode+0x7f>
cmp    $0x14,%r8
je     115c <segwit_addr_encode+0x3c>
cmp    $0x20,%r8
jne    119f <segwit_addr_encode+0x7f>
mov    %edx,0x10(%rsp)
lea    0x14(%rsp),%rdi
movl   $0x1,(%rsp)
lea    0x8(%rsp),%rsi
mov    $0x5,%edx
mov    $0x8,%r9d
call   1040 <convert_bits@plt>
mov    0x8(%rsp),%rcx
add    $0x1,%rcx
mov    %rcx,0x8(%rsp)
lea    0x10(%rsp),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <bech32_encode@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i32 @segwit_addr_encode(i8* %0, i8* %1, i32 %2, i32* %3, i64 %4) {
%6 = alloca [65 x i32]
%7 = alloca i64
%8 = bitcast [65 x i32]* %6 to i8*
%9 = bitcast i64* %7 to i8*
store i64 0, i64* %7
%10 = icmp sgt i32 %2, 16
br i1 %10, label %24, label %11
11: ; preds = %5
%12 = icmp eq i32 %2, 0
br i1 %12, label %13, label %14
13: ; preds = %11
switch i64 %4, label %24 [
i64 32, label %17
i64 20, label %17
]
14: ; preds = %11
%15 = add i64 %4, -41
%16 = icmp ult i64 %15, -39
br i1 %16, label %24, label %17
17: ; preds = %13, %13, %14
%18 = getelementptr inbounds [65 x i32], [65 x i32]* %6, i64 0, i64 0
store i32 %2, i32* %18
%19 = getelementptr inbounds [65 x i32], [65 x i32]* %6, i64 0, i64 1
%20 = call i32 @convert_bits(i32* %19, i64* %7, i32 5, i32* %3, i64 %4, i32 8, i32 1)
%21 = load i64, i64* %7
%22 = add i64 %21, 1
store i64 %22, i64* %7
%23 = call i32 @bech32_encode(i8* %0, i8* %1, i32* %18, i64 %22)
br label %24
24: ; preds = %14, %13, %5, %17
%25 = phi i32 [ %23, %17 ], [ 0, %5 ], [ 0, %13 ], [ 0, %14 ]
ret i32 %25
}