#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*/ close(int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ sscanf(char *, char *, char *);
int /*<<< orphan*/ strlen(char *);
int waitforCTP(int clientfd, char *received) {
  char CTPProtocolPhase;
  char *response;
  printf("\t<--CTP--\n");
  if (recv(clientfd, received, 53000, 0) != -1 &&
      sscanf(received, "%c\n", &CTPProtocolPhase) && CTPProtocolPhase == 't') {
    printf("\t\tProtocol Phase: %c\n", CTPProtocolPhase);
    response = "200 OK: Closing Connection\n";
  } else {
    printf("\t\tInvalid CTP: %s", received);
    response = "404 ERROR: Invalid Connection Termination Message";
  }
  send(clientfd, response, strlen(response), 0);
  close(clientfd);
  printf("\t--CTP-->\n");
  return 1;
}


x86 assembly (clang, -O0):
<waitforCTP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xcf08,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     11e6 <waitforCTP+0x86>
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
lea    -0x11(%rbp),%rdx
call   1080 <sscanf@plt>
cmp    $0x0,%rax
je     11e6 <waitforCTP+0x86>
movsbl -0x11(%rbp),%eax
cmp    $0x74,%eax
jne    11e6 <waitforCTP+0x86>
movsbl -0x11(%rbp),%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe44(%rip),%rax
mov    %rax,-0x20(%rbp)
jmp    1203 <waitforCTP+0xa3>
mov    -0x10(%rbp),%rsi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xe50(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitforCTP>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xef7(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xcf08,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     11d6 <waitforCTP+0x66>
lea    0xe71(%rip),%rsi
lea    0x7(%rsp),%rdx
mov    %rbx,%rdi
call   1090 <sscanf@plt>
test   %rax,%rax
je     11d6 <waitforCTP+0x66>
cmpb   $0x74,0x7(%rsp)
jne    11d6 <waitforCTP+0x66>
lea    0xe3f(%rip),%rdi
mov    $0x74,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe42(%rip),%rbx
jmp    11ee <waitforCTP+0x7e>
lea    0xe55(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe56(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %ebp,%edi
call   1080 <close@plt>
lea    0xe6e(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"\09<--CTP--\0A\00"
@.str.1 = [4 x i8] c"%c\0A\00"
@.str.2 = [22 x i8] c"\09\09Protocol Phase: %c\0A\00"
@.str.3 = [28 x i8] c"200 OK: Closing Connection\0A\00"
@.str.4 = [18 x i8] c"\09\09Invalid CTP: %s\00"
@.str.5 = [50 x i8] c"404 ERROR: Invalid Connection Termination Message\00"
@.str.6 = [11 x i8] c"\09--CTP-->\0A\00"
define i32 @waitforCTP(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* %3
%9 = load i8*, i8** %4
%10 = call i32 @recv(i32 %8, i8* %9, i32 53000, i32 0)
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %24
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @sscanf(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = load i8, i8* %5
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 116
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i8, i8* %5
%22 = sext i8 %21 to i32
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %22)
store i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8** %6
br label %27
24: ; preds = %16, %12, %2
%25 = load i8*, i8** %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %25)
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), i8** %6
br label %27
27: ; preds = %24, %20
%28 = load i32, i32* %3
%29 = load i8*, i8** %6
%30 = load i8*, i8** %6
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @send(i32 %28, i8* %29, i32 %31, i32 0)
%33 = load i32, i32* %3
%34 = call i32 @close(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
ret i32 1
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"%c\0A\00"
@.str.2 = [22 x i8] c"\09\09Protocol Phase: %c\0A\00"
@.str.3 = [28 x i8] c"200 OK: Closing Connection\0A\00"
@.str.4 = [18 x i8] c"\09\09Invalid CTP: %s\00"
@.str.5 = [50 x i8] c"404 ERROR: Invalid Connection Termination Message\00"
@str = [10 x i8] c"\09<--CTP--\00"
@str.7 = [10 x i8] c"\09--CTP-->\00"
define i32 @waitforCTP(i32 %0, i8* %1) {
%3 = alloca i8
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @recv(i32 %0, i8* %1, i32 53000, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %15, label %7
7: ; preds = %2
%8 = call i64 @sscanf(i8* %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%9 = icmp ne i64 %8, 0
%10 = load i8, i8* %3
%11 = icmp eq i8 %10, 116
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 116)
br label %17
15: ; preds = %7, %2
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %17
17: ; preds = %15, %13
%18 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), %13 ], [ getelementptr inbounds ([50 x i8], [50 x i8]* @.str.5, i64 0, i64 0), %15 ]
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @send(i32 %0, i8* %18, i32 %19, i32 0)
%21 = call i32 @close(i32 %0)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.7, i64 0, i64 0))
ret i32 1
}