#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 EXIT_FAILURE;
scalar_t__ EXIT_SUCCESS;
int /*<<< orphan*/ FIONREAD;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ close(int);
scalar_t__ form_response(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ print_err(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, int);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
int reply(int socket_data) {
  int len = 0;
  ioctl(socket_data, FIONREAD, &len);
  if (len <= 0)
    return EXIT_FAILURE;
  char *buffer = NULL;
  if ((buffer = (char *)malloc(sizeof(char) * len)) == NULL)
    return EXIT_FAILURE;
  read(socket_data, buffer, len);
  char *response = NULL;
  if ((response = (char *)calloc(2048, sizeof(char))) == NULL)
    return EXIT_FAILURE;
  if (form_response(buffer, response) != EXIT_SUCCESS)
    return EXIT_FAILURE;
  if (write(socket_data, response, strlen(response) + 1) < 0) {
    print_err("Server PID(%d): Unable to send data back to client", getpid());
    return EXIT_FAILURE;
  }
  free(response);
  close(socket_data);
  free(buffer);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<reply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rdx
call   1080 <ioctl@plt>
cmpl   $0x0,-0xc(%rbp)
jg     11f2 <reply+0x42>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <reply+0x16c>
movq   $0x0,-0x18(%rbp)
movslq -0xc(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1228 <reply+0x78>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <reply+0x16c>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10a0 <read@plt>
movq   $0x0,-0x20(%rbp)
mov    $0x800,%edi
mov    $0x1,%esi
call   10b0 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    126d <reply+0xbd>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <reply+0x16c>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10d0 <form_response@plt>
mov    0x2d4f(%rip),%rcx
cmp    (%rcx),%rax
je     129b <reply+0xeb>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <reply+0x16c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1050 <write@plt>
cmp    $0x0,%rax
jge    12f5 <reply+0x145>
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %eax,%esi
lea    0xd21(%rip),%rdi
call   1040 <print_err@plt>
mov    0x2ced(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    131c <reply+0x16c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
call   1090 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reply>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
movl   $0x0,0x4(%rsp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%rdx
call   1080 <ioctl@plt>
mov    0x4(%rsp),%edi
test   %edi,%edi
jle    127c <reply+0xcc>
call   10c0 <malloc@plt>
test   %rax,%rax
je     127c <reply+0xcc>
mov    %rax,%r14
mov    0x4(%rsp),%edx
mov    %ebx,%edi
mov    %rax,%rsi
call   10a0 <read@plt>
mov    $0x800,%edi
mov    $0x1,%esi
call   10b0 <calloc@plt>
test   %rax,%rax
je     127c <reply+0xcc>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
call   10d0 <form_response@plt>
mov    0x2da7(%rip),%r12
cmp    (%r12),%rax
jne    127c <reply+0xcc>
mov    %r15,%rdi
call   1070 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r15,%rsi
call   1050 <write@plt>
test   %rax,%rax
js     1267 <reply+0xb7>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    (%r12),%eax
jmp    1285 <reply+0xd5>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xd8b(%rip),%rdi
mov    %eax,%esi
call   1040 <print_err@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@FIONREAD = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i64 0
@.str = [51 x i8] c"Server PID(%d): Unable to send data back to client\00"
define i32 @reply(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
store i32 %0, i32* %3
store i32 0, i32* %4
%7 = load i32, i32* %3
%8 = load i32, i32* @FIONREAD
%9 = call i32 @ioctl(i32 %7, i32 %8, i32* %4)
%10 = load i32, i32* %4
%11 = icmp sle i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = load i32, i32* @EXIT_FAILURE
store i32 %13, i32* %2
br label %63
14: ; preds = %1
store i8* null, i8** %5
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = mul i64 1, %16
%18 = trunc i64 %17 to i32
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %5
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = load i32, i32* @EXIT_FAILURE
store i32 %23, i32* %2
br label %63
24: ; preds = %14
%25 = load i32, i32* %3
%26 = load i8*, i8** %5
%27 = load i32, i32* %4
%28 = call i32 @read(i32 %25, i8* %26, i32 %27)
store i8* null, i8** %6
%29 = call i64 @calloc(i32 2048, i32 1)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %6
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = load i32, i32* @EXIT_FAILURE
store i32 %33, i32* %2
br label %63
34: ; preds = %24
%35 = load i8*, i8** %5
%36 = load i8*, i8** %6
%37 = call i64 @form_response(i8* %35, i8* %36)
%38 = load i64, i64* @EXIT_SUCCESS
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = load i32, i32* @EXIT_FAILURE
store i32 %41, i32* %2
br label %63
42: ; preds = %34
%43 = load i32, i32* %3
%44 = load i8*, i8** %6
%45 = load i8*, i8** %6
%46 = call i64 @strlen(i8* %45)
%47 = add nsw i64 %46, 1
%48 = call i64 @write(i32 %43, i8* %44, i64 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %42
%51 = call i32 (...) @getpid()
%52 = call i32 @print_err(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %51)
%53 = load i32, i32* @EXIT_FAILURE
store i32 %53, i32* %2
br label %63
54: ; preds = %42
%55 = load i8*, i8** %6
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %3
%58 = call i32 @close(i32 %57)
%59 = load i8*, i8** %5
%60 = call i32 @free(i8* %59)
%61 = load i64, i64* @EXIT_SUCCESS
%62 = trunc i64 %61 to i32
store i32 %62, i32* %2
br label %63
63: ; preds = %54, %50, %40, %32, %22, %12
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
@FIONREAD = global i32 0
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i64 0
@.str = [51 x i8] c"Server PID(%d): Unable to send data back to client\00"
define i32 @reply(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = load i32, i32* @FIONREAD
%5 = call i32 @ioctl(i32 %0, i32 %4, i32* %2)
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load i32, i32* @EXIT_FAILURE
br label %45
10: ; preds = %1
%11 = call i64 @malloc(i32 %6)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = load i32, i32* @EXIT_FAILURE
br label %45
16: ; preds = %10
%17 = load i32, i32* %2
%18 = call i32 @read(i32 %0, i8* %12, i32 %17)
%19 = call i64 @calloc(i32 2048, i32 1)
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i64 %19, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = load i32, i32* @EXIT_FAILURE
br label %45
24: ; preds = %16
%25 = call i64 @form_response(i8* %12, i8* %20)
%26 = load i64, i64* @EXIT_SUCCESS
%27 = icmp eq i64 %25, %26
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = load i32, i32* @EXIT_FAILURE
br label %45
30: ; preds = %24
%31 = call i64 @strlen(i8* %20)
%32 = add nsw i64 %31, 1
%33 = call i64 @write(i32 %0, i8* %20, i64 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %30
%36 = call i32 (...) @getpid()
%37 = call i32 @print_err(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @EXIT_FAILURE
br label %45
39: ; preds = %30
%40 = call i32 @free(i8* %20)
%41 = call i32 @close(i32 %0)
%42 = call i32 @free(i8* %12)
%43 = load i64, i64* @EXIT_SUCCESS
%44 = trunc i64 %43 to i32
br label %45
45: ; preds = %14, %39, %35, %28, %22, %8
%46 = phi i32 [ %9, %8 ], [ %15, %14 ], [ %23, %22 ], [ %29, %28 ], [ %38, %35 ], [ %44, %39 ]
ret i32 %46
}