#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*/ query2;
typedef int /*<<< orphan*/ query1;

/* Variables and functions */
int RS400_recvbytes(unsigned char *);
int /*<<< orphan*/ RS400_sendbytes(unsigned char *, int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int RS400_get_session(char id, unsigned char data[]) {
  unsigned char buf[32768];
  int pos = 0;
  unsigned char query1[] = {0x6a, id, 0x00, 0x00};
  RS400_sendbytes(query1, sizeof(query1));
  int size = RS400_recvbytes(buf) - 4;
  memcpy(data, buf + 4, size);
  unsigned char query2[] = {0x16, id};
  pos = size;
  RS400_sendbytes(query2, sizeof(query2));
  size = RS400_recvbytes(buf) - 5;
  while (size > 2) {
    memcpy(data + pos, buf + 5, size);
    pos += size;
    RS400_sendbytes(query2, sizeof(query2));
    size = RS400_recvbytes(buf) - 5;
  }
  return pos;
}


x86 assembly (clang, -O0):
push   %rbp
mov    %rsp,%rbp
sub    $0x8020,%rsp
mov    %dil,%al
mov    %al,-0x1(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x8014(%rbp)
movb   $0x6a,-0x8018(%rbp)
mov    -0x1(%rbp),%al
mov    %al,-0x8017(%rbp)
movb   $0x0,-0x8016(%rbp)
movb   $0x0,-0x8015(%rbp)
lea    -0x8018(%rbp),%rdi
mov    $0x4,%esi
call   1030 <RS400_sendbytes@plt>
lea    -0x8010(%rbp),%rdi
call   1040 <RS400_recvbytes@plt>
sub    $0x4,%eax
mov    %eax,-0x801c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x8010(%rbp),%rsi
add    $0x4,%rsi
mov    -0x801c(%rbp),%edx
call   1050 <memcpy@plt>
movb   $0x16,-0x801e(%rbp)
mov    -0x1(%rbp),%al
mov    %al,-0x801d(%rbp)
mov    -0x801c(%rbp),%eax
mov    %eax,-0x8014(%rbp)
lea    -0x801e(%rbp),%rdi
mov    $0x2,%esi
call   1030 <RS400_sendbytes@plt>
lea    -0x8010(%rbp),%rdi
call   1040 <RS400_recvbytes@plt>
sub    $0x5,%eax
mov    %eax,-0x801c(%rbp)
cmpl   $0x2,-0x801c(%rbp)
jle    125d <RS400_get_session+0x12d>
mov    -0x10(%rbp),%rdi
movslq -0x8014(%rbp),%rax
add    %rax,%rdi
lea    -0x8010(%rbp),%rsi
add    $0x5,%rsi
mov    -0x801c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x801c(%rbp),%eax
add    -0x8014(%rbp),%eax
mov    %eax,-0x8014(%rbp)
lea    -0x801e(%rbp),%rdi
mov    $0x2,%esi
call   1030 <RS400_sendbytes@plt>
lea    -0x8010(%rbp),%rdi
call   1040 <RS400_recvbytes@plt>
sub    $0x5,%eax
mov    %eax,-0x801c(%rbp)
jmp    11ef <RS400_get_session+0xbf>
mov    -0x8014(%rbp),%eax
add    $0x8020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8018,%rsp
mov    %rsi,%r13
mov    %edi,%ebx
movb   $0x6a,0xc(%rsp)
mov    %bl,0xd(%rsp)
movw   $0x0,0xe(%rsp)
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
call   1030 <RS400_sendbytes@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <RS400_recvbytes@plt>
mov    %eax,%ebp
add    $0xfffffffc,%ebp
lea    0x14(%rsp),%rsi
mov    %r13,%rdi
mov    %ebp,%edx
call   1050 <memcpy@plt>
movb   $0x16,0xa(%rsp)
mov    %bl,0xb(%rsp)
lea    0xa(%rsp),%rdi
mov    $0x2,%esi
call   1030 <RS400_sendbytes@plt>
mov    %r14,%rdi
call   1040 <RS400_recvbytes@plt>
cmp    $0x8,%eax
jl     11f5 <RS400_get_session+0xc5>
mov    %eax,%ebx
lea    0x15(%rsp),%r14
lea    0xa(%rsp),%r15
lea    0x10(%rsp),%r12
nopl   0x0(%rax)
lea    -0x5(%rbx),%edx
movslq %ebp,%rbp
mov    %r13,%rdi
add    %rbp,%rdi
mov    %r14,%rsi
call   1050 <memcpy@plt>
add    %ebx,%ebp
add    $0xfffffffb,%ebp
mov    %r15,%rdi
mov    $0x2,%esi
call   1030 <RS400_sendbytes@plt>
mov    %r12,%rdi
call   1040 <RS400_recvbytes@plt>
mov    %eax,%ebx
cmp    $0x7,%eax
jg     11c0 <RS400_get_session+0x90>
mov    %ebp,%eax
add    $0x8018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @RS400_get_session(i8 signext %0, i8* %1) {
%3 = alloca i8
%4 = alloca i8*
%5 = alloca [32768 x i8]
%6 = alloca i32
%7 = alloca [4 x i8]
%8 = alloca i32
%9 = alloca [2 x i8]
store i8 %0, i8* %3
store i8* %1, i8** %4
store i32 0, i32* %6
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
store i8 106, i8* %10
%11 = getelementptr inbounds i8, i8* %10, i64 1
%12 = load i8, i8* %3
store i8 %12, i8* %11
%13 = getelementptr inbounds i8, i8* %11, i64 1
store i8 0, i8* %13
%14 = getelementptr inbounds i8, i8* %13, i64 1
store i8 0, i8* %14
%15 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%16 = call i32 @RS400_sendbytes(i8* %15, i32 4)
%17 = getelementptr inbounds [32768 x i8], [32768 x i8]* %5, i64 0, i64 0
%18 = call i32 @RS400_recvbytes(i8* %17)
%19 = sub nsw i32 %18, 4
store i32 %19, i32* %8
%20 = load i8*, i8** %4
%21 = getelementptr inbounds [32768 x i8], [32768 x i8]* %5, i64 0, i64 0
%22 = getelementptr inbounds i8, i8* %21, i64 4
%23 = load i32, i32* %8
%24 = call i32 @memcpy(i8* %20, i8* %22, i32 %23)
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
store i8 22, i8* %25
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8, i8* %3
store i8 %27, i8* %26
%28 = load i32, i32* %8
store i32 %28, i32* %6
%29 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%30 = call i32 @RS400_sendbytes(i8* %29, i32 2)
%31 = getelementptr inbounds [32768 x i8], [32768 x i8]* %5, i64 0, i64 0
%32 = call i32 @RS400_recvbytes(i8* %31)
%33 = sub nsw i32 %32, 5
store i32 %33, i32* %8
br label %34
34: ; preds = %37, %2
%35 = load i32, i32* %8
%36 = icmp sgt i32 %35, 2
br i1 %36, label %37, label %54
37: ; preds = %34
%38 = load i8*, i8** %4
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = getelementptr inbounds [32768 x i8], [32768 x i8]* %5, i64 0, i64 0
%43 = getelementptr inbounds i8, i8* %42, i64 5
%44 = load i32, i32* %8
%45 = call i32 @memcpy(i8* %41, i8* %43, i32 %44)
%46 = load i32, i32* %8
%47 = load i32, i32* %6
%48 = add nsw i32 %47, %46
store i32 %48, i32* %6
%49 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%50 = call i32 @RS400_sendbytes(i8* %49, i32 2)
%51 = getelementptr inbounds [32768 x i8], [32768 x i8]* %5, i64 0, i64 0
%52 = call i32 @RS400_recvbytes(i8* %51)
%53 = sub nsw i32 %52, 5
store i32 %53, i32* %8
br label %34
54: ; preds = %34
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
define i32 @RS400_get_session(i8 signext %0, i8* %1) {
%3 = alloca [32768 x i8]
%4 = alloca [4 x i8]
%5 = alloca [2 x i8]
%6 = getelementptr inbounds [32768 x i8], [32768 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 0
store i8 106, i8* %7
%8 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 1
store i8 %0, i8* %8
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 2
store i8 0, i8* %9
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %4, i64 0, i64 3
store i8 0, i8* %10
%11 = call i32 @RS400_sendbytes(i8* %7, i32 4)
%12 = call i32 @RS400_recvbytes(i8* %6)
%13 = add nsw i32 %12, -4
%14 = getelementptr inbounds [32768 x i8], [32768 x i8]* %3, i64 0, i64 4
%15 = call i32 @memcpy(i8* %1, i8* %14, i32 %13)
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
store i8 22, i8* %16
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 %0, i8* %17
%18 = call i32 @RS400_sendbytes(i8* %16, i32 2)
%19 = call i32 @RS400_recvbytes(i8* %6)
%20 = icmp sgt i32 %19, 7
br i1 %20, label %21, label %34
21: ; preds = %2
%22 = getelementptr inbounds [32768 x i8], [32768 x i8]* %3, i64 0, i64 5
br label %23
23: ; preds = %21, %23
%24 = phi i32 [ %19, %21 ], [ %32, %23 ]
%25 = phi i32 [ %13, %21 ], [ %30, %23 ]
%26 = add nsw i32 %24, -5
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds i8, i8* %1, i64 %27
%29 = call i32 @memcpy(i8* %28, i8* %22, i32 %26)
%30 = add nsw i32 %26, %25
%31 = call i32 @RS400_sendbytes(i8* %16, i32 2)
%32 = call i32 @RS400_recvbytes(i8* %6)
%33 = icmp sgt i32 %32, 7
br i1 %33, label %23, label %34
34: ; preds = %23, %2
%35 = phi i32 [ %13, %2 ], [ %30, %23 ]
ret i32 %35
}