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

/* Variables and functions */
int /*<<< orphan*/ PTHREAD_CANCEL_DEFERRED;
int /*<<< orphan*/ addclientthread(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ addusernick(int, char const *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
scalar_t__ pthread_self();
int pthread_setcanceltype(int /*<<< orphan*/, int *);
int read(int, char *, int);
int /*<<< orphan*/ removeclient(int);
int /*<<< orphan*/ sendmessagefrom(int, char *);
int /*<<< orphan*/ sendusercount(int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
char *strtok(char *, char *);
int /*<<< orphan*/ thread_error(int, char *);
int write(int, char *, int);
void *handle_client(void *fd) {
  int n;
  int oldtype;
  int clientfd;
  int nickLen;
  char buffer[256];
  char *token;
  char usernick[256];
  clientfd = (*(int *)fd);
  nickLen = 0;
  n = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
  if (n < 0)
    thread_error(clientfd, "ERROR in setcanceltype");
  addclientthread(clientfd, (pthread_t *)pthread_self());
  sendusercount(clientfd);
  while (1) {
    printf("handle client %d\n", clientfd);
    memset(buffer, 0, 256);
    n = read(clientfd, buffer, 255);
    if (n <= 0) {
      thread_error(clientfd, "Client disconnected");
    }
    if (nickLen == 0) {
      token = strtok(buffer, " ");
      if (token == NULL)
        thread_error(clientfd, "ERROR client protocol");
      if (strcmp(token, "USER") == 0) {
        token = strtok(NULL, " ");
        if (token == NULL)
          thread_error(clientfd, "ERROR reading client message");
        nickLen = strlen(token);
        if (nickLen > 0 && nickLen <= 256) {
          strcpy(usernick, (const char *)token);
          addusernick(clientfd, (const char *)&usernick);
        } else {
          n = write(clientfd, "ERROR", 5);
        }
        if (n < 0) {
          thread_error(clientfd, "ERROR writing to socket");
        }
      }
    } else {
      sendmessagefrom(clientfd, buffer);
    }
  }
  removeclient(clientfd);
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<handle_client>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1100 <pthread_setcanceltype@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    123d <handle_client+0x4d>
mov    -0x14(%rbp),%edi
lea    0xdc8(%rip),%rsi
call   10b0 <thread_error@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x234(%rbp)
mov    $0x0,%al
call   10d0 <pthread_self@plt>
mov    -0x234(%rbp),%edi
mov    %rax,%rsi
call   10e0 <addclientthread@plt>
mov    -0x14(%rbp),%edi
call   1110 <sendusercount@plt>
mov    -0x14(%rbp),%esi
lea    0xdaa(%rip),%rdi
call   1060 <printf@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     12b5 <handle_client+0xc5>
mov    -0x14(%rbp),%edi
lea    0xd79(%rip),%rsi
call   10b0 <thread_error@plt>
cmpl   $0x0,-0x18(%rbp)
jne    13ce <handle_client+0x1de>
lea    -0x120(%rbp),%rdi
lea    0xd70(%rip),%rsi
call   10f0 <strtok@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    12f6 <handle_client+0x106>
mov    -0x14(%rbp),%edi
lea    0xd4e(%rip),%rsi
call   10b0 <thread_error@plt>
mov    -0x128(%rbp),%rdi
lea    0xd51(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    13c9 <handle_client+0x1d9>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd1f(%rip),%rsi
call   10f0 <strtok@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    1347 <handle_client+0x157>
mov    -0x14(%rbp),%edi
lea    0xd18(%rip),%rsi
call   10b0 <thread_error@plt>
mov    -0x128(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1394 <handle_client+0x1a4>
cmpl   $0x100,-0x18(%rbp)
jg     1394 <handle_client+0x1a4>
lea    -0x230(%rbp),%rdi
mov    -0x128(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
call   10c0 <addusernick@plt>
jmp    13ab <handle_client+0x1bb>
mov    -0x14(%rbp),%edi
lea    0xcd9(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    13c4 <handle_client+0x1d4>
mov    -0x14(%rbp),%edi
lea    0xcbe(%rip),%rsi
call   10b0 <thread_error@plt>
jmp    13c9 <handle_client+0x1d9>
jmp    13dd <handle_client+0x1ed>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
call   10a0 <sendmessagefrom@plt>
jmp    1263 <handle_client+0x73>

x86 assembly (clang, -O2):
<handle_client>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    (%rdi),%ebx
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
call   1100 <pthread_setcanceltype@plt>
test   %eax,%eax
jns    1228 <handle_client+0x38>
lea    0xddf(%rip),%rsi
mov    %ebx,%edi
call   10b0 <thread_error@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   10d0 <pthread_self@plt>
mov    %ebx,%edi
mov    %rax,%rsi
call   10e0 <addclientthread@plt>
mov    %ebx,%edi
call   1110 <sendusercount@plt>
lea    0xdce(%rip),%r14
lea    0x110(%rsp),%r15
lea    0xdd1(%rip),%r12
jmp    126e <handle_client+0x7e>
nopw   0x0(%rax,%rax,1)
test   %ebp,%ebp
je     12a9 <handle_client+0xb9>
mov    %ebx,%edi
mov    %r15,%rsi
call   10a0 <sendmessagefrom@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,%r13d
test   %eax,%eax
jg     1260 <handle_client+0x70>
mov    %ebx,%edi
mov    %r12,%rsi
call   10b0 <thread_error@plt>
jmp    1260 <handle_client+0x70>
mov    %r15,%rdi
lea    0xd8a(%rip),%rsi
call   10f0 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    12ce <handle_client+0xde>
mov    %ebx,%edi
lea    0xd76(%rip),%rsi
call   10b0 <thread_error@plt>
mov    %rbp,%rdi
lea    0xd7d(%rip),%rsi
call   1090 <strcmp@plt>
mov    $0x0,%ebp
test   %rax,%rax
jne    126e <handle_client+0x7e>
xor    %edi,%edi
lea    0xd4d(%rip),%rsi
call   10f0 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    130b <handle_client+0x11b>
mov    %ebx,%edi
lea    0xd54(%rip),%rsi
call   10b0 <thread_error@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rbp,%rsi
mov    %eax,%ebp
lea    -0x1(%rbp),%eax
cmp    $0xff,%eax
ja     1343 <handle_client+0x153>
lea    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
mov    %ebx,%edi
lea    0x10(%rsp),%rsi
call   10c0 <addusernick@plt>
test   %r13d,%r13d
jns    126e <handle_client+0x7e>
jmp    1362 <handle_client+0x172>
mov    %ebx,%edi
lea    0xd2b(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    %eax,%r13d
test   %r13d,%r13d
jns    126e <handle_client+0x7e>
mov    %ebx,%edi
lea    0xd12(%rip),%rsi
call   10b0 <thread_error@plt>
jmp    126e <handle_client+0x7e>

LLVM IR (-O0):
@PTHREAD_CANCEL_DEFERRED = global i32 0
@.str = [23 x i8] c"ERROR in setcanceltype\00"
@.str.1 = [18 x i8] c"handle client %d\0A\00"
@.str.2 = [20 x i8] c"Client disconnected\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [22 x i8] c"ERROR client protocol\00"
@.str.5 = [5 x i8] c"USER\00"
@.str.6 = [29 x i8] c"ERROR reading client message\00"
@.str.7 = [6 x i8] c"ERROR\00"
@.str.8 = [24 x i8] c"ERROR writing to socket\00"
define i8* @handle_client(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i8*
%9 = alloca [256 x i8]
store i8* %0, i8** %2
%10 = load i8*, i8** %2
%11 = bitcast i8* %10 to i32*
%12 = load i32, i32* %11
store i32 %12, i32* %5
store i32 0, i32* %6
%13 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%14 = call i32 @pthread_setcanceltype(i32 %13, i32* %4)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i32, i32* %5
%19 = call i32 @thread_error(i32 %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %17, %1
%21 = load i32, i32* %5
%22 = call i64 (...) @pthread_self()
%23 = inttoptr i64 %22 to i32*
%24 = call i32 @addclientthread(i32 %21, i32* %23)
%25 = load i32, i32* %5
%26 = call i32 @sendusercount(i32 %25)
br label %27
27: ; preds = %20, %92
%28 = load i32, i32* %5
%29 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i32 0, i32 256)
%32 = load i32, i32* %5
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 255)
store i32 %34, i32* %3
%35 = load i32, i32* %3
%36 = icmp sle i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %27
%38 = load i32, i32* %5
%39 = call i32 @thread_error(i32 %38, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %37, %27
%41 = load i32, i32* %6
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %88
43: ; preds = %40
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%45 = call i8* @strtok(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i32, i32* %5
%50 = call i32 @thread_error(i32 %49, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %48, %43
%52 = load i8*, i8** %8
%53 = call i64 @strcmp(i8* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %87
55: ; preds = %51
%56 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i8* %56, i8** %8
%57 = load i8*, i8** %8
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %62
59: ; preds = %55
%60 = load i32, i32* %5
%61 = call i32 @thread_error(i32 %60, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %62
62: ; preds = %59, %55
%63 = load i8*, i8** %8
%64 = call i32 @strlen(i8* %63)
store i32 %64, i32* %6
%65 = load i32, i32* %6
%66 = icmp sgt i32 %65, 0
br i1 %66, label %67, label %77
67: ; preds = %62
%68 = load i32, i32* %6
%69 = icmp sle i32 %68, 256
br i1 %69, label %70, label %77
70: ; preds = %67
%71 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%72 = load i8*, i8** %8
%73 = call i32 @strcpy(i8* %71, i8* %72)
%74 = load i32, i32* %5
%75 = bitcast [256 x i8]* %9 to i8*
%76 = call i32 @addusernick(i32 %74, i8* %75)
br label %80
77: ; preds = %67, %62
%78 = load i32, i32* %5
%79 = call i32 @write(i32 %78, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 5)
store i32 %79, i32* %3
br label %80
80: ; preds = %77, %70
%81 = load i32, i32* %3
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %80
%84 = load i32, i32* %5
%85 = call i32 @thread_error(i32 %84, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %86
86: ; preds = %83, %80
br label %87
87: ; preds = %86, %51
br label %92
88: ; preds = %40
%89 = load i32, i32* %5
%90 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%91 = call i32 @sendmessagefrom(i32 %89, i8* %90)
br label %92
92: ; preds = %88, %87
br label %27
}

LLVM IR (-O2):
@PTHREAD_CANCEL_DEFERRED = global i32 0
@.str = [23 x i8] c"ERROR in setcanceltype\00"
@.str.1 = [18 x i8] c"handle client %d\0A\00"
@.str.2 = [20 x i8] c"Client disconnected\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [22 x i8] c"ERROR client protocol\00"
@.str.5 = [5 x i8] c"USER\00"
@.str.6 = [29 x i8] c"ERROR reading client message\00"
@.str.7 = [6 x i8] c"ERROR\00"
@.str.8 = [24 x i8] c"ERROR writing to socket\00"
define noalias i8* @handle_client(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = bitcast i32* %2 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = bitcast i8* %0 to i32*
%9 = load i32, i32* %8
%10 = load i32, i32* @PTHREAD_CANCEL_DEFERRED
%11 = call i32 @pthread_setcanceltype(i32 %10, i32* %2)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 @thread_error(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %1
%16 = call i64 (...) @pthread_self()
%17 = inttoptr i64 %16 to i32*
%18 = call i32 @addclientthread(i32 %9, i32* %17)
%19 = call i32 @sendusercount(i32 %9)
br label %20
20: ; preds = %58, %15
%21 = phi i32 [ 0, %15 ], [ %59, %58 ]
%22 = icmp eq i32 %21, 0
br label %23
23: ; preds = %20, %60
%24 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%25 = call i32 @memset(i8* %6, i32 0, i32 256)
%26 = call i32 @read(i32 %9, i8* %6, i32 255)
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @thread_error(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
br i1 %22, label %31, label %60
31: ; preds = %30
%32 = call i8* @strtok(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @thread_error(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
%37 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %58
39: ; preds = %36
%40 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @thread_error(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
%45 = call i32 @strlen(i8* %40)
%46 = add i32 %45, -1
%47 = icmp ult i32 %46, 256
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = call i32 @strcpy(i8* %7, i8* %40)
%50 = call i32 @addusernick(i32 %9, i8* %7)
br label %53
51: ; preds = %44
%52 = call i32 @write(i32 %9, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 5)
br label %53
53: ; preds = %51, %48
%54 = phi i32 [ %26, %48 ], [ %52, %51 ]
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %53
%57 = call i32 @thread_error(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %58
58: ; preds = %56, %53, %36
%59 = phi i32 [ 0, %36 ], [ %45, %53 ], [ %45, %56 ]
br label %20
60: ; preds = %30
%61 = call i32 @sendmessagefrom(i32 %9, i8* %6)
br label %23
}