#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 /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, unsigned int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
scalar_t__ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ puts(char *);
int rand();
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main() {
  char *advice[] = {
      "\nTake smaller bites.\r\n", "\nOne word: inappropriate\r\n",
      "\nI really couldn't advise you on such a matter!\r\n",
      "\nJust for today, be honest: tell the boss what you *really* think.\r\n",
      "\nYou might want to rethink that haircut.\r\n"};
  int listener_d = socket(PF_INET, SOCK_STREAM, 0);
  struct sockaddr_in name;
  name.sin_family = PF_INET;
  name.sin_port = (in_port_t)htons(30000);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  int reuse = 1;
  if (setsockopt(listener_d, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse,
                 sizeof(int)) == -1)
    error("Can't set the 'reuse' option on the socket.");
  if (bind(listener_d, (struct sockaddr *)&name, sizeof(name)) == -1)
    error("Can't bind the port");
  listen(listener_d, 10);
  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);
    char *msg = advice[rand() % 5];
    send(connect_d, msg, strlen(msg), 0);
    close(connect_d);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1090 <memset@plt>
lea    0xdf7(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe03(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe13(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe3a(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe74(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    $0x7530,%edi
call   1060 <htons@plt>
mov    %rax,-0x48(%rbp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x50(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    0x2d26(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12c0 <main+0xe0>
lea    0xe19(%rip),%rdi
call   10d0 <error@plt>
mov    -0x34(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x18,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12e6 <main+0x106>
lea    0xe1f(%rip),%rdi
call   10d0 <error@plt>
mov    -0x34(%rbp),%edi
mov    $0xa,%esi
call   10b0 <listen@plt>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
movl   $0x4,-0x5c(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x58(%rbp),%rsi
lea    -0x5c(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x60(%rbp)
mov    $0x0,%al
call   10f0 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
mov    -0x30(%rbp,%rax,8),%rax
mov    %rax,-0x68(%rbp)
mov    -0x60(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x68(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0x60(%rbp),%edi
call   10a0 <close@plt>
jmp    12ff <main+0x11f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x60,%rsp
lea    0xe1d(%rip),%rax
mov    %rax,0x30(%rsp)
lea    0xe28(%rip),%rax
mov    %rax,0x38(%rsp)
lea    0xe37(%rip),%rax
mov    %rax,0x40(%rsp)
lea    0xe5d(%rip),%rax
mov    %rax,0x48(%rsp)
lea    0xe96(%rip),%rax
mov    %rax,0x50(%rsp)
mov    0x2da9(%rip),%rbx
mov    (%rbx),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%r15d
mov    (%rbx),%eax
mov    %eax,0x28(%rsp)
mov    $0x7530,%edi
call   1060 <htons@plt>
mov    %rax,0x20(%rsp)
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x18(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    0x2d4d(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r15d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1299 <main+0xc9>
lea    0xe40(%rip),%rdi
call   10c0 <error@plt>
lea    0x18(%rsp),%rsi
mov    %r15d,%edi
mov    $0x18,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    12bc <main+0xec>
lea    0xe49(%rip),%rdi
call   10c0 <error@plt>
mov    %r15d,%edi
mov    $0xa,%esi
call   10a0 <listen@plt>
lea    0xe44(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%r14
lea    0x8(%rsp),%r12
nop
movl   $0x4,0x8(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   10d0 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10e0 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
cltq
mov    0x30(%rsp,%rax,8),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    12e0 <main+0x110>

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 = [23 x i8] c"\0ATake smaller bites.\0D\0A\00"
@.str.1 = [27 x i8] c"\0AOne word: inappropriate\0D\0A\00"
@.str.2 = [50 x i8] c"\0AI really couldn't advise you on such a matter!\0D\0A\00"
@.str.3 = [69 x i8] c"\0AJust for today, be honest: tell the boss what you *really* think.\0D\0A\00"
@.str.4 = [43 x i8] c"\0AYou might want to rethink that haircut.\0D\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.5 = [44 x i8] c"Can't set the 'reuse' option on the socket.\00"
@.str.6 = [20 x i8] c"Can't bind the port\00"
@.str.7 = [23 x i8] c"waiting for connection\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [5 x i8*]
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca %struct.sockaddr_storage
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %1
%10 = bitcast [5 x i8*]* %2 to i8*
%11 = bitcast i8* %10 to [5 x i8*]*
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 0
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i32 0, i32 0), i8** %12
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 1
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 2
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i32 0, i32 0), i8** %14
%15 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 3
store i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i32 0, i32 0), i8** %15
%16 = getelementptr inbounds [5 x i8*], [5 x i8*]* %11, i32 0, i32 4
store i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i32 0, i32 0), i8** %16
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %3
%20 = load i32, i32* @PF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %20, i32* %21
%22 = call i64 @htons(i32 30000)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i64 %22, i64* %23
%24 = load i32, i32* @INADDR_ANY
%25 = call i32 @htonl(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
store i32 1, i32* %5
%28 = load i32, i32* %3
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_REUSEADDR
%31 = bitcast i32* %5 to i8*
%32 = call i32 @setsockopt(i32 %28, i32 %29, i32 %30, i8* %31, i32 4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %0
%35 = call i32 @error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %34, %0
%37 = load i32, i32* %3
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = call i32 @bind(i32 %37, %struct.sockaddr* %38, i32 24)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = load i32, i32* %3
%45 = call i32 @listen(i32 %44, i32 10)
%46 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
br label %47
47: ; preds = %43, %47
store i32 4, i32* %7
%48 = load i32, i32* %3
%49 = bitcast %struct.sockaddr_storage* %6 to %struct.sockaddr*
%50 = call i32 @accept(i32 %48, %struct.sockaddr* %49, i32* %7)
store i32 %50, i32* %8
%51 = call i32 (...) @rand()
%52 = srem i32 %51, 5
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 %53
%55 = load i8*, i8** %54
store i8* %55, i8** %9
%56 = load i32, i32* %8
%57 = load i8*, i8** %9
%58 = load i8*, i8** %9
%59 = call i32 @strlen(i8* %58)
%60 = call i32 @send(i32 %56, i8* %57, i32 %59, i32 0)
%61 = load i32, i32* %8
%62 = call i32 @close(i32 %61)
br label %47
}

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 = [23 x i8] c"\0ATake smaller bites.\0D\0A\00"
@.str.1 = [27 x i8] c"\0AOne word: inappropriate\0D\0A\00"
@.str.2 = [50 x i8] c"\0AI really couldn't advise you on such a matter!\0D\0A\00"
@.str.3 = [69 x i8] c"\0AJust for today, be honest: tell the boss what you *really* think.\0D\0A\00"
@.str.4 = [43 x i8] c"\0AYou might want to rethink that haircut.\0D\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.5 = [44 x i8] c"Can't set the 'reuse' option on the socket.\00"
@.str.6 = [20 x i8] c"Can't bind the port\00"
@.str.7 = [23 x i8] c"waiting for connection\00"
define i32 @main() {
%1 = alloca [5 x i8*]
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = bitcast [5 x i8*]* %1 to i8*
%7 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 0
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %7
%8 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 1
store i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8** %8
%9 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 2
store i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8** %9
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 3
store i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 4
store i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8** %11
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = bitcast %struct.sockaddr_in* %2 to i8*
%16 = load i32, i32* @PF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %16, i32* %17
%18 = tail call i64 @htons(i32 30000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i64 %18, i64* %19
%20 = load i32, i32* @INADDR_ANY
%21 = tail call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %14, i32 %24, i32 %25, i8* %23, i32 4)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %0
%29 = call i32 @error(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
br label %30
30: ; preds = %28, %0
%31 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%32 = call i32 @bind(i32 %14, %struct.sockaddr* %31, i32 24)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
%37 = call i32 @listen(i32 %14, i32 10)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0))
%39 = bitcast %struct.sockaddr_storage* %4 to i8*
%40 = bitcast i32* %5 to i8*
%41 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
br label %42
42: ; preds = %42, %36
store i32 4, i32* %5
%43 = call i32 @accept(i32 %14, %struct.sockaddr* %41, i32* %5)
%44 = call i32 (...) @rand()
%45 = srem i32 %44, 5
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 %46
%48 = load i8*, i8** %47
%49 = call i32 @strlen(i8* %48)
%50 = call i32 @send(i32 %43, i8* %48, i32 %49, i32 0)
%51 = call i32 @close(i32 %43)
br label %42
}