#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_storage {
  int dummy;
};
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ name;
typedef scalar_t__ in_port_t;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, unsigned int *);
int atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
scalar_t__ htons(int);
int listen(int, int);
int /*<<< orphan*/ puts(char *);
int rand();
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  int service_port = 30000;
  if (argc > 1) {
    service_port = atoi(argv[1]);
  }
  char *advice[] = {"Take smaller bites\n", "Go for the tight jeans\n",
                    "One word: inappropriate\n",
                    "You might want to rethink that haircut\n"};
  int listener_d = socket(PF_INET, SOCK_STREAM, 0);
  if (listener_d == -1)
    error("Unable to create a socket");
  struct sockaddr_in name;
  name.sin_family = PF_INET;
  name.sin_port = (in_port_t)htons(service_port);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  int c = bind(listener_d, (struct sockaddr *)&name, sizeof(name));
  if (c == -1) {
    char err_msg[80];
    sprintf(err_msg, "Unable to bind the socket to localhost:%i", service_port);
    error(err_msg);
  }
  c = listen(listener_d, 10);
  if (c == -1)
    error("Unable to listen for connections");
  puts("Waiting for connection");
  while (1) {
    struct sockaddr_storage client_addr;
    unsigned int address_size = sizeof(client_addr);
    int connect_d =
        accept(listener_d, (struct sockaddr *)&client_addr, &address_size);
    if (connect_d == -1)
      error("Unable to open secondary socket");
    char *msg = advice[rand() % 4];
    c = send(connect_d, msg, strlen(msg), 0);
    if (c == -1)
      error("Unable to send the message");
    close(connect_d);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x7530,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    121a <main+0x3a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2bbf(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2bbc(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2bb9(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2bb6(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1278 <main+0x98>
lea    0xdfa(%rip),%rdi
call   10b0 <error@plt>
mov    0x2d49(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %rax,-0x58(%rbp)
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x60(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x18,%edx
call   10a0 <bind@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    12e1 <main+0x101>
lea    -0xc0(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xdb7(%rip),%rsi
call   10e0 <sprintf@plt>
lea    -0xc0(%rbp),%rdi
call   10b0 <error@plt>
mov    -0x44(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    1307 <main+0x127>
lea    0xdaf(%rip),%rdi
call   10b0 <error@plt>
lea    0xdc4(%rip),%rdi
call   1030 <puts@plt>
movl   $0x4,-0xcc(%rbp)
mov    -0x44(%rbp),%edi
lea    -0xc8(%rbp),%rsi
lea    -0xcc(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xd0(%rbp)
cmpl   $0xffffffff,-0xd0(%rbp)
jne    1352 <main+0x172>
lea    0xd9c(%rip),%rdi
call   10b0 <error@plt>
mov    $0x0,%al
call   10f0 <rand@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
mov    -0x40(%rbp,%rax,8),%rax
mov    %rax,-0xd8(%rbp)
mov    -0xd0(%rbp),%eax
mov    %eax,-0xe4(%rbp)
mov    -0xd8(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0xd8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xe4(%rbp),%edi
mov    -0xe0(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    13c5 <main+0x1e5>
lea    0xd49(%rip),%rdi
call   10b0 <error@plt>
mov    -0xd0(%rbp),%edi
call   1080 <close@plt>
jmp    1313 <main+0x133>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    $0x7530,%ebp
cmp    $0x2,%edi
jl     1203 <main+0x23>
mov    0x8(%rsi),%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebp
mov    0x2dbe(%rip),%r14
mov    (%r14),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1230 <main+0x50>
lea    0xe42(%rip),%rdi
call   10b0 <error@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %rax,0x10(%rsp)
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x18,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1289 <main+0xa9>
lea    0xe15(%rip),%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%edx
call   10e0 <sprintf@plt>
mov    %r14,%rdi
call   10b0 <error@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    12a6 <main+0xc6>
lea    0xe10(%rip),%rdi
call   10b0 <error@plt>
lea    0xe25(%rip),%rdi
call   1030 <puts@plt>
lea    0x20(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0x2b1d(%rip),%r13
jmp    12d7 <main+0xf7>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
call   1080 <close@plt>
movl   $0x4,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12ff <main+0x11f>
lea    0xdef(%rip),%rdi
call   10b0 <error@plt>
xor    %eax,%eax
call   10f0 <rand@plt>
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffffc,%ecx
sub    %ecx,%eax
cltq
mov    0x0(%r13,%rax,8),%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    12d0 <main+0xf0>
lea    0xdcd(%rip),%rdi
call   10b0 <error@plt>
jmp    12d0 <main+0xf0>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"Take smaller bites\0A\00"
@.str.1 = [24 x i8] c"Go for the tight jeans\0A\00"
@.str.2 = [25 x i8] c"One word: inappropriate\0A\00"
@.str.3 = [40 x i8] c"You might want to rethink that haircut\0A\00"
@__const.main.advice = [4 x i8*] [i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i32 0, i32 0)]
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [26 x i8] c"Unable to create a socket\00"
@INADDR_ANY = global i32 0
@.str.5 = [42 x i8] c"Unable to bind the socket to localhost:%i\00"
@.str.6 = [33 x i8] c"Unable to listen for connections\00"
@.str.7 = [23 x i8] c"Waiting for connection\00"
@.str.8 = [32 x i8] c"Unable to open secondary socket\00"
@.str.9 = [27 x i8] c"Unable to send the message\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [4 x i8*]
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca [80 x i8]
%12 = alloca %struct.sockaddr_storage
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 30000, i32* %6
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 1
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %6
br label %23
23: ; preds = %18, %2
%24 = bitcast [4 x i8*]* %7 to i8*
%25 = load i32, i32* @PF_INET
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %23
%33 = load i32, i32* @PF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %33, i32* %34
%35 = load i32, i32* %6
%36 = call i64 @htons(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i64 %36, i64* %37
%38 = load i32, i32* @INADDR_ANY
%39 = call i32 @htonl(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%41 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %40, i32 0, i32 0
store i32 %39, i32* %41
%42 = load i32, i32* %8
%43 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%44 = call i32 @bind(i32 %42, %struct.sockaddr* %43, i32 24)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %53
47: ; preds = %32
%48 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%49 = load i32, i32* %6
%50 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %49)
%51 = getelementptr inbounds [80 x i8], [80 x i8]* %11, i64 0, i64 0
%52 = call i32 @error(i8* %51)
br label %53
53: ; preds = %47, %32
%54 = load i32, i32* %8
%55 = call i32 @listen(i32 %54, i32 10)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = call i32 @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
br label %60
60: ; preds = %58, %53
%61 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %62
62: ; preds = %60, %85
store i32 4, i32* %13
%63 = load i32, i32* %8
%64 = bitcast %struct.sockaddr_storage* %12 to %struct.sockaddr*
%65 = call i32 @accept(i32 %63, %struct.sockaddr* %64, i32* %13)
store i32 %65, i32* %14
%66 = load i32, i32* %14
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %70
68: ; preds = %62
%69 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0))
br label %70
70: ; preds = %68, %62
%71 = call i32 (...) @rand()
%72 = srem i32 %71, 4
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [4 x i8*], [4 x i8*]* %7, i64 0, i64 %73
%75 = load i8*, i8** %74
store i8* %75, i8** %15
%76 = load i32, i32* %14
%77 = load i8*, i8** %15
%78 = load i8*, i8** %15
%79 = call i32 @strlen(i8* %78)
%80 = call i32 @send(i32 %76, i8* %77, i32 %79, i32 0)
store i32 %80, i32* %10
%81 = load i32, i32* %10
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %85
83: ; preds = %70
%84 = call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
br label %85
85: ; preds = %83, %70
%86 = load i32, i32* %14
%87 = call i32 @close(i32 %86)
br label %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"Take smaller bites\0A\00"
@.str.1 = [24 x i8] c"Go for the tight jeans\0A\00"
@.str.2 = [25 x i8] c"One word: inappropriate\0A\00"
@.str.3 = [40 x i8] c"You might want to rethink that haircut\0A\00"
@__const.main.advice = [4 x i8*] [i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i32 0, i32 0)]
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [26 x i8] c"Unable to create a socket\00"
@INADDR_ANY = global i32 0
@.str.5 = [42 x i8] c"Unable to bind the socket to localhost:%i\00"
@.str.6 = [33 x i8] c"Unable to listen for connections\00"
@.str.7 = [23 x i8] c"Waiting for connection\00"
@.str.8 = [32 x i8] c"Unable to open secondary socket\00"
@.str.9 = [27 x i8] c"Unable to send the message\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [80 x i8]
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32 @atoi(i8* %10)
br label %12
12: ; preds = %8, %2
%13 = phi i32 [ %11, %8 ], [ 30000, %2 ]
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = bitcast %struct.sockaddr_in* %3 to i8*
%22 = load i32, i32* @PF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %22, i32* %23
%24 = tail call i64 @htons(i32 %13)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i64 %24, i64* %25
%26 = load i32, i32* @INADDR_ANY
%27 = tail call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @bind(i32 %16, %struct.sockaddr* %29, i32 24)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %20
%33 = getelementptr inbounds [80 x i8], [80 x i8]* %4, i64 0, i64 0
%34 = call i32 @sprintf(i8* %33, i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32 %13)
%35 = call i32 @error(i8* %33)
br label %36
36: ; preds = %32, %20
%37 = call i32 @listen(i32 %16, i32 10)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
%43 = bitcast %struct.sockaddr_storage* %5 to i8*
%44 = bitcast i32* %6 to i8*
%45 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
br label %46
46: ; preds = %62, %41
store i32 4, i32* %6
%47 = call i32 @accept(i32 %16, %struct.sockaddr* %45, i32* %6)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
%52 = call i32 (...) @rand()
%53 = srem i32 %52, 4
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [4 x i8*], [4 x i8*]* @__const.main.advice, i64 0, i64 %54
%56 = load i8*, i8** %55
%57 = call i32 @strlen(i8* %56)
%58 = call i32 @send(i32 %47, i8* %56, i32 %57, i32 0)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %51
%61 = call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.9, i64 0, i64 0))
br label %62
62: ; preds = %60, %51
%63 = call i32 @close(i32 %47)
br label %46
}