#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*/ handle_message(int, char *);
int recv(int, char **, int, int /*<<< orphan*/);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *xmalloc(int);
char *xrealloc(char *, int);
void server_connection(int socketfd) {
  char buf[128];
  int len;
  char *msg = xmalloc(sizeof(char));
  strcpy(msg, "");
  while (len = recv(socketfd, &buf, 128, 0), len > 0) {
    if (len == -1) {
      break;
    }
    char *newline = strchr(buf, '\n');
    if (newline == NULL) {
      msg = xrealloc(msg, strlen(msg) + strlen(buf) + 1);
      strncat(msg, buf, strlen(msg) + strlen(buf));
    } else {
      char *rest = &newline[1];
      newline[0] = '\0';
      msg = xrealloc(msg, strlen(msg) + strlen(buf) + 1);
      strncat(msg, buf, strlen(msg) + strlen(buf));
      handle_message(socketfd, msg);
      if (strlen(rest) > 1) {
        msg = xrealloc(msg, strlen(rest) + 1);
        strcpy(msg, rest);
      } else {
        msg = xrealloc(msg, sizeof(char));
        strcpy(msg, "");
      }
    }
  }
}


x86 assembly (clang, -O0):
<server_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x1,%edi
call   1080 <xmalloc@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jle    1415 <server_connection+0x295>
cmpl   $0xffffffff,-0x94(%rbp)
jne    11ed <server_connection+0x6d>
jmp    1415 <server_connection+0x295>
lea    -0x90(%rbp),%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
jne    12b7 <server_connection+0x137>
mov    -0xa0(%rbp),%rax
mov    %rax,-0xd0(%rbp)
mov    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0xd4(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xd4(%rbp),%esi
mov    -0xd0(%rbp),%rdi
add    %eax,%esi
add    $0x1,%esi
call   1070 <xrealloc@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rax
mov    %rax,-0xc0(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xb8(%rbp)
mov    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0xc4(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xc4(%rbp),%edx
mov    -0xc0(%rbp),%rdi
mov    -0xb8(%rbp),%rsi
add    %eax,%edx
call   1090 <strncat@plt>
jmp    1410 <server_connection+0x290>
mov    -0xa8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0xb0(%rbp)
mov    -0xa8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0xa0(%rbp),%rax
mov    %rax,-0xf8(%rbp)
mov    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0xfc(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xfc(%rbp),%esi
mov    -0xf8(%rbp),%rdi
add    %eax,%esi
add    $0x1,%esi
call   1070 <xrealloc@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rax
mov    %rax,-0xe8(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0xec(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xec(%rbp),%edx
mov    -0xe8(%rbp),%rdi
mov    -0xe0(%rbp),%rsi
add    %eax,%edx
call   1090 <strncat@plt>
mov    -0x4(%rbp),%edi
mov    -0xa0(%rbp),%rsi
call   10a0 <handle_message@plt>
mov    -0xb0(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jle    13e0 <server_connection+0x260>
mov    -0xa0(%rbp),%rax
mov    %rax,-0x108(%rbp)
mov    -0xb0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x108(%rbp),%rdi
mov    %eax,%esi
add    $0x1,%esi
call   1070 <xrealloc@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rdi
mov    -0xb0(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    140b <server_connection+0x28b>
mov    -0xa0(%rbp),%rdi
mov    $0x1,%esi
call   1070 <xrealloc@plt>
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rdi
lea    0xbfa(%rip),%rsi
call   1040 <strcpy@plt>
jmp    1410 <server_connection+0x290>
jmp    11b2 <server_connection+0x32>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_connection>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %edi,%r15d
mov    $0x1,%edi
call   1080 <xmalloc@plt>
mov    %rax,%r13
lea    0xe58(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1300 <server_connection+0x180>
mov    %rsp,%r12
lea    0xe2c(%rip),%r14
jmp    123e <server_connection+0xbe>
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%esi
add    $0x1,%esi
mov    %r13,%rdi
call   1070 <xrealloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %eax,%ebx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1090 <strncat@plt>
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1300 <server_connection+0x180>
mov    %r12,%rdi
mov    $0xa,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11e0 <server_connection+0x60>
mov    %rax,%rbx
add    $0x1,%rbx
movb   $0x0,(%rax)
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    (%rax,%rbp,1),%esi
add    $0x1,%esi
mov    %r13,%rdi
call   1070 <xrealloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1050 <strlen@plt>
add    %eax,%ebp
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1090 <strncat@plt>
mov    %r15d,%edi
mov    %r13,%rsi
call   10a0 <handle_message@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x2,%eax
jl     12e0 <server_connection+0x160>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%esi
mov    %r13,%rdi
call   1070 <xrealloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbx,%rsi
jmp    12f6 <server_connection+0x176>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
mov    $0x1,%esi
call   1070 <xrealloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    1224 <server_connection+0xa4>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define void @server_connection(i32 %0) {
%2 = alloca i32
%3 = alloca [128 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i32 %0, i32* %2
%8 = call i8* @xmalloc(i32 1)
store i8* %8, i8** %5
%9 = load i8*, i8** %5
%10 = call i32 @strcpy(i8* %9, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %85, %1
%12 = load i32, i32* %2
%13 = bitcast [128 x i8]* %3 to i8**
%14 = call i32 @recv(i32 %12, i8** %13, i32 128, i32 0)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %86
17: ; preds = %11
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %17
br label %86
21: ; preds = %17
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%23 = call i8* @strchr(i8* %22, i8 signext 10)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %43
26: ; preds = %21
%27 = load i8*, i8** %5
%28 = load i8*, i8** %5
%29 = call i32 @strlen(i8* %28)
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = add nsw i32 %29, %31
%33 = add nsw i32 %32, 1
%34 = call i8* @xrealloc(i8* %27, i32 %33)
store i8* %34, i8** %5
%35 = load i8*, i8** %5
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%37 = load i8*, i8** %5
%38 = call i32 @strlen(i8* %37)
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%40 = call i32 @strlen(i8* %39)
%41 = add nsw i32 %38, %40
%42 = call i32 @strncat(i8* %35, i8* %36, i32 %41)
br label %85
43: ; preds = %21
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i64 1
store i8* %45, i8** %7
%46 = load i8*, i8** %6
%47 = getelementptr inbounds i8, i8* %46, i64 0
store i8 0, i8* %47
%48 = load i8*, i8** %5
%49 = load i8*, i8** %5
%50 = call i32 @strlen(i8* %49)
%51 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%52 = call i32 @strlen(i8* %51)
%53 = add nsw i32 %50, %52
%54 = add nsw i32 %53, 1
%55 = call i8* @xrealloc(i8* %48, i32 %54)
store i8* %55, i8** %5
%56 = load i8*, i8** %5
%57 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%58 = load i8*, i8** %5
%59 = call i32 @strlen(i8* %58)
%60 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%61 = call i32 @strlen(i8* %60)
%62 = add nsw i32 %59, %61
%63 = call i32 @strncat(i8* %56, i8* %57, i32 %62)
%64 = load i32, i32* %2
%65 = load i8*, i8** %5
%66 = call i32 @handle_message(i32 %64, i8* %65)
%67 = load i8*, i8** %7
%68 = call i32 @strlen(i8* %67)
%69 = icmp sgt i32 %68, 1
br i1 %69, label %70, label %79
70: ; preds = %43
%71 = load i8*, i8** %5
%72 = load i8*, i8** %7
%73 = call i32 @strlen(i8* %72)
%74 = add nsw i32 %73, 1
%75 = call i8* @xrealloc(i8* %71, i32 %74)
store i8* %75, i8** %5
%76 = load i8*, i8** %5
%77 = load i8*, i8** %7
%78 = call i32 @strcpy(i8* %76, i8* %77)
br label %84
79: ; preds = %43
%80 = load i8*, i8** %5
%81 = call i8* @xrealloc(i8* %80, i32 1)
store i8* %81, i8** %5
%82 = load i8*, i8** %5
%83 = call i32 @strcpy(i8* %82, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %84
84: ; preds = %79, %70
br label %85
85: ; preds = %84, %26
br label %11
86: ; preds = %20, %11
ret void
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define void @server_connection(i32 %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @xmalloc(i32 1)
%5 = tail call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%6 = bitcast [128 x i8]* %2 to i8**
%7 = call i32 @recv(i32 %0, i8** %6, i32 128, i32 0)
%8 = icmp slt i32 %7, 1
br i1 %8, label %49, label %9
9: ; preds = %1, %45
%10 = phi i8* [ %46, %45 ], [ %4, %1 ]
%11 = call i8* @strchr(i8* %3, i8 signext 10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %23
13: ; preds = %9
%14 = call i32 @strlen(i8* %10)
%15 = call i32 @strlen(i8* %3)
%16 = add i32 %14, 1
%17 = add i32 %16, %15
%18 = call i8* @xrealloc(i8* %10, i32 %17)
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @strlen(i8* %3)
%21 = add nsw i32 %20, %19
%22 = call i32 @strncat(i8* %18, i8* %3, i32 %21)
br label %45
23: ; preds = %9
%24 = getelementptr inbounds i8, i8* %11, i64 1
store i8 0, i8* %11
%25 = call i32 @strlen(i8* %10)
%26 = call i32 @strlen(i8* %3)
%27 = add i32 %25, 1
%28 = add i32 %27, %26
%29 = call i8* @xrealloc(i8* %10, i32 %28)
%30 = call i32 @strlen(i8* %29)
%31 = call i32 @strlen(i8* %3)
%32 = add nsw i32 %31, %30
%33 = call i32 @strncat(i8* %29, i8* %3, i32 %32)
%34 = call i32 @handle_message(i32 %0, i8* %29)
%35 = call i32 @strlen(i8* %24)
%36 = icmp sgt i32 %35, 1
br i1 %36, label %37, label %42
37: ; preds = %23
%38 = call i32 @strlen(i8* %24)
%39 = add nsw i32 %38, 1
%40 = call i8* @xrealloc(i8* %29, i32 %39)
%41 = call i32 @strcpy(i8* %40, i8* %24)
br label %45
42: ; preds = %23
%43 = call i8* @xrealloc(i8* %29, i32 1)
%44 = call i32 @strcpy(i8* %43, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %37, %42, %13
%46 = phi i8* [ %18, %13 ], [ %40, %37 ], [ %43, %42 ]
%47 = call i32 @recv(i32 %0, i8** %6, i32 128, i32 0)
%48 = icmp slt i32 %47, 1
br i1 %48, label %49, label %9
49: ; preds = %45, %1
ret void
}