#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*/ client(unsigned char *, char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ scanf(char *, ...);
int /*<<< orphan*/ server(unsigned char *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  char *mode = NULL;
  unsigned char *key = NULL;
  if (argc < 3) {
    fprintf(stderr, "Usage %s <server|client> <key>\n", argv[0]);
    exit(1);
  }
  mode = argv[1];
  key = (unsigned char *)argv[2];
  printf("Welcome to OsLab chat room\n");
  if (strcmp(mode, "server") == 0) {
    server(key);
  } else {
    char hostname[50];
    int port;
    printf("Please write the hostname you want to connect to: ");
    scanf("%s", hostname);
    printf("Please insert port's number: ");
    scanf("%d", &port);
    while (port > 65535 || port < 0) {
      printf("Port number invalid! Please insert port's number: ");
      scanf("%d", &port);
    }
    client(key, hostname, port);
  }
  fprintf(stderr, "Internal error. Unreachable point\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11c8 <main+0x58>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
lea    0xe39(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe45(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1214 <main+0xa4>
mov    -0x20(%rbp),%rdi
call   1060 <server@plt>
jmp    12af <main+0x13f>
lea    0xe28(%rip),%rdi
call   1030 <printf@plt>
lea    -0x60(%rbp),%rsi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1070 <scanf@plt>
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
lea    0xe34(%rip),%rdi
lea    -0x64(%rbp),%rsi
mov    $0x0,%al
call   1070 <scanf@plt>
mov    $0x1,%al
cmpl   $0xffff,-0x64(%rbp)
mov    %al,-0x65(%rbp)
jg     126c <main+0xfc>
cmpl   $0x0,-0x64(%rbp)
setl   %al
mov    %al,-0x65(%rbp)
mov    -0x65(%rbp),%al
test   $0x1,%al
jne    127c <main+0x10c>
jmp    129f <main+0x12f>
lea    0xdf9(%rip),%rdi
call   1030 <printf@plt>
lea    0xdea(%rip),%rdi
lea    -0x64(%rbp),%rsi
mov    $0x0,%al
call   1070 <scanf@plt>
jmp    1250 <main+0xe0>
mov    -0x20(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    -0x64(%rbp),%edx
call   1080 <client@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x48,%rsp
cmp    $0x2,%edi
jle    1280 <main+0x100>
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%r14
lea    0xf13(%rip),%rdi
call   1030 <puts@plt>
lea    0xe71(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1250 <main+0xd0>
lea    0xe60(%rip),%rdi
call   1040 <printf@plt>
lea    0xe87(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1080 <scanf@plt>
lea    0xe8f(%rip),%rdi
call   1040 <printf@plt>
lea    0xe6b(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1080 <scanf@plt>
mov    0xc(%rsp),%edx
cmp    $0x10000,%edx
jb     1241 <main+0xc1>
lea    0xe4f(%rip),%r15
lea    0xe45(%rip),%r12
lea    0xc(%rsp),%rbx
nopl   (%rax)
mov    %r15,%rdi
call   1040 <printf@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <scanf@plt>
mov    0xc(%rsp),%edx
cmp    $0xffff,%edx
ja     1220 <main+0xa0>
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   1090 <client@plt>
jmp    1258 <main+0xd8>
mov    %r14,%rdi
call   1070 <server@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd6d(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Usage %s <server|client> <key>\0A\00"
@.str.1 = [28 x i8] c"Welcome to OsLab chat room\0A\00"
@.str.2 = [7 x i8] c"server\00"
@.str.3 = [51 x i8] c"Please write the hostname you want to connect to: \00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [30 x i8] c"Please insert port's number: \00"
@.str.6 = [3 x i8] c"%d\00"
@.str.7 = [51 x i8] c"Port number invalid! Please insert port's number: \00"
@.str.8 = [35 x i8] c"Internal error. Unreachable point\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [50 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %7
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
store i8* %25, i8** %7
%26 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%27 = load i8*, i8** %6
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %19
%31 = load i8*, i8** %7
%32 = call i32 @server(i8* %31)
br label %55
33: ; preds = %19
%34 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%35 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%36 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %9)
br label %39
39: ; preds = %47, %33
%40 = load i32, i32* %9
%41 = icmp sgt i32 %40, 65535
br i1 %41, label %45, label %42
42: ; preds = %39
%43 = load i32, i32* %9
%44 = icmp slt i32 %43, 0
br label %45
45: ; preds = %42, %39
%46 = phi i1 [ true, %39 ], [ %44, %42 ]
br i1 %46, label %47, label %50
47: ; preds = %45
%48 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.7, i64 0, i64 0))
%49 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %9)
br label %39
50: ; preds = %45
%51 = load i8*, i8** %7
%52 = getelementptr inbounds [50 x i8], [50 x i8]* %8, i64 0, i64 0
%53 = load i32, i32* %9
%54 = call i32 @client(i8* %51, i8* %52, i32 %53)
br label %55
55: ; preds = %50, %30
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0))
ret i32 1
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Usage %s <server|client> <key>\0A\00"
@.str.2 = [7 x i8] c"server\00"
@.str.3 = [51 x i8] c"Please write the hostname you want to connect to: \00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [30 x i8] c"Please insert port's number: \00"
@.str.6 = [3 x i8] c"%d\00"
@.str.7 = [51 x i8] c"Port number invalid! Please insert port's number: \00"
@.str.8 = [35 x i8] c"Internal error. Unreachable point\0A\00"
@str = [27 x i8] c"Welcome to OsLab chat room\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [50 x i8]
%4 = alloca i32
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%17 = tail call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = tail call i32 @server(i8* %15)
br label %38
21: ; preds = %11
%22 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%23 = bitcast i32* %4 to i8*
%24 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %22)
%26 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%27 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %4)
%28 = load i32, i32* %4
%29 = icmp ugt i32 %28, 65535
br i1 %29, label %30, label %35
30: ; preds = %21, %30
%31 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.7, i64 0, i64 0))
%32 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* %4)
%33 = load i32, i32* %4
%34 = icmp ugt i32 %33, 65535
br i1 %34, label %30, label %35
35: ; preds = %30, %21
%36 = phi i32 [ %28, %21 ], [ %33, %30 ]
%37 = call i32 @client(i8* %15, i8* %22, i32 %36)
br label %38
38: ; preds = %35, %19
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0))
ret i32 1
}