#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 */
struct registration {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ completeMessage(char const *, size_t);
int inviteUser(char *, int, char const *, size_t, struct registration *,
               size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int sendall(int, char *, int *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char const *strtok(char *, char const *);
int handleInviteMsg(int socketfd, const char *msg, size_t msglen,
                    struct registration *registrations, size_t numReg) {
  int ret;
  char str[8192];
  char user[128];
  const char delim[2] = "\n";
  printf("Handling Invite \n");
  if (!completeMessage(msg, msglen)) {
    printf("Message not complet... waiting for next bit..\n");
    return 0;
  }
  strncpy(str, msg, 8192);
  strncpy(user, strtok(str + 7, delim), 128);
  ret = inviteUser(user, socketfd, msg, msglen, registrations, numReg);
  if (ret == 100) {
    char retStr[] = "100 Trying\r\n\0";
    int len = strlen(retStr);
    if (sendall(socketfd, retStr, &len) == -1) {
      perror("100 Trying send");
    }
  } else {
    char retStr[] = "404\r\n\0";
    int len = strlen(retStr);
    if (sendall(socketfd, retStr, &len) == -1) {
      perror("404 send");
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<handleInviteMsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20e0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    0xe6b(%rip),%ax
mov    %ax,-0x20b2(%rbp)
lea    0xe4d(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <completeMessage@plt>
cmp    $0x0,%eax
jne    11e6 <handleInviteMsg+0x66>
lea    0xe3d(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1339 <handleInviteMsg+0x1b9>
lea    -0x2030(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x2000,%edx
call   1040 <strncpy@plt>
lea    -0x20b0(%rbp),%rax
mov    %rax,-0x20d8(%rbp)
lea    -0x2030(%rbp),%rdi
add    $0x7,%rdi
lea    -0x20b2(%rbp),%rsi
call   10a0 <strtok@plt>
mov    -0x20d8(%rbp),%rdi
mov    %rax,%rsi
mov    $0x80,%edx
call   1040 <strncpy@plt>
lea    -0x20b0(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9
call   1030 <inviteUser@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x64,-0x2c(%rbp)
jne    12ca <handleInviteMsg+0x14a>
mov    0xdf3(%rip),%rax
mov    %rax,-0x20c0(%rbp)
mov    0xdee(%rip),%eax
mov    %eax,-0x20b8(%rbp)
mov    0xde5(%rip),%ax
mov    %ax,-0x20b4(%rbp)
lea    -0x20c0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20c4(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20c0(%rbp),%rsi
lea    -0x20c4(%rbp),%rdx
call   1080 <sendall@plt>
cmp    $0xffffffff,%eax
jne    12c5 <handleInviteMsg+0x145>
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
jmp    1332 <handleInviteMsg+0x1b2>
mov    0xd98(%rip),%eax
mov    %eax,-0x20cb(%rbp)
mov    0xd8f(%rip),%ax
mov    %ax,-0x20c7(%rbp)
mov    0xd84(%rip),%al
mov    %al,-0x20c5(%rbp)
lea    -0x20cb(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20d0(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20cb(%rbp),%rsi
lea    -0x20d0(%rbp),%rdx
call   1080 <sendall@plt>
cmp    $0xffffffff,%eax
jne    132d <handleInviteMsg+0x1ad>
lea    0xd29(%rip),%rdi
call   1090 <perror@plt>
jmp    1332 <handleInviteMsg+0x1b2>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handleInviteMsg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20a8,%rsp
mov    %r8,%r14
mov    %rcx,%r12
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %edi,%r15d
movw   $0xa,0xe(%rsp)
lea    0xe80(%rip),%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <completeMessage@plt>
test   %eax,%eax
je     126b <handleInviteMsg+0xeb>
lea    0xa0(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1040 <strncpy@plt>
lea    0xa7(%rsp),%rdi
lea    0xe(%rsp),%rsi
call   10a0 <strtok@plt>
lea    0x20(%rsp),%r13
mov    %r13,%rdi
mov    %rax,%rsi
mov    $0x80,%edx
call   1040 <strncpy@plt>
mov    %r13,%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
mov    %rbp,%rcx
mov    %r12,%r8
mov    %r14,%r9
call   1030 <inviteUser@plt>
cmp    $0x64,%eax
jne    127b <handleInviteMsg+0xfb>
movabs $0xa0d676e6979,%rax
mov    %rax,0x16(%rsp)
movabs $0x6979725420303031,%rax
72 79 69
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1080 <sendall@plt>
cmp    $0xffffffff,%eax
jne    12b1 <handleInviteMsg+0x131>
lea    0xdac(%rip),%rdi
jmp    12cf <handleInviteMsg+0x14f>
lea    0xdcd(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
jmp    12b6 <handleInviteMsg+0x136>
movl   $0xa0d,0x13(%rsp)
movl   $0xd343034,0x10(%rsp)
0d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1080 <sendall@plt>
cmp    $0xffffffff,%eax
je     12c8 <handleInviteMsg+0x148>
mov    $0x1,%eax
add    $0x20a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd56(%rip),%rdi
call   1090 <perror@plt>
jmp    12b1 <handleInviteMsg+0x131>

LLVM IR (-O0):
%struct.registration = type { i32 }
@__const.handleInviteMsg.delim = [2 x i8] c"\0A\00"
@.str = [18 x i8] c"Handling Invite \0A\00"
@.str.1 = [47 x i8] c"Message not complet... waiting for next bit..\0A\00"
@__const.handleInviteMsg.retStr = [14 x i8] c"100 Trying\0D\0A\00\00"
@.str.2 = [16 x i8] c"100 Trying send\00"
@__const.handleInviteMsg.retStr.3 = [7 x i8] c"404\0D\0A\00\00"
@.str.4 = [9 x i8] c"404 send\00"
define i32 @handleInviteMsg(i32 %0, i8* %1, i64 %2, %struct.registration* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.registration*
%11 = alloca i64
%12 = alloca i32
%13 = alloca [8192 x i8]
%14 = alloca [128 x i8]
%15 = alloca [2 x i8]
%16 = alloca [14 x i8]
%17 = alloca i32
%18 = alloca [7 x i8]
%19 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store %struct.registration* %3, %struct.registration** %10
store i64 %4, i64* %11
%20 = bitcast [2 x i8]* %15 to i8*
%21 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%22 = load i8*, i8** %8
%23 = load i64, i64* %9
%24 = call i32 @completeMessage(i8* %22, i64 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %5
%27 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %6
br label %70
28: ; preds = %5
%29 = getelementptr inbounds [8192 x i8], [8192 x i8]* %13, i64 0, i64 0
%30 = load i8*, i8** %8
%31 = call i32 @strncpy(i8* %29, i8* %30, i32 8192)
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %14, i64 0, i64 0
%33 = getelementptr inbounds [8192 x i8], [8192 x i8]* %13, i64 0, i64 0
%34 = getelementptr inbounds i8, i8* %33, i64 7
%35 = getelementptr inbounds [2 x i8], [2 x i8]* %15, i64 0, i64 0
%36 = call i8* @strtok(i8* %34, i8* %35)
%37 = call i32 @strncpy(i8* %32, i8* %36, i32 128)
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %14, i64 0, i64 0
%39 = load i32, i32* %7
%40 = load i8*, i8** %8
%41 = load i64, i64* %9
%42 = load %struct.registration*, %struct.registration** %10
%43 = load i64, i64* %11
%44 = call i32 @inviteUser(i8* %38, i32 %39, i8* %40, i64 %41, %struct.registration* %42, i64 %43)
store i32 %44, i32* %12
%45 = load i32, i32* %12
%46 = icmp eq i32 %45, 100
br i1 %46, label %47, label %58
47: ; preds = %28
%48 = bitcast [14 x i8]* %16 to i8*
%49 = getelementptr inbounds [14 x i8], [14 x i8]* %16, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
store i32 %50, i32* %17
%51 = load i32, i32* %7
%52 = getelementptr inbounds [14 x i8], [14 x i8]* %16, i64 0, i64 0
%53 = call i32 @sendall(i32 %51, i8* %52, i32* %17)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %57
55: ; preds = %47
%56 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %57
57: ; preds = %55, %47
br label %69
58: ; preds = %28
%59 = bitcast [7 x i8]* %18 to i8*
%60 = getelementptr inbounds [7 x i8], [7 x i8]* %18, i64 0, i64 0
%61 = call i32 @strlen(i8* %60)
store i32 %61, i32* %19
%62 = load i32, i32* %7
%63 = getelementptr inbounds [7 x i8], [7 x i8]* %18, i64 0, i64 0
%64 = call i32 @sendall(i32 %62, i8* %63, i32* %19)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %68
66: ; preds = %58
%67 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %68
68: ; preds = %66, %58
br label %69
69: ; preds = %68, %57
store i32 1, i32* %6
br label %70
70: ; preds = %69, %26
%71 = load i32, i32* %6
ret i32 %71
}

LLVM IR (-O2):
%struct.registration = type { i32 }
@__const.handleInviteMsg.retStr = [14 x i8] c"100 Trying\0D\0A\00\00"
@.str.2 = [16 x i8] c"100 Trying send\00"
@__const.handleInviteMsg.retStr.3 = [7 x i8] c"404\0D\0A\00\00"
@.str.4 = [9 x i8] c"404 send\00"
@str = [17 x i8] c"Handling Invite \00"
@str.5 = [46 x i8] c"Message not complet... waiting for next bit..\00"
define i32 @handleInviteMsg(i32 %0, i8* %1, i64 %2, %struct.registration* %3, i64 %4) {
%6 = alloca [8192 x i8]
%7 = alloca [128 x i8]
%8 = alloca i16
%9 = alloca [14 x i8]
%10 = alloca i32
%11 = alloca [7 x i8]
%12 = alloca i32
%13 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 0
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%15 = bitcast i16* %8 to i8*
store i16 10, i16* %8
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @completeMessage(i8* %1, i64 %2)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %5
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str.5, i64 0, i64 0))
br label %46
21: ; preds = %5
%22 = call i32 @strncpy(i8* %13, i8* %1, i32 8192)
%23 = getelementptr inbounds [8192 x i8], [8192 x i8]* %6, i64 0, i64 7
%24 = call i8* @strtok(i8* %23, i8* %15)
%25 = call i32 @strncpy(i8* %14, i8* %24, i32 128)
%26 = call i32 @inviteUser(i8* %14, i32 %0, i8* %1, i64 %2, %struct.registration* %3, i64 %4)
%27 = icmp eq i32 %26, 100
br i1 %27, label %28, label %37
28: ; preds = %21
%29 = getelementptr inbounds [14 x i8], [14 x i8]* %9, i64 0, i64 0
%30 = bitcast i32* %10 to i8*
%31 = call i32 @strlen(i8* %29)
store i32 %31, i32* %10
%32 = call i32 @sendall(i32 %0, i8* %29, i32* %10)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %28
br label %46
37: ; preds = %21
%38 = getelementptr inbounds [7 x i8], [7 x i8]* %11, i64 0, i64 0
%39 = bitcast i32* %12 to i8*
%40 = call i32 @strlen(i8* %38)
store i32 %40, i32* %12
%41 = call i32 @sendall(i32 %0, i8* %38, i32* %12)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %37
br label %46
46: ; preds = %36, %45, %19
%47 = phi i32 [ 0, %19 ], [ 1, %45 ], [ 1, %36 ]
ret i32 %47
}