#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 */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ bitstream_write_byte(char **, int);
int /*<<< orphan*/ bitstream_write_string(char **, char *, int);
int strlen(char *);
int pwp_send_handshake(void *callee, void *udata,
                       int (*send)(void *callee, const void *udata,
                                   const void *send_data, const int len),
                       char *expected_ih, char *my_pi) {
  char buf[1024], *protocol_name = "BitTorrent protocol", *ptr;
  int size, ii;
  assert(NULL != expected_ih);
  assert(NULL != my_pi);
  ptr = buf;
  bitstream_write_byte((char **)&ptr, strlen(protocol_name));
  bitstream_write_string((char **)&ptr, protocol_name, strlen(protocol_name));
  for (ii = 0; ii < 8; ii++)
    bitstream_write_byte((char **)&ptr, 0);
  bitstream_write_string((char **)&ptr, expected_ih, 20);
  bitstream_write_string((char **)&ptr, my_pi, 20);
  size = 1 + strlen(protocol_name) + 8 + 20 + 20;
  if (0 == send(callee, udata, buf, size)) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<pwp_send_handshake>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
lea    0xe9a(%rip),%rax
mov    %rax,-0x438(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
lea    -0x430(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x438(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%esi
lea    -0x440(%rbp),%rdi
call   1040 <bitstream_write_byte@plt>
mov    -0x438(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x438(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x450(%rbp),%rsi
mov    %eax,%edx
lea    -0x440(%rbp),%rdi
call   1030 <bitstream_write_string@plt>
movl   $0x0,-0x448(%rbp)
cmpl   $0x8,-0x448(%rbp)
jge    1223 <pwp_send_handshake+0xe3>
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
call   1040 <bitstream_write_byte@plt>
mov    -0x448(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x448(%rbp)
jmp    11f4 <pwp_send_handshake+0xb4>
mov    -0x28(%rbp),%rsi
lea    -0x440(%rbp),%rdi
mov    $0x14,%edx
call   1030 <bitstream_write_string@plt>
mov    -0x30(%rbp),%rsi
lea    -0x440(%rbp),%rdi
mov    $0x14,%edx
call   1030 <bitstream_write_string@plt>
mov    -0x438(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
add    $0x8,%eax
add    $0x14,%eax
add    $0x14,%eax
mov    %eax,-0x444(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x430(%rbp),%rdx
mov    -0x444(%rbp),%ecx
call   *%rax
mov    %eax,%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    129e <pwp_send_handshake+0x15e>
movl   $0x0,-0x4(%rbp)
jmp    12a5 <pwp_send_handshake+0x165>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pwp_send_handshake>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %r8,%r13
mov    %rcx,%rbx
mov    %rdx,(%rsp)
mov    %rsi,%r15
mov    %rdi,%r12
xor    %edi,%edi
test   %rcx,%rcx
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r13,%r13
setne  %dil
call   1060 <assert@plt>
lea    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
lea    0xe72(%rip),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <bitstream_write_byte@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <bitstream_write_string@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <bitstream_write_byte@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <bitstream_write_byte@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <bitstream_write_byte@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <bitstream_write_byte@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <bitstream_write_byte@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <bitstream_write_byte@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <bitstream_write_byte@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1040 <bitstream_write_byte@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x14,%edx
call   1030 <bitstream_write_string@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    $0x14,%edx
call   1030 <bitstream_write_string@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x31(%rax),%ecx
mov    %r12,%rdi
mov    %r15,%rsi
lea    0x10(%rsp),%rdx
call   *(%rsp)
xor    %ecx,%ecx
test   %eax,%eax
setne  %cl
mov    %ecx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"BitTorrent protocol\00"
define i32 @pwp_send_handshake(i8* %0, i8* %1, i32 (i8*, i8*, i8*, i32)* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32 (i8*, i8*, i8*, i32)*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca [1024 x i8]
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 (i8*, i8*, i8*, i32)* %2, i32 (i8*, i8*, i8*, i32)** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8** %13
%17 = load i8*, i8** %10
%18 = icmp ne i8* null, %17
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i8*, i8** %11
%22 = icmp ne i8* null, %21
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
store i8* %25, i8** %14
%26 = load i8*, i8** %13
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @bitstream_write_byte(i8** %14, i32 %27)
%29 = load i8*, i8** %13
%30 = load i8*, i8** %13
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @bitstream_write_string(i8** %14, i8* %29, i32 %31)
store i32 0, i32* %16
br label %33
33: ; preds = %38, %5
%34 = load i32, i32* %16
%35 = icmp slt i32 %34, 8
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = call i32 @bitstream_write_byte(i8** %14, i32 0)
br label %38
38: ; preds = %36
%39 = load i32, i32* %16
%40 = add nsw i32 %39, 1
store i32 %40, i32* %16
br label %33
41: ; preds = %33
%42 = load i8*, i8** %10
%43 = call i32 @bitstream_write_string(i8** %14, i8* %42, i32 20)
%44 = load i8*, i8** %11
%45 = call i32 @bitstream_write_string(i8** %14, i8* %44, i32 20)
%46 = load i8*, i8** %13
%47 = call i32 @strlen(i8* %46)
%48 = add nsw i32 1, %47
%49 = add nsw i32 %48, 8
%50 = add nsw i32 %49, 20
%51 = add nsw i32 %50, 20
store i32 %51, i32* %15
%52 = load i32 (i8*, i8*, i8*, i32)*, i32 (i8*, i8*, i8*, i32)** %9
%53 = load i8*, i8** %7
%54 = load i8*, i8** %8
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%56 = load i32, i32* %15
%57 = call i32 %52(i8* %53, i8* %54, i8* %55, i32 %56)
%58 = icmp eq i32 0, %57
br i1 %58, label %59, label %60
59: ; preds = %41
store i32 0, i32* %6
br label %61
60: ; preds = %41
store i32 1, i32* %6
br label %61
61: ; preds = %60, %59
%62 = load i32, i32* %6
ret i32 %62
}

LLVM IR (-O2):
@.str = [20 x i8] c"BitTorrent protocol\00"
define i32 @pwp_send_handshake(i8* %0, i8* %1, i32 (i8*, i8*, i8*, i32)* nocapture %2, i8* %3, i8* %4) {
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%9 = bitcast i8** %7 to i8*
%10 = icmp ne i8* %3, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = icmp ne i8* %4, null
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
store i8* %8, i8** %7
%16 = call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @bitstream_write_byte(i8** %7, i32 %16)
%18 = call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @bitstream_write_string(i8** %7, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = call i32 @bitstream_write_byte(i8** %7, i32 0)
%21 = call i32 @bitstream_write_byte(i8** %7, i32 0)
%22 = call i32 @bitstream_write_byte(i8** %7, i32 0)
%23 = call i32 @bitstream_write_byte(i8** %7, i32 0)
%24 = call i32 @bitstream_write_byte(i8** %7, i32 0)
%25 = call i32 @bitstream_write_byte(i8** %7, i32 0)
%26 = call i32 @bitstream_write_byte(i8** %7, i32 0)
%27 = call i32 @bitstream_write_byte(i8** %7, i32 0)
%28 = call i32 @bitstream_write_string(i8** %7, i8* %3, i32 20)
%29 = call i32 @bitstream_write_string(i8** %7, i8* %4, i32 20)
%30 = call i32 @strlen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%31 = add nsw i32 %30, 49
%32 = call i32 %2(i8* %0, i8* %1, i8* %8, i32 %31)
%33 = icmp ne i32 %32, 0
%34 = zext i1 %33 to i32
ret i32 %34
}