#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 cw_read(int, char *, int, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *realloc(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *old_sk_recv(int sock, int ssl) {
  int i = 1;
  int end = 0;
  int tambuffer = 128;
  char *aux;
  char *buffer = malloc(tambuffer + 1);
  char *retval = malloc(tambuffer + 1);
  memset(retval, 0, tambuffer + 1);
  memset(buffer, 0, tambuffer + 1);
  while (!end) {
    int ret = cw_read(sock, buffer, tambuffer, ssl);
    if (ret == 1 && *buffer == ' ' && i == 1)
      break;
    if (i == 1)
      strcpy(retval, buffer);
    else {
      retval = realloc(retval, (tambuffer * i) + 1);
      strncat(retval, buffer, tambuffer + 1);
    }
    i++;
    aux = retval + strlen(retval) - 1;
    if (*aux != '>')
      end = 0;
    else
      end = 1;
    memset(buffer, 0, tambuffer + 1);
  }
  free(buffer);
  return retval;
}


x86 assembly (clang, -O0):
<old_sk_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x1,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x80,-0x14(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1060 <memset@plt>
cmpl   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11f9 <old_sk_recv+0x79>
jmp    12e4 <old_sk_recv+0x164>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%ecx
call   1090 <cw_read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
jne    1237 <old_sk_recv+0xb7>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x20,%eax
jne    1237 <old_sk_recv+0xb7>
cmpl   $0x1,-0xc(%rbp)
jne    1237 <old_sk_recv+0xb7>
jmp    12e4 <old_sk_recv+0x164>
cmpl   $0x1,-0xc(%rbp)
jne    1253 <old_sk_recv+0xd3>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    127d <old_sk_recv+0xfd>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
imul   -0xc(%rbp),%esi
add    $0x1,%esi
call   10a0 <realloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1070 <strncat@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3e,%eax
je     12c7 <old_sk_recv+0x147>
movl   $0x0,-0x10(%rbp)
jmp    12ce <old_sk_recv+0x14e>
movl   $0x1,-0x10(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1060 <memset@plt>
jmp    11e3 <old_sk_recv+0x63>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<old_sk_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,0x4(%rsp)
mov    %edi,%r15d
mov    $0x81,%ebp
mov    $0x81,%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    $0x81,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
xor    %r13d,%r13d
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x81,%edx
call   1060 <memset@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x81,%edx
call   1060 <memset@plt>
jmp    1225 <old_sk_recv+0xa5>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %ebp,%esi
call   10a0 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x81,%edx
call   1070 <strncat@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movzbl -0x1(%rbx,%rax,1),%r14d
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x81,%edx
call   1060 <memset@plt>
sub    $0xffffff80,%ebp
cmp    $0x3e,%r14b
je     125d <old_sk_recv+0xdd>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x80,%edx
mov    0x4(%rsp),%ecx
call   1090 <cw_read@plt>
cmp    $0x1,%eax
jne    124a <old_sk_recv+0xca>
cmpb   $0x20,(%r12)
jne    124a <old_sk_recv+0xca>
test   %r13d,%r13d
je     125d <old_sk_recv+0xdd>
sub    $0x1,%r13d
jae    11e0 <old_sk_recv+0x60>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
jmp    11fd <old_sk_recv+0x7d>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @old_sk_recv(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 1, i32* %5
store i32 0, i32* %6
store i32 128, i32* %7
%12 = load i32, i32* %7
%13 = add nsw i32 %12, 1
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %9
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %10
%18 = load i8*, i8** %10
%19 = load i32, i32* %7
%20 = add nsw i32 %19, 1
%21 = call i32 @memset(i8* %18, i32 0, i32 %20)
%22 = load i8*, i8** %9
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
%25 = call i32 @memset(i8* %22, i32 0, i32 %24)
br label %26
26: ; preds = %81, %2
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
%29 = xor i1 %28, true
br i1 %29, label %30, label %86
30: ; preds = %26
%31 = load i32, i32* %3
%32 = load i8*, i8** %9
%33 = load i32, i32* %7
%34 = load i32, i32* %4
%35 = call i32 @cw_read(i32 %31, i8* %32, i32 %33, i32 %34)
store i32 %35, i32* %11
%36 = load i32, i32* %11
%37 = icmp eq i32 %36, 1
br i1 %37, label %38, label %47
38: ; preds = %30
%39 = load i8*, i8** %9
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 32
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = load i32, i32* %5
%45 = icmp eq i32 %44, 1
br i1 %45, label %46, label %47
46: ; preds = %43
br label %86
47: ; preds = %43, %38, %30
%48 = load i32, i32* %5
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %54
50: ; preds = %47
%51 = load i8*, i8** %10
%52 = load i8*, i8** %9
%53 = call i32 @strcpy(i8* %51, i8* %52)
br label %66
54: ; preds = %47
%55 = load i8*, i8** %10
%56 = load i32, i32* %7
%57 = load i32, i32* %5
%58 = mul nsw i32 %56, %57
%59 = add nsw i32 %58, 1
%60 = call i8* @realloc(i8* %55, i32 %59)
store i8* %60, i8** %10
%61 = load i8*, i8** %10
%62 = load i8*, i8** %9
%63 = load i32, i32* %7
%64 = add nsw i32 %63, 1
%65 = call i32 @strncat(i8* %61, i8* %62, i32 %64)
br label %66
66: ; preds = %54, %50
%67 = load i32, i32* %5
%68 = add nsw i32 %67, 1
store i32 %68, i32* %5
%69 = load i8*, i8** %10
%70 = load i8*, i8** %10
%71 = call i32 @strlen(i8* %70)
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %69, i64 %72
%74 = getelementptr inbounds i8, i8* %73, i64 -1
store i8* %74, i8** %8
%75 = load i8*, i8** %8
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = icmp ne i32 %77, 62
br i1 %78, label %79, label %80
79: ; preds = %66
store i32 0, i32* %6
br label %81
80: ; preds = %66
store i32 1, i32* %6
br label %81
81: ; preds = %80, %79
%82 = load i8*, i8** %9
%83 = load i32, i32* %7
%84 = add nsw i32 %83, 1
%85 = call i32 @memset(i8* %82, i32 0, i32 %84)
br label %26
86: ; preds = %46, %26
%87 = load i8*, i8** %9
%88 = call i32 @free(i8* %87)
%89 = load i8*, i8** %10
ret i8* %89
}

LLVM IR (-O2):
define i8* @old_sk_recv(i32 %0, i32 %1) {
%3 = tail call dereferenceable_or_null(129) i8* @malloc(i32 129)
%4 = tail call dereferenceable_or_null(129) i8* @malloc(i32 129)
%5 = tail call i32 @memset(i8* %4, i32 0, i32 129)
%6 = tail call i32 @memset(i8* %3, i32 0, i32 129)
br label %7
7: ; preds = %2, %26
%8 = phi i32 [ 1, %2 ], [ %28, %26 ]
%9 = phi i8* [ %4, %2 ], [ %27, %26 ]
%10 = tail call i32 @cw_read(i32 %0, i8* %3, i32 128, i32 %1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %7
%13 = load i8, i8* %3
%14 = icmp eq i8 %13, 32
%15 = icmp eq i32 %8, 1
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %36, label %17
17: ; preds = %12, %7
%18 = icmp eq i32 %8, 1
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call i32 @strcpy(i8* %9, i8* %3)
br label %26
21: ; preds = %17
%22 = shl nsw i32 %8, 7
%23 = or i32 %22, 1
%24 = tail call i8* @realloc(i8* %9, i32 %23)
%25 = tail call i32 @strncat(i8* %24, i8* %3, i32 129)
br label %26
26: ; preds = %19, %21
%27 = phi i8* [ %9, %19 ], [ %24, %21 ]
%28 = add nuw nsw i32 %8, 1
%29 = tail call i32 @strlen(i8* %27)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 -1
%32 = getelementptr inbounds i8, i8* %31, i64 %30
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 62
%35 = tail call i32 @memset(i8* %3, i32 0, i32 129)
br i1 %34, label %36, label %7
36: ; preds = %12, %26
%37 = phi i8* [ %9, %12 ], [ %27, %26 ]
%38 = tail call i32 @free(i8* %3)
ret i8* %37
}