#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*/ temp_buffer;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int htxd_receive_bytes(int, char *, int);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
char *htxd_receive_command(int new_fd) {
  int result;
  char *command_details_buffer = NULL;
  char temp_buffer[20];
  int command_length;
  memset(temp_buffer, 0, sizeof(temp_buffer));
  result = htxd_receive_bytes(new_fd, temp_buffer, 10);
  if (result == -1) {
    return NULL;
  }
  temp_buffer[10] = '\0';
  command_length = atoi(temp_buffer);
  command_details_buffer = malloc(command_length + 10);
  if (command_details_buffer == NULL) {
    printf("[DEBUG] : malloc() failed while allocating command_details_buffer");
    fflush(stdout);
    return NULL;
  }
  memset(command_details_buffer, 0, command_length + 10);
  result = htxd_receive_bytes(new_fd, command_details_buffer, command_length);
  if (result == -1) {
    return NULL;
  }
  command_details_buffer[command_length] = '\0';
  return command_details_buffer;
}


x86 assembly (clang, -O0):
<htxd_receive_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1030 <htxd_receive_bytes@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11ae <htxd_receive_command+0x4e>
movq   $0x0,-0x8(%rbp)
jmp    124d <htxd_receive_command+0xed>
movb   $0x0,-0x26(%rbp)
lea    -0x30(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%edi
add    $0xa,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ff <htxd_receive_command+0x9f>
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
movq   $0x0,-0x8(%rbp)
jmp    124d <htxd_receive_command+0xed>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%edx
add    $0xa,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <htxd_receive_bytes@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1239 <htxd_receive_command+0xd9>
movq   $0x0,-0x8(%rbp)
jmp    124d <htxd_receive_command+0xed>
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<htxd_receive_command>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    %rsp,%rbp
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0xa,%edx
call   1030 <htxd_receive_bytes@plt>
cmp    $0xffffffff,%eax
je     11fd <htxd_receive_command+0x9d>
movb   $0x0,0xa(%rsp)
mov    %rsp,%rdi
call   1080 <atoi@plt>
mov    %eax,%r15d
lea    0xa(%r15),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e1 <htxd_receive_command+0x81>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <htxd_receive_bytes@plt>
cmp    $0xffffffff,%eax
je     11fb <htxd_receive_command+0x9b>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
jmp    11fd <htxd_receive_command+0x9d>
lea    0xe18(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [66 x i8] c"[DEBUG] : malloc() failed while allocating command_details_buffer\00"
@stdout = global i32 0
define i8* @htxd_receive_command(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [20 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i8* null, i8** %5
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 20)
%10 = load i32, i32* %3
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%12 = call i32 @htxd_receive_bytes(i32 %10, i8* %11, i32 10)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %1
store i8* null, i8** %2
br label %47
16: ; preds = %1
%17 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 10
store i8 0, i8* %17
%18 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = add nsw i32 %20, 10
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %5
%23 = load i8*, i8** %5
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = call i32 @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* @stdout
%28 = call i32 @fflush(i32 %27)
store i8* null, i8** %2
br label %47
29: ; preds = %16
%30 = load i8*, i8** %5
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 10
%33 = call i32 @memset(i8* %30, i32 0, i32 %32)
%34 = load i32, i32* %3
%35 = load i8*, i8** %5
%36 = load i32, i32* %7
%37 = call i32 @htxd_receive_bytes(i32 %34, i8* %35, i32 %36)
store i32 %37, i32* %4
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %29
store i8* null, i8** %2
br label %47
41: ; preds = %29
%42 = load i8*, i8** %5
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i8*, i8** %5
store i8* %46, i8** %2
br label %47
47: ; preds = %41, %40, %25, %15
%48 = load i8*, i8** %2
ret i8* %48
}

LLVM IR (-O2):
@.str = [66 x i8] c"[DEBUG] : malloc() failed while allocating command_details_buffer\00"
@stdout = global i32 0
define i8* @htxd_receive_command(i32 %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i32 0, i32 20)
%5 = call i32 @htxd_receive_bytes(i32 %0, i8* %3, i32 10)
%6 = icmp eq i32 %5, -1
br i1 %6, label %24, label %7
7: ; preds = %1
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 10
store i8 0, i8* %8
%9 = call i32 @atoi(i8* %3)
%10 = add nsw i32 %9, 10
%11 = call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %7
%14 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
br label %24
17: ; preds = %7
%18 = call i32 @memset(i8* %11, i32 0, i32 %10)
%19 = call i32 @htxd_receive_bytes(i32 %0, i8* %11, i32 %9)
%20 = icmp eq i32 %19, -1
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = sext i32 %9 to i64
%23 = getelementptr inbounds i8, i8* %11, i64 %22
store i8 0, i8* %23
br label %24
24: ; preds = %17, %1, %21, %13
%25 = phi i8* [ null, %13 ], [ %11, %21 ], [ null, %1 ], [ null, %17 ]
ret i8* %25
}