#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 *);
scalar_t__ malloc(int);
char *sk_recv(int sock, int ssl) {
  char *buffer = (char *)malloc(1024 + 1);
  int ret = cw_read(sock, buffer, 1024, ssl);
  if (ret <= 0) {
    free(buffer);
    return NULL;
  }
  buffer[ret] = 0;
  return buffer;
}


x86 assembly (clang, -O0):
<sk_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    $0x401,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%ecx
mov    $0x400,%edx
call   1050 <cw_read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     1183 <sk_recv+0x53>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1197 <sk_recv+0x67>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sk_recv>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    $0x401,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    $0x400,%edx
mov    %r14d,%ecx
call   1050 <cw_read@plt>
test   %eax,%eax
jle    1164 <sk_recv+0x34>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
jmp    116e <sk_recv+0x3e>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @sk_recv(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = call i64 @malloc(i32 1025)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %6
%10 = load i32, i32* %4
%11 = load i8*, i8** %6
%12 = load i32, i32* %5
%13 = call i32 @cw_read(i32 %10, i8* %11, i32 1024, i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i8*, i8** %6
%18 = call i32 @free(i8* %17)
store i8* null, i8** %3
br label %25
19: ; preds = %2
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
%24 = load i8*, i8** %6
store i8* %24, i8** %3
br label %25
25: ; preds = %19, %16
%26 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
define i8* @sk_recv(i32 %0, i32 %1) {
%3 = tail call i64 @malloc(i32 1025)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @cw_read(i32 %0, i8* %4, i32 1024, i32 %1)
%6 = icmp slt i32 %5, 1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @free(i8* %4)
br label %12
9: ; preds = %2
%10 = zext i32 %5 to i64
%11 = getelementptr inbounds i8, i8* %4, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %9, %7
%13 = phi i8* [ null, %7 ], [ %4, %9 ]
ret i8* %13
}