#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 c_connection(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ handle(int, char *);
int /*<<< orphan*/ printf(char *, ...);
int s_connection();
int strlen(char *);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  int socket = 0;
  char *str = NULL;
  int size = 0;
  printf("argc %d\n", argc);
  if (argc == 1) {
    usage();
    return -1;
  }
  if (*argv[1] == 'c') {
    if (argc < 4 || argc > 4) {
      usage();
      return -1;
    }
    if (strlen(argv[2]) > 32) {
      printf("[*]Error: username cannot be longer than 32 characters\n");
      return -1;
    } else
      socket = c_connection(argv[3]);
  } else if (*argv[1] == 's') {
    if (argc < 3 || argc > 3) {
      usage();
      return -1;
    } else
      socket = s_connection();
  } else {
    usage();
    return -1;
  }
  if (socket > 0)
    handle(socket, argv[2]);
  else {
    printf("[*]could not connect\n");
    printf("[*]terminating\n");
  }
  close(socket);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jne    11ca <main+0x5a>
mov    $0x0,%al
call   1090 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f6 <main+0x186>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x63,%eax
jne    124a <main+0xda>
cmpl   $0x4,-0x8(%rbp)
jl     11f2 <main+0x82>
cmpl   $0x4,-0x8(%rbp)
jle    1205 <main+0x95>
mov    $0x0,%al
call   1090 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f6 <main+0x186>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1030 <strlen@plt>
cmp    $0x20,%eax
jle    1235 <main+0xc5>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f6 <main+0x186>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1070 <c_connection@plt>
mov    %eax,-0x14(%rbp)
jmp    12ac <main+0x13c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x73,%eax
jne    1294 <main+0x124>
cmpl   $0x3,-0x8(%rbp)
jl     1272 <main+0x102>
cmpl   $0x3,-0x8(%rbp)
jle    1285 <main+0x115>
mov    $0x0,%al
call   1090 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f6 <main+0x186>
mov    $0x0,%al
call   1080 <s_connection@plt>
mov    %eax,-0x14(%rbp)
jmp    12a7 <main+0x137>
mov    $0x0,%al
call   1090 <usage@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f6 <main+0x186>
jmp    12ac <main+0x13c>
cmpl   $0x0,-0x14(%rbp)
jle    12cb <main+0x15b>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1060 <handle@plt>
jmp    12e7 <main+0x177>
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe71(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x1,%ebp
je     11da <main+0x5a>
mov    0x8(%rbx),%rax
mov    (%rax),%al
cmp    $0x73,%al
je     11cc <main+0x4c>
cmp    $0x63,%al
jne    11da <main+0x5a>
cmp    $0x4,%ebp
jne    11da <main+0x5a>
mov    0x10(%rbx),%rdi
call   1040 <strlen@plt>
cmp    $0x21,%eax
jl     11ed <main+0x6d>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
jmp    11e1 <main+0x61>
cmp    $0x3,%ebp
jne    11da <main+0x5a>
xor    %eax,%eax
call   1090 <s_connection@plt>
jmp    11f6 <main+0x76>
xor    %eax,%eax
call   10a0 <usage@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x18(%rbx),%rdi
call   1080 <c_connection@plt>
mov    %eax,%ebp
test   %eax,%eax
jle    1209 <main+0x89>
mov    0x10(%rbx),%rsi
mov    %ebp,%edi
call   1070 <handle@plt>
jmp    1221 <main+0xa1>
lea    0xdf9(%rip),%rdi
call   1030 <puts@plt>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11e6 <main+0x66>

LLVM IR (-O0):
@.str = [9 x i8] c"argc %d\0A\00"
@.str.1 = [56 x i8] c"[*]Error: username cannot be longer than 32 characters\0A\00"
@.str.2 = [22 x i8] c"[*]could not connect\0A\00"
@.str.3 = [16 x i8] c"[*]terminating\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i8* null, i8** %7
store i32 0, i32* %8
%9 = load i32, i32* %4
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (...) @usage()
store i32 -1, i32* %3
br label %80
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 99
br i1 %21, label %22, label %44
22: ; preds = %15
%23 = load i32, i32* %4
%24 = icmp slt i32 %23, 4
br i1 %24, label %28, label %25
25: ; preds = %22
%26 = load i32, i32* %4
%27 = icmp sgt i32 %26, 4
br i1 %27, label %28, label %30
28: ; preds = %25, %22
%29 = call i32 (...) @usage()
store i32 -1, i32* %3
br label %80
30: ; preds = %25
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @strlen(i8* %33)
%35 = icmp sgt i32 %34, 32
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %80
38: ; preds = %30
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 3
%41 = load i8*, i8** %40
%42 = call i32 @c_connection(i8* %41)
store i32 %42, i32* %6
br label %43
43: ; preds = %38
br label %65
44: ; preds = %15
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 115
br i1 %50, label %51, label %62
51: ; preds = %44
%52 = load i32, i32* %4
%53 = icmp slt i32 %52, 3
br i1 %53, label %57, label %54
54: ; preds = %51
%55 = load i32, i32* %4
%56 = icmp sgt i32 %55, 3
br i1 %56, label %57, label %59
57: ; preds = %54, %51
%58 = call i32 (...) @usage()
store i32 -1, i32* %3
br label %80
59: ; preds = %54
%60 = call i32 (...) @s_connection()
store i32 %60, i32* %6
br label %61
61: ; preds = %59
br label %64
62: ; preds = %44
%63 = call i32 (...) @usage()
store i32 -1, i32* %3
br label %80
64: ; preds = %61
br label %65
65: ; preds = %64, %43
%66 = load i32, i32* %6
%67 = icmp sgt i32 %66, 0
br i1 %67, label %68, label %74
68: ; preds = %65
%69 = load i32, i32* %6
%70 = load i8**, i8*** %5
%71 = getelementptr inbounds i8*, i8** %70, i64 2
%72 = load i8*, i8** %71
%73 = call i32 @handle(i32 %69, i8* %72)
br label %77
74: ; preds = %65
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %77
77: ; preds = %74, %68
%78 = load i32, i32* %6
%79 = call i32 @close(i32 %78)
store i32 0, i32* %3
br label %80
80: ; preds = %77, %62, %57, %36, %28, %13
%81 = load i32, i32* %3
ret i32 %81
}

LLVM IR (-O2):
@.str = [9 x i8] c"argc %d\0A\00"
@str = [21 x i8] c"[*]could not connect\00"
@str.4 = [15 x i8] c"[*]terminating\00"
@str.5 = [55 x i8] c"[*]Error: username cannot be longer than 32 characters\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%4 = icmp eq i32 %0, 1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (...) @usage()
br label %46
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i8, i8* %9
switch i8 %10, label %32 [
i8 99, label %11
i8 115, label %26
]
11: ; preds = %7
%12 = icmp eq i32 %0, 4
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = tail call i32 (...) @usage()
br label %46
15: ; preds = %11
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @strlen(i8* %17)
%19 = icmp sgt i32 %18, 32
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.5, i64 0, i64 0))
br label %46
22: ; preds = %15
%23 = getelementptr inbounds i8*, i8** %1, i64 3
%24 = load i8*, i8** %23
%25 = tail call i32 @c_connection(i8* %24)
br label %34
26: ; preds = %7
%27 = icmp eq i32 %0, 3
br i1 %27, label %30, label %28
28: ; preds = %26
%29 = tail call i32 (...) @usage()
br label %46
30: ; preds = %26
%31 = tail call i32 (...) @s_connection()
br label %34
32: ; preds = %7
%33 = tail call i32 (...) @usage()
br label %46
34: ; preds = %30, %22
%35 = phi i32 [ %25, %22 ], [ %31, %30 ]
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = getelementptr inbounds i8*, i8** %1, i64 2
%39 = load i8*, i8** %38
%40 = tail call i32 @handle(i32 %35, i8* %39)
br label %44
41: ; preds = %34
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.4, i64 0, i64 0))
br label %44
44: ; preds = %41, %37
%45 = tail call i32 @close(i32 %35)
br label %46
46: ; preds = %44, %32, %28, %20, %13, %5
%47 = phi i32 [ -1, %5 ], [ -1, %13 ], [ -1, %20 ], [ 0, %44 ], [ -1, %28 ], [ -1, %32 ]
ret i32 %47
}