#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 /*<<< orphan*/ hash1;
typedef int /*<<< orphan*/ binres;
typedef int /*<<< orphan*/ EC_POINT;
typedef int /*<<< orphan*/ EC_GROUP;

/* Variables and functions */
int /*<<< orphan*/ EC_POINT_point2oct(int /*<<< orphan*/ const *,
                                      int /*<<< orphan*/ const *,
                                      int /*<<< orphan*/, unsigned char *, int,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ POINT_CONVERSION_COMPRESSED;
int /*<<< orphan*/ POINT_CONVERSION_UNCOMPRESSED;
int /*<<< orphan*/ RIPEMD160(unsigned char *, int, unsigned char *);
int /*<<< orphan*/ SHA256(unsigned char *, int, unsigned char *);
int /*<<< orphan*/ vg_b58_encode_check(unsigned char *, int, char *);
void vg_encode_script_address_segwit(const EC_POINT *ppoint,
                                     const EC_GROUP *pgroup, int addrtype,
                                     int compressed, char *result) {
  int key_len;
  key_len = compressed ? 33 : 65;
  unsigned char script_buf[69];
  unsigned char segwit_buf[22] = {
      0,
      0x14,
  };
  unsigned char binres[21] = {
      0,
  };
  unsigned char hash1[32];
  EC_POINT_point2oct(pgroup, ppoint,
                     compressed ? POINT_CONVERSION_COMPRESSED
                                : POINT_CONVERSION_UNCOMPRESSED,
                     script_buf, key_len, NULL);
  SHA256(script_buf, key_len, hash1);
  RIPEMD160(hash1, sizeof(hash1), &segwit_buf[2]);
  binres[0] = addrtype;
  SHA256(segwit_buf, 20 + 2, hash1);
  RIPEMD160(hash1, sizeof(hash1), &binres[1]);
  vg_b58_encode_check(binres, sizeof(binres), result);
}


x86 assembly (clang, -O0):
<vg_encode_script_address_segwit>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x18(%rbp),%edx
mov    $0x41,%eax
mov    $0x21,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    0xe76(%rip),%rax
mov    %rax,-0x90(%rbp)
mov    0xe70(%rip),%rax
mov    %rax,-0x88(%rbp)
mov    0xe6b(%rip),%eax
mov    %eax,-0x80(%rbp)
mov    0xe65(%rip),%ax
mov    %ax,-0x7c(%rbp)
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x15,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0xd8(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11fa <vg_encode_script_address_segwit+0xaa>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xe4(%rbp)
jmp    1209 <vg_encode_script_address_segwit+0xb9>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xe4(%rbp)
mov    -0xd8(%rbp),%rsi
mov    -0xe0(%rbp),%rdi
mov    -0xe4(%rbp),%edx
lea    -0x70(%rbp),%rcx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%r9d
call   1060 <EC_POINT_point2oct@plt>
lea    -0x70(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    -0xd0(%rbp),%rdx
call   1030 <SHA256@plt>
lea    -0xd0(%rbp),%rdi
lea    -0x90(%rbp),%rdx
add    $0x2,%rdx
mov    $0x20,%esi
call   1050 <RIPEMD160@plt>
mov    -0x14(%rbp),%eax
mov    %al,-0xb0(%rbp)
lea    -0x90(%rbp),%rdi
lea    -0xd0(%rbp),%rdx
mov    $0x16,%esi
call   1030 <SHA256@plt>
lea    -0xd0(%rbp),%rdi
lea    -0xb0(%rbp),%rdx
add    $0x1,%rdx
mov    $0x20,%esi
call   1050 <RIPEMD160@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x15,%esi
call   1070 <vg_b58_encode_check@plt>
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vg_encode_script_address_segwit>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xb0,%rsp
mov    %r8,%r14
mov    %edx,%r15d
mov    %rsi,%rax
mov    %rdi,%rsi
mov    0xeac(%rip),%rdx
mov    %rdx,0x2e(%rsp)
xor    %ebx,%ebx
movaps 0xe90(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0xd(%rsp)
test   %ecx,%ecx
sete   %bl
jne    1195 <vg_encode_script_address_segwit+0x55>
mov    0x2e45(%rip),%rcx
jmp    119c <vg_encode_script_address_segwit+0x5c>
mov    0x2e4c(%rip),%rcx
shl    $0x5,%ebx
add    $0x21,%ebx
mov    (%rcx),%edx
lea    0x60(%rsp),%r12
mov    %rax,%rdi
mov    %r12,%rcx
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <EC_POINT_point2oct@plt>
lea    0x40(%rsp),%rbp
mov    %r12,%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
call   1030 <SHA256@plt>
lea    0x22(%rsp),%rdx
mov    %rbp,%rdi
mov    $0x20,%esi
call   1040 <RIPEMD160@plt>
mov    %r15b,(%rsp)
lea    0x20(%rsp),%rdi
mov    $0x16,%esi
mov    %rbp,%rdx
call   1030 <SHA256@plt>
lea    0x1(%rsp),%rdx
mov    %rbp,%rdi
mov    $0x20,%esi
call   1040 <RIPEMD160@plt>
mov    %rsp,%rdi
mov    $0x15,%esi
mov    %r14,%rdx
call   1060 <vg_b58_encode_check@plt>
add    $0xb0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.vg_encode_script_address_segwit.segwit_buf = <{ i8, i8, [20 x i8] }> <{ i8 0, i8 20, [20 x i8] zeroinitializer }>
@POINT_CONVERSION_COMPRESSED = global i32 0
@POINT_CONVERSION_UNCOMPRESSED = global i32 0
define void @vg_encode_script_address_segwit(i32* %0, i32* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca [69 x i8]
%13 = alloca [22 x i8]
%14 = alloca [21 x i8]
%15 = alloca [32 x i8]
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 0
%18 = zext i1 %17 to i64
%19 = select i1 %17, i32 33, i32 65
store i32 %19, i32* %11
%20 = bitcast [22 x i8]* %13 to i8*
%21 = bitcast [21 x i8]* %14 to i8*
%22 = load i32*, i32** %7
%23 = load i32*, i32** %6
%24 = load i32, i32* %9
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %5
%27 = load i32, i32* @POINT_CONVERSION_COMPRESSED
br label %30
28: ; preds = %5
%29 = load i32, i32* @POINT_CONVERSION_UNCOMPRESSED
br label %30
30: ; preds = %28, %26
%31 = phi i32 [ %27, %26 ], [ %29, %28 ]
%32 = getelementptr inbounds [69 x i8], [69 x i8]* %12, i64 0, i64 0
%33 = load i32, i32* %11
%34 = call i32 @EC_POINT_point2oct(i32* %22, i32* %23, i32 %31, i8* %32, i32 %33, i32* null)
%35 = getelementptr inbounds [69 x i8], [69 x i8]* %12, i64 0, i64 0
%36 = load i32, i32* %11
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %15, i64 0, i64 0
%38 = call i32 @SHA256(i8* %35, i32 %36, i8* %37)
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %15, i64 0, i64 0
%40 = getelementptr inbounds [22 x i8], [22 x i8]* %13, i64 0, i64 2
%41 = call i32 @RIPEMD160(i8* %39, i32 32, i8* %40)
%42 = load i32, i32* %8
%43 = trunc i32 %42 to i8
%44 = getelementptr inbounds [21 x i8], [21 x i8]* %14, i64 0, i64 0
store i8 %43, i8* %44
%45 = getelementptr inbounds [22 x i8], [22 x i8]* %13, i64 0, i64 0
%46 = getelementptr inbounds [32 x i8], [32 x i8]* %15, i64 0, i64 0
%47 = call i32 @SHA256(i8* %45, i32 22, i8* %46)
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %15, i64 0, i64 0
%49 = getelementptr inbounds [21 x i8], [21 x i8]* %14, i64 0, i64 1
%50 = call i32 @RIPEMD160(i8* %48, i32 32, i8* %49)
%51 = getelementptr inbounds [21 x i8], [21 x i8]* %14, i64 0, i64 0
%52 = load i8*, i8** %10
%53 = call i32 @vg_b58_encode_check(i8* %51, i32 21, i8* %52)
ret void
}

LLVM IR (-O2):
@__const.vg_encode_script_address_segwit.segwit_buf = <{ i8, i8, [20 x i8] }> <{ i8 0, i8 20, [20 x i8] zeroinitializer }>
@POINT_CONVERSION_COMPRESSED = global i32 0
@POINT_CONVERSION_UNCOMPRESSED = global i32 0
define void @vg_encode_script_address_segwit(i32* %0, i32* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca [69 x i8]
%7 = alloca [22 x i8]
%8 = alloca [21 x i8]
%9 = alloca [32 x i8]
%10 = icmp eq i32 %3, 0
%11 = select i1 %10, i32 65, i32 33
%12 = getelementptr inbounds [69 x i8], [69 x i8]* %6, i64 0, i64 0
%13 = getelementptr inbounds [22 x i8], [22 x i8]* %7, i64 0, i64 0
%14 = getelementptr inbounds [21 x i8], [21 x i8]* %8, i64 0, i64 0
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %9, i64 0, i64 0
%16 = load i32, i32* @POINT_CONVERSION_COMPRESSED
%17 = load i32, i32* @POINT_CONVERSION_UNCOMPRESSED
%18 = select i1 %10, i32 %17, i32 %16
%19 = call i32 @EC_POINT_point2oct(i32* %1, i32* %0, i32 %18, i8* %12, i32 %11, i32* null)
%20 = call i32 @SHA256(i8* %12, i32 %11, i8* %15)
%21 = getelementptr inbounds [22 x i8], [22 x i8]* %7, i64 0, i64 2
%22 = call i32 @RIPEMD160(i8* %15, i32 32, i8* %21)
%23 = trunc i32 %2 to i8
store i8 %23, i8* %14
%24 = call i32 @SHA256(i8* %13, i32 22, i8* %15)
%25 = getelementptr inbounds [21 x i8], [21 x i8]* %8, i64 0, i64 1
%26 = call i32 @RIPEMD160(i8* %15, i32 32, i8* %25)
%27 = call i32 @vg_b58_encode_check(i8* %14, i32 21, i8* %4)
ret void
}