#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 pptcapi_connection {
  int dummy;
};

/* Variables and functions */
scalar_t__ malloc(int);
int pptcapi_authenticate(struct pptcapi_connection *, char **);
int pptcapi_doreceive(struct pptcapi_connection *, char *, int, char **);
int pptcapi_dosend(struct pptcapi_connection *, char *, int, char **);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
int strlen(char *);
int strncmp(char *, char *, int);
int pptcapi_initialize_connect(struct pptcapi_connection *connection,
                               char **error) {
  char *tosend = "PPTCLIENT_TESTING_CONNECTION";
  int send_len = strlen(tosend);
  int retval = pptcapi_dosend(connection, tosend, send_len, error);
  if (retval == 2) {
    return 2;
  }
  char inbuf[65535 + 1];
  int rec_len = 65535;
  int bytes_read = pptcapi_doreceive(connection, inbuf, rec_len, error);
  if (bytes_read < 1) {
    return 2;
  }
  if (bytes_read > 65535)
    bytes_read = 65535;
  inbuf[bytes_read] = '\0';
  int undef_len = strlen("PPT_PROTOCOL_UNDEFINED");
  int ok_len = strlen("PPTSERVER_CONNECTION_OK");
  int auth_len = strlen("PPTSERVER_AUTHENTICATE");
  if (bytes_read == undef_len) {
    int cmp = strncmp(inbuf, "PPT_PROTOCOL_UNDEFINED", undef_len);
    if (cmp == 0) {
      *error = (char *)malloc(512);
      if (*error) {
        snprintf(*error, 512,
                 "Could not connect to server, server may be down or busy");
      }
      return 2;
    }
  }
  if (bytes_read == ok_len) {
    int cmp = strncmp(inbuf, "PPTSERVER_CONNECTION_OK", ok_len);
    if (cmp == 0) {
      return 1;
    }
  }
  if (bytes_read == auth_len) {
    int cmp = strncmp(inbuf, "PPTSERVER_AUTHENTICATE", auth_len);
    if (cmp == 0) {
      return pptcapi_authenticate(connection, error);
    }
  }
  *error = (char *)malloc(512);
  if (*error) {
    snprintf(*error, 512, "Server reported an invalid connection \"%s\"",
             inbuf);
  }
  return 2;
}


x86 assembly (clang, -O0):
<pptcapi_initialize_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xe76(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1030 <pptcapi_dosend@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x2,-0x28(%rbp)
jne    11c7 <pptcapi_initialize_connect+0x57>
movl   $0x2,-0x4(%rbp)
jmp    13e5 <pptcapi_initialize_connect+0x275>
movl   $0xffff,-0x10034(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x10030(%rbp),%rsi
mov    -0x10034(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1050 <pptcapi_doreceive@plt>
mov    %eax,-0x10038(%rbp)
cmpl   $0x1,-0x10038(%rbp)
jge    120a <pptcapi_initialize_connect+0x9a>
movl   $0x2,-0x4(%rbp)
jmp    13e5 <pptcapi_initialize_connect+0x275>
cmpl   $0xffff,-0x10038(%rbp)
jle    1224 <pptcapi_initialize_connect+0xb4>
movl   $0xffff,-0x10038(%rbp)
movslq -0x10038(%rbp),%rax
movb   $0x0,-0x10030(%rbp,%rax,1)
lea    0xde3(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x1003c(%rbp)
lea    0xde8(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x10040(%rbp)
lea    0xdee(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x10044(%rbp)
mov    -0x10038(%rbp),%eax
cmp    -0x1003c(%rbp),%eax
jne    12f4 <pptcapi_initialize_connect+0x184>
lea    -0x10030(%rbp),%rdi
mov    -0x1003c(%rbp),%edx
lea    0xd8e(%rip),%rsi
call   1040 <strncmp@plt>
mov    %eax,-0x10048(%rbp)
cmpl   $0x0,-0x10048(%rbp)
jne    12ef <pptcapi_initialize_connect+0x17f>
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     12e3 <pptcapi_initialize_connect+0x173>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    $0x200,%esi
lea    0xd87(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    13e5 <pptcapi_initialize_connect+0x275>
jmp    12f4 <pptcapi_initialize_connect+0x184>
mov    -0x10038(%rbp),%eax
cmp    -0x10040(%rbp),%eax
jne    1343 <pptcapi_initialize_connect+0x1d3>
lea    -0x10030(%rbp),%rdi
mov    -0x10040(%rbp),%edx
lea    0xd1a(%rip),%rsi
call   1040 <strncmp@plt>
mov    %eax,-0x1004c(%rbp)
cmpl   $0x0,-0x1004c(%rbp)
jne    133e <pptcapi_initialize_connect+0x1ce>
movl   $0x1,-0x4(%rbp)
jmp    13e5 <pptcapi_initialize_connect+0x275>
jmp    1343 <pptcapi_initialize_connect+0x1d3>
mov    -0x10038(%rbp),%eax
cmp    -0x10044(%rbp),%eax
jne    139b <pptcapi_initialize_connect+0x22b>
lea    -0x10030(%rbp),%rdi
mov    -0x10044(%rbp),%edx
lea    0xce3(%rip),%rsi
call   1040 <strncmp@plt>
mov    %eax,-0x10050(%rbp)
cmpl   $0x0,-0x10050(%rbp)
jne    1396 <pptcapi_initialize_connect+0x226>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <pptcapi_authenticate@plt>
mov    %eax,-0x4(%rbp)
jmp    13e5 <pptcapi_initialize_connect+0x275>
jmp    139b <pptcapi_initialize_connect+0x22b>
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
je     13de <pptcapi_initialize_connect+0x26e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    -0x10030(%rbp),%rcx
mov    $0x200,%esi
lea    0xcc4(%rip),%rdx
mov    $0x0,%al
call   1070 <snprintf@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pptcapi_initialize_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10018,%rsp
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0xe72(%rip),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
mov    %r15,%rcx
call   1030 <pptcapi_dosend@plt>
mov    $0x2,%r14d
cmp    $0x2,%eax
je     12dc <pptcapi_initialize_connect+0x16c>
lea    0x10(%rsp),%rsi
mov    $0xffff,%ebx
mov    %rbp,%rdi
mov    $0xffff,%edx
mov    %r15,%rcx
call   1050 <pptcapi_doreceive@plt>
mov    $0x2,%r14d
test   %eax,%eax
jle    12dc <pptcapi_initialize_connect+0x16c>
cmp    $0xffff,%eax
cmovb  %eax,%ebx
movb   $0x0,0x10(%rsp,%rbx,1)
lea    0xe2c(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%r12d
lea    0xe34(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,%r13d
lea    0xe3d(%rip),%rdi
call   1060 <strlen@plt>
mov    %eax,0xc(%rsp)
cmp    %r12d,%ebx
jne    1234 <pptcapi_initialize_connect+0xc4>
lea    0xdf9(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %ebx,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
je     129c <pptcapi_initialize_connect+0x12c>
cmp    %r13d,%ebx
jne    1250 <pptcapi_initialize_connect+0xe0>
lea    0xdf4(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %ebx,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
je     12c6 <pptcapi_initialize_connect+0x156>
cmp    0xc(%rsp),%ebx
jne    126d <pptcapi_initialize_connect+0xfd>
lea    0xdef(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %ebx,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
je     12ce <pptcapi_initialize_connect+0x15e>
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     12dc <pptcapi_initialize_connect+0x16c>
lea    0xe15(%rip),%rdx
lea    0x10(%rsp),%rcx
mov    %rax,%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1070 <snprintf@plt>
jmp    12dc <pptcapi_initialize_connect+0x16c>
mov    $0x200,%edi
call   1080 <malloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     12dc <pptcapi_initialize_connect+0x16c>
lea    0xdae(%rip),%rdx
mov    %rax,%rdi
mov    $0x200,%esi
xor    %eax,%eax
call   1070 <snprintf@plt>
jmp    12dc <pptcapi_initialize_connect+0x16c>
mov    $0x1,%r14d
jmp    12dc <pptcapi_initialize_connect+0x16c>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1090 <pptcapi_authenticate@plt>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x10018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pptcapi_connection = type { i32 }
@.str = [29 x i8] c"PPTCLIENT_TESTING_CONNECTION\00"
@.str.1 = [23 x i8] c"PPT_PROTOCOL_UNDEFINED\00"
@.str.2 = [24 x i8] c"PPTSERVER_CONNECTION_OK\00"
@.str.3 = [23 x i8] c"PPTSERVER_AUTHENTICATE\00"
@.str.4 = [56 x i8] c"Could not connect to server, server may be down or busy\00"
@.str.5 = [43 x i8] c"Server reported an invalid connection \22%s\22\00"
define i32 @pptcapi_initialize_connect(%struct.pptcapi_connection* %0, i8** %1) {
%3 = alloca i32
%4 = alloca %struct.pptcapi_connection*
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [65536 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store %struct.pptcapi_connection* %0, %struct.pptcapi_connection** %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8** %6
%18 = load i8*, i8** %6
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %7
%20 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %4
%21 = load i8*, i8** %6
%22 = load i32, i32* %7
%23 = load i8**, i8*** %5
%24 = call i32 @pptcapi_dosend(%struct.pptcapi_connection* %20, i8* %21, i32 %22, i8** %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 2
br i1 %26, label %27, label %28
27: ; preds = %2
store i32 2, i32* %3
br label %110
28: ; preds = %2
store i32 65535, i32* %10
%29 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %4
%30 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
%31 = load i32, i32* %10
%32 = load i8**, i8*** %5
%33 = call i32 @pptcapi_doreceive(%struct.pptcapi_connection* %29, i8* %30, i32 %31, i8** %32)
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp slt i32 %34, 1
br i1 %35, label %36, label %37
36: ; preds = %28
store i32 2, i32* %3
br label %110
37: ; preds = %28
%38 = load i32, i32* %11
%39 = icmp sgt i32 %38, 65535
br i1 %39, label %40, label %41
40: ; preds = %37
store i32 65535, i32* %11
br label %41
41: ; preds = %40, %37
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 %43
store i8 0, i8* %44
%45 = call i32 @strlen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 %45, i32* %12
%46 = call i32 @strlen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 %46, i32* %13
%47 = call i32 @strlen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 %47, i32* %14
%48 = load i32, i32* %11
%49 = load i32, i32* %12
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %70
51: ; preds = %41
%52 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
%53 = load i32, i32* %12
%54 = call i32 @strncmp(i8* %52, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %53)
store i32 %54, i32* %15
%55 = load i32, i32* %15
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %69
57: ; preds = %51
%58 = call i64 @malloc(i32 512)
%59 = inttoptr i64 %58 to i8*
%60 = load i8**, i8*** %5
store i8* %59, i8** %60
%61 = load i8**, i8*** %5
%62 = load i8*, i8** %61
%63 = icmp ne i8* %62, null
br i1 %63, label %64, label %68
64: ; preds = %57
%65 = load i8**, i8*** %5
%66 = load i8*, i8** %65
%67 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %66, i32 512, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
br label %68
68: ; preds = %64, %57
store i32 2, i32* %3
br label %110
69: ; preds = %51
br label %70
70: ; preds = %69, %41
%71 = load i32, i32* %11
%72 = load i32, i32* %13
%73 = icmp eq i32 %71, %72
br i1 %73, label %74, label %82
74: ; preds = %70
%75 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
%76 = load i32, i32* %13
%77 = call i32 @strncmp(i8* %75, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %76)
store i32 %77, i32* %16
%78 = load i32, i32* %16
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %74
store i32 1, i32* %3
br label %110
81: ; preds = %74
br label %82
82: ; preds = %81, %70
%83 = load i32, i32* %11
%84 = load i32, i32* %14
%85 = icmp eq i32 %83, %84
br i1 %85, label %86, label %97
86: ; preds = %82
%87 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
%88 = load i32, i32* %14
%89 = call i32 @strncmp(i8* %87, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %88)
store i32 %89, i32* %17
%90 = load i32, i32* %17
%91 = icmp eq i32 %90, 0
br i1 %91, label %92, label %96
92: ; preds = %86
%93 = load %struct.pptcapi_connection*, %struct.pptcapi_connection** %4
%94 = load i8**, i8*** %5
%95 = call i32 @pptcapi_authenticate(%struct.pptcapi_connection* %93, i8** %94)
store i32 %95, i32* %3
br label %110
96: ; preds = %86
br label %97
97: ; preds = %96, %82
%98 = call i64 @malloc(i32 512)
%99 = inttoptr i64 %98 to i8*
%100 = load i8**, i8*** %5
store i8* %99, i8** %100
%101 = load i8**, i8*** %5
%102 = load i8*, i8** %101
%103 = icmp ne i8* %102, null
br i1 %103, label %104, label %109
104: ; preds = %97
%105 = load i8**, i8*** %5
%106 = load i8*, i8** %105
%107 = getelementptr inbounds [65536 x i8], [65536 x i8]* %9, i64 0, i64 0
%108 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %106, i32 512, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i8* %107)
br label %109
109: ; preds = %104, %97
store i32 2, i32* %3
br label %110
110: ; preds = %109, %92, %80, %68, %36, %27
%111 = load i32, i32* %3
ret i32 %111
}

LLVM IR (-O2):
%struct.pptcapi_connection = type { i32 }
@.str = [29 x i8] c"PPTCLIENT_TESTING_CONNECTION\00"
@.str.1 = [23 x i8] c"PPT_PROTOCOL_UNDEFINED\00"
@.str.2 = [24 x i8] c"PPTSERVER_CONNECTION_OK\00"
@.str.3 = [23 x i8] c"PPTSERVER_AUTHENTICATE\00"
@.str.4 = [56 x i8] c"Could not connect to server, server may be down or busy\00"
@.str.5 = [43 x i8] c"Server reported an invalid connection \22%s\22\00"
define i32 @pptcapi_initialize_connect(%struct.pptcapi_connection* %0, i8** %1) {
%3 = alloca [65536 x i8]
%4 = tail call i32 @strlen(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @pptcapi_dosend(%struct.pptcapi_connection* %0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i32 %4, i8** %1)
%6 = icmp eq i32 %5, 2
br i1 %6, label %49, label %7
7: ; preds = %2
%8 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 0
%9 = call i32 @pptcapi_doreceive(%struct.pptcapi_connection* %0, i8* %8, i32 65535, i8** %1)
%10 = icmp slt i32 %9, 1
br i1 %10, label %47, label %11
11: ; preds = %7
%12 = icmp ult i32 %9, 65535
%13 = select i1 %12, i32 %9, i32 65535
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds [65536 x i8], [65536 x i8]* %3, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @strlen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @strlen(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @strlen(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32 %13, %16
br i1 %19, label %20, label %29
20: ; preds = %11
%21 = call i32 @strncmp(i8* %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = call i64 @malloc(i32 512)
%25 = inttoptr i64 %24 to i8*
store i8* %25, i8** %1
%26 = icmp eq i64 %24, 0
br i1 %26, label %47, label %27
27: ; preds = %23
%28 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %25, i32 512, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.4, i64 0, i64 0))
br label %47
29: ; preds = %20, %11
%30 = icmp eq i32 %13, %17
br i1 %30, label %31, label %34
31: ; preds = %29
%32 = call i32 @strncmp(i8* %8, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%33 = icmp eq i32 %32, 0
br i1 %33, label %47, label %34
34: ; preds = %31, %29
%35 = icmp eq i32 %13, %18
br i1 %35, label %36, label %41
36: ; preds = %34
%37 = call i32 @strncmp(i8* %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @pptcapi_authenticate(%struct.pptcapi_connection* %0, i8** %1)
br label %47
41: ; preds = %36, %34
%42 = call i64 @malloc(i32 512)
%43 = inttoptr i64 %42 to i8*
store i8* %43, i8** %1
%44 = icmp eq i64 %42, 0
br i1 %44, label %47, label %45
45: ; preds = %41
%46 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %43, i32 512, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i8* %8)
br label %47
47: ; preds = %39, %27, %23, %31, %45, %41, %7
%48 = phi i32 [ 2, %7 ], [ 1, %31 ], [ 2, %45 ], [ 2, %41 ], [ 2, %23 ], [ 2, %27 ], [ %40, %39 ]
br label %49
49: ; preds = %2, %47
%50 = phi i32 [ %48, %47 ], [ 2, %2 ]
ret i32 %50
}