#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 sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ loc_serv_addr;

/* Variables and functions */
int const EOF;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ feof(int /*<<< orphan*/);
char *fgets(char *, int, int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ getsockname(int, struct sockaddr *, int *);
int listen_and_accept(int);
int ntohs(int /*<<< orphan*/);
int open_socket();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int write(int, char *, size_t const);
int main() {
  int server_socket = 0;
  int accepted_socket = 0;
  while (!feof(stdin)) {
    int const c = getchar();
    if (c == EOF) {
      break;
    }
    switch (c) {
    case 'o': {
      struct sockaddr_in loc_serv_addr;
      socklen_t addrlen = sizeof(loc_serv_addr);
      server_socket = open_socket();
      getsockname(server_socket, (struct sockaddr *)&loc_serv_addr, &addrlen);
      printf("%d\n", ntohs(loc_serv_addr.sin_port));
    } break;
    case 'a': {
      accepted_socket = listen_and_accept(server_socket);
      printf("1\n");
    } break;
    case 'r': {
      char buffer[256];
      bzero(buffer, 256);
      int const n = read(accepted_socket, buffer, 255);
      if (n < 0) {
        perror("ERROR reading from socket");
        exit(1);
      }
      printf("%s\n", buffer);
    } break;
    case 'w': {
      char text[256];
      char const *fs = fgets(text, 256, stdin);
      if (fs == 0) {
        perror("text");
        exit(1);
      }
      size_t const cnt = strlen(text) - 1;
      ssize_t const w = write(accepted_socket, text, cnt);
      if (w == -1) {
        perror("write");
        exit(1);
      }
    } break;
    case 'l': {
      close(accepted_socket);
    } break;
    case 'p':
      printf("pong\n");
      break;
    case '\n':
      break;
    default:
      printf("Unknown command [%c]\n", c);
      abort();
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   10c0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1243 <main+0x43>
jmp    149a <main+0x29a>
mov    $0x0,%al
call   10b0 <getchar@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    0x2d89(%rip),%rcx
cmp    (%rcx),%eax
jne    1264 <main+0x64>
jmp    149a <main+0x29a>
mov    -0x10(%rbp),%eax
mov    %eax,-0x248(%rbp)
sub    $0xa,%eax
je     1478 <main+0x278>
jmp    127b <main+0x7b>
mov    -0x248(%rbp),%eax
sub    $0x61,%eax
je     1331 <main+0x131>
jmp    128f <main+0x8f>
mov    -0x248(%rbp),%eax
sub    $0x6c,%eax
je     1458 <main+0x258>
jmp    12a3 <main+0xa3>
mov    -0x248(%rbp),%eax
sub    $0x6f,%eax
je     12f3 <main+0xf3>
jmp    12b7 <main+0xb7>
mov    -0x248(%rbp),%eax
sub    $0x70,%eax
je     1465 <main+0x265>
jmp    12cb <main+0xcb>
mov    -0x248(%rbp),%eax
sub    $0x72,%eax
je     134f <main+0x14f>
jmp    12df <main+0xdf>
mov    -0x248(%rbp),%eax
sub    $0x77,%eax
je     13b7 <main+0x1b7>
jmp    147d <main+0x27d>
movl   $0x4,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <open_socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1110 <getsockname@plt>
mov    -0x18(%rbp),%edi
call   10e0 <ntohs@plt>
mov    %eax,%esi
lea    0xcdf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1495 <main+0x295>
mov    -0x8(%rbp),%edi
call   10d0 <listen_and_accept@plt>
mov    %eax,-0xc(%rbp)
lea    0xcc5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1495 <main+0x295>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1100 <bzero@plt>
mov    -0xc(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1090 <read@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    139d <main+0x19d>
lea    0xc7d(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    -0x120(%rbp),%rsi
lea    0xc7a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1495 <main+0x295>
lea    -0x230(%rbp),%rdi
mov    0x2c23(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   10a0 <fgets@plt>
mov    %rax,-0x238(%rbp)
cmpq   $0x0,-0x238(%rbp)
jne    13fc <main+0x1fc>
lea    0xc3c(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
lea    -0x230(%rbp),%rdi
call   1060 <strlen@plt>
sub    $0x1,%eax
cltq
mov    %rax,-0x240(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    -0x240(%rbp),%rdx
call   1040 <write@plt>
mov    %eax,-0x244(%rbp)
cmpl   $0xffffffff,-0x244(%rbp)
jne    1453 <main+0x253>
lea    0xbea(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%edi
call   1120 <exit@plt>
jmp    1495 <main+0x295>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
jmp    1495 <main+0x295>
lea    0xbc8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1495 <main+0x295>
jmp    1495 <main+0x295>
mov    -0x10(%rbp),%esi
lea    0xbb3(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    1220 <main+0x20>
xor    %eax,%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    0x2dc0(%rip),%r15
mov    (%r15),%edi
call   10d0 <feof@plt>
test   %eax,%eax
je     1248 <main+0x38>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb1(%rip),%r12
lea    0x10(%rsp),%rbx
lea    0xe44(%rip),%r13
xor    %r14d,%r14d
xor    %ebp,%ebp
jmp    12ac <main+0x9c>
movl   $0x4,0xc(%rsp)
xor    %eax,%eax
call   1060 <open_socket@plt>
mov    %eax,%r14d
mov    %eax,%edi
mov    %rbx,%rsi
lea    0xc(%rsp),%rdx
call   1120 <getsockname@plt>
mov    0x10(%rsp),%edi
call   10f0 <ntohs@plt>
lea    0xdcd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
nopl   0x0(%rax)
mov    (%r15),%edi
call   10d0 <feof@plt>
test   %eax,%eax
jne    1234 <main+0x24>
xor    %eax,%eax
call   10c0 <getchar@plt>
lea    -0x61(%rax),%ecx
cmp    $0x16,%ecx
ja     12dc <main+0xcc>
movslq (%r12,%rcx,4),%rcx
add    %r12,%rcx
jmp    *%rcx
mov    %r14d,%edi
call   10e0 <listen_and_accept@plt>
mov    %eax,%ebp
lea    0xdcf(%rip),%rdi
call   1040 <puts@plt>
jmp    12a0 <main+0x90>
cmp    $0xa,%eax
je     12a0 <main+0x90>
jmp    135f <main+0x14f>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    12a0 <main+0x90>
mov    %r13,%rdi
call   1040 <puts@plt>
jmp    12a0 <main+0x90>
mov    %rbx,%rdi
mov    $0x100,%esi
call   1110 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   10a0 <read@plt>
test   %eax,%eax
js     137e <main+0x16e>
mov    %rbx,%rdi
call   1040 <puts@plt>
jmp    12a0 <main+0x90>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10b0 <fgets@plt>
test   %rax,%rax
je     1387 <main+0x177>
mov    %rbx,%rdi
call   1070 <strlen@plt>
add    $0xffffffff,%eax
movslq %eax,%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <write@plt>
cmp    $0xffffffff,%eax
jne    12a0 <main+0x90>
lea    0xd26(%rip),%rdi
jmp    138e <main+0x17e>
test   %eax,%eax
je     1234 <main+0x24>
lea    0xd1b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
call   1030 <abort@plt>
lea    0xcdf(%rip),%rdi
jmp    138e <main+0x17e>
lea    0xcf0(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%edi
call   1130 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stdin = global i32 0
@EOF = constant i32 0
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [3 x i8] c"1\0A\00"
@.str.2 = [26 x i8] c"ERROR reading from socket\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [5 x i8] c"text\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [6 x i8] c"pong\0A\00"
@.str.7 = [22 x i8] c"Unknown command [%c]\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
%9 = alloca [256 x i8]
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 0, i32* %3
br label %13
13: ; preds = %87, %0
%14 = load i32, i32* @stdin
%15 = call i32 @feof(i32 %14)
%16 = icmp ne i32 %15, 0
%17 = xor i1 %16, true
br i1 %17, label %18, label %88
18: ; preds = %13
%19 = call i32 (...) @getchar()
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %18
br label %88
24: ; preds = %18
%25 = load i32, i32* %4
switch i32 %25, label %83 [
i32 111, label %26
i32 97, label %35
i32 114, label %39
i32 119, label %53
i32 108, label %77
i32 112, label %80
i32 10, label %82
]
26: ; preds = %24
store i32 4, i32* %6
%27 = call i32 (...) @open_socket()
store i32 %27, i32* %2
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%30 = call i32 @getsockname(i32 %28, %struct.sockaddr* %29, i32* %6)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @ntohs(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %33)
br label %87
35: ; preds = %24
%36 = load i32, i32* %2
%37 = call i32 @listen_and_accept(i32 %36)
store i32 %37, i32* %3
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %87
39: ; preds = %24
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%41 = call i32 @bzero(i8* %40, i32 256)
%42 = load i32, i32* %3
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%44 = call i32 @read(i32 %42, i8* %43, i32 255)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %39
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %51)
br label %87
53: ; preds = %24
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%55 = load i32, i32* @stdin
%56 = call i8* @fgets(i8* %54, i32 256, i32 %55)
store i8* %56, i8** %10
%57 = load i8*, i8** %10
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %53
%63 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%64 = call i32 @strlen(i8* %63)
%65 = sub nsw i32 %64, 1
%66 = sext i32 %65 to i64
store i64 %66, i64* %11
%67 = load i32, i32* %3
%68 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%69 = load i64, i64* %11
%70 = call i32 @write(i32 %67, i8* %68, i64 %69)
store i32 %70, i32* %12
%71 = load i32, i32* %12
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %76
73: ; preds = %62
%74 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %62
br label %87
77: ; preds = %24
%78 = load i32, i32* %3
%79 = call i32 @close(i32 %78)
br label %87
80: ; preds = %24
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %87
82: ; preds = %24
br label %87
83: ; preds = %24
%84 = load i32, i32* %4
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i32 %84)
%86 = call i32 (...) @abort()
unreachable
87: ; preds = %82, %80, %77, %76, %50, %35, %26
br label %13
88: ; preds = %23, %13
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stdin = global i32 0
@EOF = constant i32 0
@.str = [4 x i8] c"%d\0A\00"
@.str.2 = [26 x i8] c"ERROR reading from socket\00"
@.str.4 = [5 x i8] c"text\00"
@.str.5 = [6 x i8] c"write\00"
@.str.7 = [22 x i8] c"Unknown command [%c]\0A\00"
@str = [5 x i8] c"pong\00"
@str.8 = [2 x i8] c"1\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = load i32, i32* @stdin
%6 = call i32 @feof(i32 %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %67
8: ; preds = %0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%11 = bitcast %struct.sockaddr_in* %1 to i8*
%12 = bitcast i32* %2 to i8*
%13 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
br label %15
15: ; preds = %8, %61
%16 = phi i32 [ 0, %8 ], [ %63, %61 ]
%17 = phi i32 [ 0, %8 ], [ %62, %61 ]
%18 = call i32 (...) @getchar()
switch i32 %18, label %58 [
i32 0, label %67
i32 111, label %19
i32 97, label %25
i32 114, label %28
i32 119, label %37
i32 108, label %54
i32 112, label %56
i32 10, label %61
]
19: ; preds = %15
store i32 4, i32* %2
%20 = call i32 (...) @open_socket()
%21 = call i32 @getsockname(i32 %20, %struct.sockaddr* %13, i32* %2)
%22 = load i32, i32* %14
%23 = call i32 @ntohs(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %61
25: ; preds = %15
%26 = call i32 @listen_and_accept(i32 %16)
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str.8, i64 0, i64 0))
br label %61
28: ; preds = %15
%29 = call i32 @bzero(i8* %10, i32 256)
%30 = call i32 @read(i32 %17, i8* %10, i32 255)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %28
%36 = call i32 @puts(i8* %10)
br label %61
37: ; preds = %15
%38 = load i32, i32* @stdin
%39 = call i8* @fgets(i8* %9, i32 256, i32 %38)
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %37
%45 = call i32 @strlen(i8* %9)
%46 = add nsw i32 %45, -1
%47 = sext i32 %46 to i64
%48 = call i32 @write(i32 %17, i8* %9, i64 %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %44
br label %61
54: ; preds = %15
%55 = call i32 @close(i32 %17)
br label %61
56: ; preds = %15
%57 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %61
58: ; preds = %15
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i32 %18)
%60 = call i32 (...) @abort()
unreachable
61: ; preds = %19, %25, %35, %53, %54, %56, %15
%62 = phi i32 [ %17, %56 ], [ %17, %54 ], [ %17, %53 ], [ %17, %35 ], [ %26, %25 ], [ %17, %19 ], [ %17, %15 ]
%63 = phi i32 [ %16, %56 ], [ %16, %54 ], [ %16, %53 ], [ %16, %35 ], [ %16, %25 ], [ %20, %19 ], [ %16, %15 ]
%64 = load i32, i32* @stdin
%65 = call i32 @feof(i32 %64)
%66 = icmp eq i32 %65, 0
br i1 %66, label %15, label %67
67: ; preds = %61, %15, %0
ret i32 0
}