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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, ...);
int /*<<< orphan*/ strcmp(char *, char *);
int server(int client_socket) {
  while (1) {
    int length;
    char *text;
    if (read(client_socket, &length, sizeof(length)) == 0) {
      return 0;
    }
    text = (char *)malloc(length);
    read(client_socket, text, length);
    printf("%s\n", text);
    if (!strcmp(text, "quit")) {
      free(text);
      return 1;
    }
    free(text);
  }
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <read@plt>
cmp    $0x0,%rax
jne    1184 <server+0x34>
movl   $0x0,-0x4(%rbp)
jmp    11ed <server+0x9d>
mov    -0xc(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1050 <read@plt>
mov    -0x18(%rbp),%rsi
lea    0xe54(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11df <server+0x8f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ed <server+0x9d>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
jmp    115b <server+0xb>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
xor    %ebp,%ebp
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <read@plt>
test   %rax,%rax
je     11f2 <server+0xa2>
lea    0xe83(%rip),%r14
lea    0xe80(%rip),%r15
lea    0xc(%rsp),%r12
nopl   0x0(%rax)
mov    0xc(%rsp),%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    0xc(%rsp),%edx
mov    %ebx,%edi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <read@plt>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <strcmp@plt>
mov    %rbp,%rdi
test   %eax,%eax
je     11e8 <server+0x98>
call   1030 <free@plt>
xor    %ebp,%ebp
mov    $0x4,%edx
mov    %ebx,%edi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <read@plt>
test   %rax,%rax
jne    1190 <server+0x40>
jmp    11f2 <server+0xa2>
call   1030 <free@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [5 x i8] c"quit\00"
define i32 @server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %3
br label %6
6: ; preds = %1, %27
%7 = load i32, i32* %3
%8 = call i64 (i32, ...) @read(i32 %7, i32* %4, i64 4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %6
store i32 0, i32* %2
br label %30
11: ; preds = %6
%12 = load i32, i32* %4
%13 = call i64 @malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %5
%15 = load i32, i32* %3
%16 = load i8*, i8** %5
%17 = load i32, i32* %4
%18 = call i64 (i32, ...) @read(i32 %15, i8* %16, i32 %17)
%19 = load i8*, i8** %5
%20 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %5
%22 = call i32 @strcmp(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp ne i32 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %11
%25 = load i8*, i8** %5
%26 = call i32 @free(i8* %25)
store i32 1, i32* %2
br label %30
27: ; preds = %11
%28 = load i8*, i8** %5
%29 = call i32 @free(i8* %28)
br label %6
30: ; preds = %24, %10
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s\0A\00"
@.str.1 = [5 x i8] c"quit\00"
define i32 @server(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i64 (i32, ...) @read(i32 %0, i32* %2, i64 4)
%5 = icmp eq i64 %4, 0
br i1 %5, label %22, label %6
6: ; preds = %1, %18
%7 = load i32, i32* %2
%8 = call i64 @malloc(i32 %7)
%9 = inttoptr i64 %8 to i8*
%10 = load i32, i32* %2
%11 = call i64 (i32, ...) @read(i32 %0, i8* %9, i32 %10)
%12 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %9)
%13 = call i32 @strcmp(i8* dereferenceable(1) %9, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %6
%16 = inttoptr i64 %8 to i8*
%17 = call i32 @free(i8* %16)
br label %22
18: ; preds = %6
%19 = call i32 @free(i8* %9)
%20 = call i64 (i32, ...) @read(i32 %0, i32* %2, i64 4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %6
22: ; preds = %18, %1, %15
%23 = phi i32 [ 1, %15 ], [ 0, %1 ], [ 0, %18 ]
ret i32 %23
}