#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*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int read(int, char *, int);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
char *strdup(char *);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
char *strndup(char *, int);
void handleSocket(int socket, char **buffer, int *bufferlen,
                  void (*callback)(char *), void (*callbackClose)(int)) {
  char buf[1024 + 1];
  int readcount = read(socket, buf, 1024);
  if (readcount < 0) {
    fprintf(stderr, "Client Socket Error: %s\n", strerror(errno));
    exit(1);
  } else if (readcount == 0) {
    callbackClose(socket);
    return;
  }
  buf[readcount] = '\0';
  *bufferlen += readcount;
  *buffer = (char *)realloc(*buffer, sizeof(char) * *bufferlen);
  strcat(*buffer, buf);
  char *newline;
  char *newcontent;
  char *content = *buffer;
  while ((newline = strchr(content, '\n')) && newline != NULL) {
    newcontent = strndup(content, newline - content + 1);
    if (newcontent == NULL) {
      fprintf(stderr, "Speicherallokationsfehler\n");
      exit(1);
    }
    callback(newcontent);
    free(newcontent);
    content = newline;
    content++;
  }
  newcontent = strdup(content);
  if (newcontent == NULL) {
    fprintf(stderr, "Speicherallokationsfehler\n");
    exit(1);
  }
  *bufferlen = strlen(newcontent) + 1;
  free(*buffer);
  *buffer = newcontent;
}


x86 assembly (clang, -O0):
<handleSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0x0,-0x434(%rbp)
jge    1233 <handleSocket+0x83>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x454(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x454(%rbp),%edi
mov    %rax,%rdx
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x434(%rbp)
jne    124e <handleSocket+0x9e>
mov    -0x28(%rbp),%rax
mov    -0x4(%rbp),%edi
call   *%rax
jmp    140f <handleSocket+0x25f>
jmp    1253 <handleSocket+0xa3>
movslq -0x434(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
mov    -0x434(%rbp),%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1090 <realloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x430(%rbp),%rsi
call   10a0 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x450(%rbp)
mov    -0x450(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,%rcx
mov    %rcx,-0x440(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x455(%rbp)
je     12f2 <handleSocket+0x142>
cmpq   $0x0,-0x440(%rbp)
setne  %al
mov    %al,-0x455(%rbp)
mov    -0x455(%rbp),%al
test   $0x1,%al
jne    1305 <handleSocket+0x155>
jmp    139c <handleSocket+0x1ec>
mov    -0x450(%rbp),%rdi
mov    -0x440(%rbp),%rax
mov    -0x450(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%esi
call   1070 <strndup@plt>
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
jne    135e <handleSocket+0x1ae>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
lea    0xccc(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rax
mov    -0x448(%rbp),%rdi
call   *%rax
mov    -0x448(%rbp),%rdi
call   1030 <free@plt>
mov    -0x440(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x450(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x450(%rbp)
jmp    12b4 <handleSocket+0x104>
mov    -0x450(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x448(%rbp)
cmpq   $0x0,-0x448(%rbp)
jne    13de <handleSocket+0x22e>
mov    0x2c1c(%rip),%rax
mov    (%rax),%edi
lea    0xc4c(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x448(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x448(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handleSocket>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %r8,%r13
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
test   %eax,%eax
js     12e4 <handleSocket+0x134>
je     12ac <handleSocket+0xfc>
mov    %eax,%ecx
movb   $0x0,(%rsp,%rcx,1)
mov    (%r14),%esi
add    %eax,%esi
mov    %esi,(%r14)
mov    (%r15),%rdi
call   1090 <realloc@plt>
mov    %rax,(%r15)
mov    %rsp,%rsi
mov    %rax,%rdi
call   10a0 <strcat@plt>
mov    (%r15),%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1272 <handleSocket+0xc2>
mov    %rax,%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r13d,%esi
sub    %ebx,%esi
add    $0x1,%esi
mov    %rbx,%rdi
call   1070 <strndup@plt>
test   %rax,%rax
je     12c3 <handleSocket+0x113>
mov    %rax,%rbx
mov    %rax,%rdi
call   *%r12
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1230 <handleSocket+0x80>
mov    %rbx,%rdi
call   10c0 <strdup@plt>
test   %rax,%rax
je     12c3 <handleSocket+0x113>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,(%r14)
mov    (%r15),%rdi
call   1030 <free@plt>
mov    %rbx,(%r15)
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %ebx,%edi
mov    %r13,%rax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    *%rax
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    0xd46(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2cf5(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xcfe(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"Client Socket Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [27 x i8] c"Speicherallokationsfehler\0A\00"
define void @handleSocket(i32 %0, i8** %1, i32* %2, void (i8*)* %3, void (i32)* %4) {
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca void (i8*)*
%10 = alloca void (i32)*
%11 = alloca [1025 x i8]
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
store i32 %0, i32* %6
store i8** %1, i8*** %7
store i32* %2, i32** %8
store void (i8*)* %3, void (i8*)** %9
store void (i32)* %4, void (i32)** %10
%16 = load i32, i32* %6
%17 = getelementptr inbounds [1025 x i8], [1025 x i8]* %11, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 1024)
store i32 %18, i32* %12
%19 = load i32, i32* %12
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %5
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %5
%28 = load i32, i32* %12
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = load void (i32)*, void (i32)** %10
%32 = load i32, i32* %6
br label %110
33: ; preds = %27
br label %34
34: ; preds = %33
%35 = load i32, i32* %12
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [1025 x i8], [1025 x i8]* %11, i64 0, i64 %36
store i8 0, i8* %37
%38 = load i32, i32* %12
%39 = load i32*, i32** %8
%40 = load i32, i32* %39
%41 = add nsw i32 %40, %38
store i32 %41, i32* %39
%42 = load i8**, i8*** %7
%43 = load i8*, i8** %42
%44 = load i32*, i32** %8
%45 = load i32, i32* %44
%46 = sext i32 %45 to i64
%47 = mul i64 1, %46
%48 = trunc i64 %47 to i32
%49 = call i64 @realloc(i8* %43, i32 %48)
%50 = inttoptr i64 %49 to i8*
%51 = load i8**, i8*** %7
store i8* %50, i8** %51
%52 = load i8**, i8*** %7
%53 = load i8*, i8** %52
%54 = getelementptr inbounds [1025 x i8], [1025 x i8]* %11, i64 0, i64 0
%55 = call i32 @strcat(i8* %53, i8* %54)
%56 = load i8**, i8*** %7
%57 = load i8*, i8** %56
store i8* %57, i8** %15
br label %58
58: ; preds = %83, %34
%59 = load i8*, i8** %15
%60 = call i8* @strchr(i8* %59, i8 signext 10)
store i8* %60, i8** %13
%61 = icmp ne i8* %60, null
br i1 %61, label %62, label %65
62: ; preds = %58
%63 = load i8*, i8** %13
%64 = icmp ne i8* %63, null
br label %65
65: ; preds = %62, %58
%66 = phi i1 [ false, %58 ], [ %64, %62 ]
br i1 %66, label %67, label %91
67: ; preds = %65
%68 = load i8*, i8** %15
%69 = load i8*, i8** %13
%70 = load i8*, i8** %15
%71 = ptrtoint i8* %69 to i64
%72 = ptrtoint i8* %70 to i64
%73 = sub i64 %71, %72
%74 = add nsw i64 %73, 1
%75 = trunc i64 %74 to i32
%76 = call i8* @strndup(i8* %68, i32 %75)
store i8* %76, i8** %14
%77 = load i8*, i8** %14
%78 = icmp eq i8* %77, null
br i1 %78, label %79, label %83
79: ; preds = %67
%80 = load i32, i32* @stderr
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %67
%84 = load void (i8*)*, void (i8*)** %9
%85 = load i8*, i8** %14
%86 = load i8*, i8** %14
%87 = call i32 @free(i8* %86)
%88 = load i8*, i8** %13
store i8* %88, i8** %15
%89 = load i8*, i8** %15
%90 = getelementptr inbounds i8, i8* %89, i32 1
store i8* %90, i8** %15
br label %58
91: ; preds = %65
%92 = load i8*, i8** %15
%93 = call i8* @strdup(i8* %92)
store i8* %93, i8** %14
%94 = load i8*, i8** %14
%95 = icmp eq i8* %94, null
br i1 %95, label %96, label %100
96: ; preds = %91
%97 = load i32, i32* @stderr
%98 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%99 = call i32 @exit(i32 1)
unreachable
100: ; preds = %91
%101 = load i8*, i8** %14
%102 = call i32 @strlen(i8* %101)
%103 = add nsw i32 %102, 1
%104 = load i32*, i32** %8
store i32 %103, i32* %104
%105 = load i8**, i8*** %7
%106 = load i8*, i8** %105
%107 = call i32 @free(i8* %106)
%108 = load i8*, i8** %14
%109 = load i8**, i8*** %7
store i8* %108, i8** %109
br label %110
110: ; preds = %100, %30
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"Client Socket Error: %s\0A\00"
@errno = global i32 0
@.str.1 = [27 x i8] c"Speicherallokationsfehler\0A\00"
define void @handleSocket(i32 %0, i8** nocapture %1, i32* nocapture %2, void (i8*)* nocapture %3, void (i32)* nocapture %4) {
%6 = alloca [1025 x i8]
%7 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%8 = call i32 @read(i32 %0, i8* %7, i32 1024)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %5
%17 = icmp eq i32 %8, 0
br i1 %17, label %18, label %19
18: ; preds = %16
tail call void %4(i32 %0)
br label %63
19: ; preds = %16
%20 = zext i32 %8 to i64
%21 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 %20
store i8 0, i8* %21
%22 = load i32, i32* %2
%23 = add nsw i32 %22, %8
store i32 %23, i32* %2
%24 = load i8*, i8** %1
%25 = tail call i64 @realloc(i8* %24, i32 %23)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %1
%27 = call i32 @strcat(i8* %26, i8* %7)
%28 = load i8*, i8** %1
%29 = call i8* @strchr(i8* dereferenceable(1) %28, i8 signext 10)
%30 = icmp eq i8* %29, null
br i1 %30, label %50, label %31
31: ; preds = %19, %45
%32 = phi i8* [ %48, %45 ], [ %29, %19 ]
%33 = phi i8* [ %47, %45 ], [ %28, %19 ]
%34 = ptrtoint i8* %32 to i64
%35 = ptrtoint i8* %33 to i64
%36 = sub i64 %34, %35
%37 = trunc i64 %36 to i32
%38 = add i32 %37, 1
%39 = call i8* @strndup(i8* %33, i32 %38)
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %45
41: ; preds = %31
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %31
%46 = call i32 @free(i8* %39)
%47 = getelementptr inbounds i8, i8* %32, i64 1
%48 = call i8* @strchr(i8* dereferenceable(1) %47, i8 signext 10)
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %31
50: ; preds = %45, %19
%51 = phi i8* [ %28, %19 ], [ %47, %45 ]
%52 = call i8* @strdup(i8* %51)
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %50
%59 = call i32 @strlen(i8* %52)
%60 = add nsw i32 %59, 1
store i32 %60, i32* %2
%61 = load i8*, i8** %1
%62 = call i32 @free(i8* %61)
store i8* %52, i8** %1
br label %63
63: ; preds = %58, %18
ret void
}