#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
void connect_client_to_server(int sockfd) {
  int nread, nwrite;
  char *protocol = "<rembash>\n";
  char server_protocol[513];
  if ((nread = read(sockfd, server_protocol, 512)) == -1) {
    perror("Client: Error reading from client.");
    exit(EXIT_FAILURE);
  }
  server_protocol[nread] = '\0';
  if (strcmp(protocol, server_protocol) != 0) {
    fprintf(stderr, "Client: Incorrect Protocol.\n");
    exit(EXIT_FAILURE);
  }
  if ((nwrite = write(sockfd, "<cs410591>\n", strlen("<cs410591>\n"))) == -1) {
    perror("Client: Unable to send secret to client.");
    exit(EXIT_FAILURE);
  }
  char confirm_protocol[513];
  if ((nread = read(sockfd, confirm_protocol, 512)) == -1) {
    perror("Client: Error reading from client.");
    exit(EXIT_FAILURE);
  }
  confirm_protocol[nread] = '\0';
  if (strcmp(confirm_protocol, "<ok>\n") != 0) {
    fprintf(stderr, "Client: Server unable to Confirm Handshake.\n");
    exit(EXIT_FAILURE);
  }
  return;
}


x86 assembly (clang, -O0):
<connect_client_to_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe7b(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11c3 <connect_client_to_server+0x53>
lea    0xe5b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
lea    -0x220(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     120c <connect_client_to_server+0x9c>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x434(%rbp)
lea    0xe2f(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x434(%rbp),%edi
mov    %eax,%edx
lea    0xe1b(%rip),%rsi
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    125b <connect_client_to_server+0xeb>
lea    0xe0f(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1295 <connect_client_to_server+0x125>
lea    0xd89(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rdi
lea    0xdd1(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     12e1 <connect_client_to_server+0x171>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2cfe(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_client_to_server>:
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %edi,%ebx
lea    0x210(%rsp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     120f <connect_client_to_server+0x9f>
cltq
movb   $0x0,0x210(%rsp,%rax,1)
lea    0xe5c(%rip),%rdi
lea    0x210(%rsp),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1218 <connect_client_to_server+0xa8>
lea    0xe8e(%rip),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     122a <connect_client_to_server+0xba>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x200,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     120f <connect_client_to_server+0x9f>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xe89(%rip),%rsi
mov    %rsp,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1238 <connect_client_to_server+0xc8>
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdf5(%rip),%rdi
jmp    1231 <connect_client_to_server+0xc1>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
jmp    1248 <connect_client_to_server+0xd8>
lea    0xe26(%rip),%rdi
call   1080 <perror@plt>
jmp    124d <connect_client_to_server+0xdd>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"<rembash>\0A\00"
@.str.1 = [35 x i8] c"Client: Error reading from client.\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.2 = [29 x i8] c"Client: Incorrect Protocol.\0A\00"
@.str.3 = [12 x i8] c"<cs410591>\0A\00"
@.str.4 = [41 x i8] c"Client: Unable to send secret to client.\00"
@.str.5 = [6 x i8] c"<ok>\0A\00"
@.str.6 = [45 x i8] c"Client: Server unable to Confirm Handshake.\0A\00"
define void @connect_client_to_server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [513 x i8]
%7 = alloca [513 x i8]
store i32 %0, i32* %2
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %5
%8 = load i32, i32* %2
%9 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%10 = call i32 @read(i32 %8, i8* %9, i32 512)
store i32 %10, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
%17 = load i32, i32* %3
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 %18
store i8 0, i8* %19
%20 = load i8*, i8** %5
%21 = getelementptr inbounds [513 x i8], [513 x i8]* %6, i64 0, i64 0
%22 = call i64 @strcmp(i8* %20, i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %16
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %16
%30 = load i32, i32* %2
%31 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @write(i32 %30, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %31)
store i32 %32, i32* %4
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = load i32, i32* %2
%40 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 512)
store i32 %41, i32* %3
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %38
%48 = load i32, i32* %3
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 %49
store i8 0, i8* %50
%51 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%52 = call i64 @strcmp(i8* %51, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %47
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %47
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"<rembash>\0A\00"
@.str.1 = [35 x i8] c"Client: Error reading from client.\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.2 = [29 x i8] c"Client: Incorrect Protocol.\0A\00"
@.str.3 = [12 x i8] c"<cs410591>\0A\00"
@.str.4 = [41 x i8] c"Client: Unable to send secret to client.\00"
@.str.5 = [6 x i8] c"<ok>\0A\00"
@.str.6 = [45 x i8] c"Client: Server unable to Confirm Handshake.\0A\00"
define void @connect_client_to_server(i32 %0) {
%2 = alloca [513 x i8]
%3 = alloca [513 x i8]
%4 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 0
%5 = call i32 @read(i32 %0, i8* %4, i32 512)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = sext i32 %5 to i64
%13 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i64 @strcmp(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %11
%22 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @write(i32 %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 0
%31 = call i32 @read(i32 %0, i8* %30, i32 512)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %29
%38 = sext i32 %31 to i64
%39 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 %38
store i8 0, i8* %39
%40 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %37
ret void
}