#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 /*<<< orphan*/ free(char *);
int /*<<< orphan*/ generate_request(int /*<<< orphan*/, size_t *, char *);
size_t getline(char **, size_t *, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int receive_response(int, size_t *, char *);
int send_request(int, size_t, char *);
int /*<<< orphan*/ stdin;
int handle_connect(int sfd, int run, int random) {
  size_t size = 128;
  size_t size_request, size_response;
  char *inputs;
  char *request = malloc(128);
  char response[128];
  while (run--) {
    if (random == 0) {
      size_request = getline(&request, &size, stdin);
      if (request[size_request - 1] == '\n')
        request[--size_request] = '\0';
    } else
      generate_request(0, &size_request, request);
    if (send_request(sfd, size_request, request) == -1)
      return -1;
    request[size_request] = '\0';
    printf("[LOG] send: %s\n", request);
    if (receive_response(sfd, &size_response, response) == -1)
      return -1;
    response[size_response] = '\0';
    printf("[LOG] receive: %s\n", response);
  }
  free(request);
  return 0;
}


x86 assembly (clang, -O0):
<handle_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movq   $0x80,-0x18(%rbp)
mov    $0x80,%edi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x0,%eax
je     12a4 <handle_connect+0x134>
cmpl   $0x0,-0x10(%rbp)
jne    1208 <handle_connect+0x98>
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1090 <getline@plt>
mov    %rax,-0x20(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1203 <handle_connect+0x93>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0xffffffffffffffff,%rdx
mov    %rdx,-0x20(%rbp)
movb   $0x0,-0x1(%rax,%rcx,1)
jmp    1217 <handle_connect+0xa7>
mov    -0x38(%rbp),%rdx
xor    %edi,%edi
lea    -0x20(%rbp),%rsi
call   1050 <generate_request@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1070 <send_request@plt>
cmp    $0xffffffff,%eax
jne    123c <handle_connect+0xcc>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <handle_connect+0x144>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rsi
lea    0xdad(%rip),%rdi
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0xc0(%rbp),%rdx
lea    -0x28(%rbp),%rsi
call   1030 <receive_response@plt>
cmp    $0xffffffff,%eax
jne    1280 <handle_connect+0x110>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b4 <handle_connect+0x144>
mov    -0x28(%rbp),%rax
movb   $0x0,-0xc0(%rbp,%rax,1)
lea    -0xc0(%rbp),%rsi
lea    0xd76(%rip),%rdi
call   1060 <printf@plt>
jmp    119a <handle_connect+0x2a>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
movq   $0x80,0x18(%rsp)
mov    $0x80,%edi
call   1080 <malloc@plt>
mov    %rax,(%rsp)
test   %ebp,%ebp
je     128c <handle_connect+0x11c>
lea    0x10(%rsp),%r15
lea    0x20(%rsp),%r12
mov    $0xffffffff,%r13d
nopw   0x0(%rax,%rax,1)
test   %r14d,%r14d
je     11e0 <handle_connect+0x70>
mov    (%rsp),%rdx
xor    %edi,%edi
lea    0x8(%rsp),%rsi
call   1050 <generate_request@plt>
jmp    1220 <handle_connect+0xb0>
nopw   0x0(%rax,%rax,1)
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
lea    0x18(%rsp),%rsi
call   1090 <getline@plt>
mov    %rax,0x8(%rsp)
mov    (%rsp),%rcx
cmpb   $0xa,-0x1(%rcx,%rax,1)
jne    1220 <handle_connect+0xb0>
add    $0xffffffffffffffff,%rax
mov    %rax,0x8(%rsp)
movb   $0x0,(%rcx,%rax,1)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rsp),%rsi
mov    (%rsp),%rdx
mov    %ebx,%edi
call   1070 <send_request@plt>
cmp    $0xffffffff,%eax
je     1297 <handle_connect+0x127>
mov    (%rsp),%rax
mov    0x8(%rsp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    (%rsp),%rsi
lea    0xdb3(%rip),%rdi
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <receive_response@plt>
cmp    $0xffffffff,%eax
je     1297 <handle_connect+0x127>
add    $0xffffffff,%ebp
mov    0x10(%rsp),%rax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xd98(%rip),%rdi
mov    %r12,%rsi
call   1060 <printf@plt>
test   %ebp,%ebp
jne    11c0 <handle_connect+0x50>
mov    (%rsp),%rax
mov    %rax,%rdi
call   1040 <free@plt>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [16 x i8] c"[LOG] send: %s\0A\00"
@.str.1 = [19 x i8] c"[LOG] receive: %s\0A\00"
define i32 @handle_connect(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca [128 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i64 128, i64* %8
%14 = call i8* @malloc(i32 128)
store i8* %14, i8** %12
br label %15
15: ; preds = %59, %3
%16 = load i32, i32* %6
%17 = add nsw i32 %16, -1
store i32 %17, i32* %6
%18 = icmp ne i32 %16, 0
br i1 %18, label %19, label %64
19: ; preds = %15
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %38
22: ; preds = %19
%23 = load i32, i32* @stdin
%24 = call i64 @getline(i8** %12, i64* %8, i32 %23)
store i64 %24, i64* %9
%25 = load i8*, i8** %12
%26 = load i64, i64* %9
%27 = sub i64 %26, 1
%28 = getelementptr inbounds i8, i8* %25, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 10
br i1 %31, label %32, label %37
32: ; preds = %22
%33 = load i8*, i8** %12
%34 = load i64, i64* %9
%35 = add i64 %34, -1
store i64 %35, i64* %9
%36 = getelementptr inbounds i8, i8* %33, i64 %35
store i8 0, i8* %36
br label %37
37: ; preds = %32, %22
br label %41
38: ; preds = %19
%39 = load i8*, i8** %12
%40 = call i32 @generate_request(i32 0, i64* %9, i8* %39)
br label %41
41: ; preds = %38, %37
%42 = load i32, i32* %5
%43 = load i64, i64* %9
%44 = load i8*, i8** %12
%45 = call i32 @send_request(i32 %42, i64 %43, i8* %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %41
store i32 -1, i32* %4
br label %67
48: ; preds = %41
%49 = load i8*, i8** %12
%50 = load i64, i64* %9
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %12
%53 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %52)
%54 = load i32, i32* %5
%55 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%56 = call i32 @receive_response(i32 %54, i64* %10, i8* %55)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %59
58: ; preds = %48
store i32 -1, i32* %4
br label %67
59: ; preds = %48
%60 = load i64, i64* %10
%61 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 %60
store i8 0, i8* %61
%62 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%63 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %62)
br label %15
64: ; preds = %15
%65 = load i8*, i8** %12
%66 = call i32 @free(i8* %65)
store i32 0, i32* %4
br label %67
67: ; preds = %64, %58, %47
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [16 x i8] c"[LOG] send: %s\0A\00"
@.str.1 = [19 x i8] c"[LOG] receive: %s\0A\00"
define i32 @handle_connect(i32 %0, i32 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca [128 x i8]
%9 = bitcast i64* %4 to i8*
store i64 128, i64* %4
%10 = bitcast i64* %5 to i8*
%11 = bitcast i64* %6 to i8*
%12 = bitcast i8** %7 to i8*
%13 = tail call dereferenceable_or_null(128) i8* @malloc(i32 128)
store i8* %13, i8** %7
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%15 = icmp eq i32 %1, 0
br i1 %15, label %53, label %16
16: ; preds = %3
%17 = icmp eq i32 %2, 0
br label %18
18: ; preds = %16, %46
%19 = phi i32 [ %1, %16 ], [ %20, %46 ]
%20 = add nsw i32 %19, -1
br i1 %17, label %21, label %30
21: ; preds = %18
%22 = load i32, i32* @stdin
%23 = call i64 @getline(i8** %7, i64* %4, i32 %22)
store i64 %23, i64* %5
%24 = load i8*, i8** %7
%25 = add i64 %23, -1
%26 = getelementptr inbounds i8, i8* %24, i64 %25
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 10
br i1 %28, label %29, label %33
29: ; preds = %21
store i64 %25, i64* %5
store i8 0, i8* %26
br label %33
30: ; preds = %18
%31 = load i8*, i8** %7
%32 = call i32 @generate_request(i32 0, i64* %5, i8* %31)
br label %33
33: ; preds = %21, %29, %30
%34 = load i64, i64* %5
%35 = load i8*, i8** %7
%36 = call i32 @send_request(i32 %0, i64 %34, i8* %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %56, label %38
38: ; preds = %33
%39 = load i8*, i8** %7
%40 = load i64, i64* %5
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 0, i8* %41
%42 = load i8*, i8** %7
%43 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %42)
%44 = call i32 @receive_response(i32 %0, i64* %6, i8* %14)
%45 = icmp eq i32 %44, -1
br i1 %45, label %56, label %46
46: ; preds = %38
%47 = load i64, i64* %6
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 %47
store i8 0, i8* %48
%49 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%50 = icmp eq i32 %20, 0
br i1 %50, label %51, label %18
51: ; preds = %46
%52 = load i8*, i8** %7
br label %53
53: ; preds = %51, %3
%54 = phi i8* [ %52, %51 ], [ %13, %3 ]
%55 = call i32 @free(i8* %54)
br label %56
56: ; preds = %38, %33, %53
%57 = phi i32 [ 0, %53 ], [ -1, %33 ], [ -1, %38 ]
ret i32 %57
}