#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 */
scalar_t__ check_reply(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ realloc(char *, scalar_t__);
char *sk_recv(int, int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
char *srv_readstr(int sock, int ssl) {
  char *ret = NULL;
  char *str;
  do {
    str = sk_recv(sock, ssl);
    if (str == NULL) {
      if (ret)
        free(ret);
      return NULL;
    } else if (ret) {
      ret = (char *)realloc(ret, strlen(ret) + strlen(str) + 1);
      strcat(ret, str);
      free(str);
    } else
      ret = str;
  } while (check_reply(ret, NULL));
  return ret;
}


x86 assembly (clang, -O0):
<srv_readstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1070 <sk_recv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b1 <srv_readstr+0x51>
cmpq   $0x0,-0x18(%rbp)
je     11a4 <srv_readstr+0x44>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    123e <srv_readstr+0xde>
cmpq   $0x0,-0x18(%rbp)
je     120d <srv_readstr+0xad>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdi
add    %rax,%rsi
add    $0x1,%rsi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    1215 <srv_readstr+0xb5>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    121a <srv_readstr+0xba>
jmp    121f <srv_readstr+0xbf>
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <check_reply@plt>
cmp    $0x0,%rax
jne    1176 <srv_readstr+0x16>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<srv_readstr>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %edi,%r15d
xor    %ebp,%ebp
jmp    1192 <srv_readstr+0x32>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <check_reply@plt>
mov    %rbx,%rbp
test   %rax,%rax
je     11f7 <srv_readstr+0x97>
mov    %r15d,%edi
mov    %r14d,%esi
call   1070 <sk_recv@plt>
test   %rax,%rax
je     11e8 <srv_readstr+0x88>
mov    %rax,%rbx
test   %rbp,%rbp
je     1180 <srv_readstr+0x20>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    (%r12,%rax,1),%rsi
add    $0x1,%rsi
mov    %rbp,%rdi
call   1060 <realloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rbx
jmp    1180 <srv_readstr+0x20>
test   %rbp,%rbp
je     11f5 <srv_readstr+0x95>
mov    %rbp,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @srv_readstr(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* null, i8** %6
br label %8
8: ; preds = %43, %2
%9 = load i32, i32* %4
%10 = load i32, i32* %5
%11 = call i8* @sk_recv(i32 %9, i32 %10)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %21
14: ; preds = %8
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i8*, i8** %6
%19 = call i32 @free(i8* %18)
br label %20
20: ; preds = %17, %14
store i8* null, i8** %3
br label %49
21: ; preds = %8
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %39
24: ; preds = %21
%25 = load i8*, i8** %6
%26 = load i8*, i8** %6
%27 = call i64 @strlen(i8* %26)
%28 = load i8*, i8** %7
%29 = call i64 @strlen(i8* %28)
%30 = add nsw i64 %27, %29
%31 = add nsw i64 %30, 1
%32 = call i64 @realloc(i8* %25, i64 %31)
%33 = inttoptr i64 %32 to i8*
store i8* %33, i8** %6
%34 = load i8*, i8** %6
%35 = load i8*, i8** %7
%36 = call i32 @strcat(i8* %34, i8* %35)
%37 = load i8*, i8** %7
%38 = call i32 @free(i8* %37)
br label %41
39: ; preds = %21
%40 = load i8*, i8** %7
store i8* %40, i8** %6
br label %41
41: ; preds = %39, %24
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42
%44 = load i8*, i8** %6
%45 = call i64 @check_reply(i8* %44, i32* null)
%46 = icmp ne i64 %45, 0
br i1 %46, label %8, label %47
47: ; preds = %43
%48 = load i8*, i8** %6
store i8* %48, i8** %3
br label %49
49: ; preds = %47, %20
%50 = load i8*, i8** %3
ret i8* %50
}

LLVM IR (-O2):
define i8* @srv_readstr(i32 %0, i32 %1) {
br label %3
3: ; preds = %21, %2
%4 = phi i8* [ null, %2 ], [ %22, %21 ]
%5 = tail call i8* @sk_recv(i32 %0, i32 %1)
%6 = icmp eq i8* %5, null
%7 = icmp eq i8* %4, null
br i1 %6, label %8, label %11
8: ; preds = %3
br i1 %7, label %25, label %9
9: ; preds = %8
%10 = tail call i32 @free(i8* %4)
br label %25
11: ; preds = %3
br i1 %7, label %21, label %12
12: ; preds = %11
%13 = tail call i64 @strlen(i8* %4)
%14 = tail call i64 @strlen(i8* %5)
%15 = add i64 %13, 1
%16 = add i64 %15, %14
%17 = tail call i64 @realloc(i8* %4, i64 %16)
%18 = inttoptr i64 %17 to i8*
%19 = tail call i32 @strcat(i8* %18, i8* %5)
%20 = tail call i32 @free(i8* %5)
br label %21
21: ; preds = %11, %12
%22 = phi i8* [ %18, %12 ], [ %5, %11 ]
%23 = tail call i64 @check_reply(i8* %22, i32* null)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %3
25: ; preds = %21, %8, %9
%26 = phi i8* [ null, %9 ], [ null, %8 ], [ %22, %21 ]
ret i8* %26
}