#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 TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ srv_sockaddr;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read_message(int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ send_message(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  struct sockaddr_in srv_sockaddr;
  struct hostent *server;
  int act, max_fd, client_socket;
  fd_set write_fd_set, read_fd_set, master;
  server = gethostbyname("localhost");
  memset(&srv_sockaddr, 0, sizeof(struct sockaddr_in));
  srv_sockaddr.sin_family = AF_INET;
  memcpy((char *)server->h_addr, (char *)&srv_sockaddr.sin_addr.s_addr,
         server->h_length);
  srv_sockaddr.sin_port = htons(5432);
  client_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (connect(client_socket, (struct sockaddr *)&srv_sockaddr,
              sizeof(srv_sockaddr)) == -1) {
    perror("connect() failed");
    exit(1);
  }
  printf("------ Welcome to FancyChat multiuser SIN HILOS CHACHO!!! ------\n");
  FD_ZERO(&read_fd_set);
  FD_ZERO(&write_fd_set);
  FD_ZERO(&master);
  FD_SET(client_socket, &master);
  FD_SET(0, &master);
  max_fd = client_socket;
  while (1) {
    read_fd_set = master;
    write_fd_set = master;
    act = select(max_fd + 1, &read_fd_set, NULL, NULL, NULL);
    if (act < 0) {
      perror("select() failed");
      exit(1);
    }
    if (FD_ISSET(client_socket, &read_fd_set)) {
      read_message(client_socket);
    } else if (FD_ISSET(0, &read_fd_set)) {
      send_message(client_socket);
    };
  }
  close(client_socket);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xdfa(%rip),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1090 <memcpy@plt>
mov    $0x1538,%edi
call   1040 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
mov    0x2d75(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0xffffffff,%eax
jne    129f <main+0xaf>
lea    0xd7a(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd75(%rip),%rdi
call   1050 <printf@plt>
lea    -0x2c(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
lea    -0x28(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
lea    -0x30(%rbp),%rdi
call   10a0 <FD_ZERO@plt>
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1100 <FD_SET@plt>
xor    %edi,%edi
lea    -0x30(%rbp),%rsi
call   1100 <FD_SET@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10b0 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    132c <main+0x13c>
lea    0xd40(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     134f <main+0x15f>
mov    -0x24(%rbp),%edi
call   10d0 <read_message@plt>
jmp    1371 <main+0x181>
xor    %edi,%edi
lea    -0x2c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     136c <main+0x17c>
mov    -0x24(%rbp),%edi
call   1060 <send_message@plt>
jmp    1371 <main+0x181>
jmp    12e3 <main+0xf3>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xdff(%rip),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,%rbx
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dc4(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x20(%rsp)
mov    0x8(%rbx),%rdi
lea    0x1c(%rsp),%rsi
mov    (%rbx),%edx
call   1090 <memcpy@plt>
mov    $0x1538,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x0(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0xffffffff,%eax
jne    127b <main+0x8b>
lea    0xd91(%rip),%rdi
jmp    12ef <main+0xff>
lea    0xda9(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%r15
mov    %r15,%rdi
call   10a0 <FD_ZERO@plt>
lea    0x14(%rsp),%rdi
call   10a0 <FD_ZERO@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   10a0 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1100 <FD_SET@plt>
xor    %edi,%edi
mov    %rbp,%rsi
call   1100 <FD_SET@plt>
mov    %ebx,%r14d
add    $0x1,%r14d
mov    0x10(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
test   %eax,%eax
jns    12fe <main+0x10e>
lea    0xd2c(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xc(%rsp),%rbp
jmp    1339 <main+0x149>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
call   10d0 <read_message@plt>
mov    0x10(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10b0 <select@plt>
test   %eax,%eax
js     12e8 <main+0xf8>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    1310 <main+0x120>
xor    %edi,%edi
mov    %rbp,%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1317 <main+0x127>
mov    %ebx,%edi
call   1060 <send_message@plt>
jmp    1317 <main+0x127>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"connect() failed\00"
@.str.2 = [66 x i8] c"------ Welcome to FancyChat multiuser SIN HILOS CHACHO!!! ------\0A\00"
@.str.3 = [16 x i8] c"select() failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.hostent*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store %struct.hostent* %10, %struct.hostent** %3
%11 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %12, i32* %13
%14 = load %struct.hostent*, %struct.hostent** %3
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i32 0, i32 1
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to i8*
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
%20 = bitcast i32* %19 to i8*
%21 = load %struct.hostent*, %struct.hostent** %3
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @memcpy(i8* %17, i8* %20, i32 %23)
%25 = call i32 @htons(i32 5432)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%32 = call i32 @connect(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %0
%35 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %0
%38 = call i32 @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @FD_ZERO(i32* %8)
%40 = call i32 @FD_ZERO(i32* %7)
%41 = call i32 @FD_ZERO(i32* %9)
%42 = load i32, i32* %6
%43 = call i32 @FD_SET(i32 %42, i32* %9)
%44 = call i32 @FD_SET(i32 0, i32* %9)
%45 = load i32, i32* %6
store i32 %45, i32* %5
br label %46
46: ; preds = %37, %71
%47 = load i32, i32* %9
store i32 %47, i32* %8
%48 = load i32, i32* %9
store i32 %48, i32* %7
%49 = load i32, i32* %5
%50 = add nsw i32 %49, 1
%51 = call i32 @select(i32 %50, i32* %8, i32* null, i32* null, i32* null)
store i32 %51, i32* %4
%52 = load i32, i32* %4
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %46
%55 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %46
%58 = load i32, i32* %6
%59 = call i64 @FD_ISSET(i32 %58, i32* %8)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = load i32, i32* %6
%63 = call i32 @read_message(i32 %62)
br label %71
64: ; preds = %57
%65 = call i64 @FD_ISSET(i32 0, i32* %8)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i32, i32* %6
%69 = call i32 @send_message(i32 %68)
br label %70
70: ; preds = %67, %64
br label %71
71: ; preds = %70, %61
br label %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"connect() failed\00"
@.str.3 = [16 x i8] c"select() failed\00"
@str = [65 x i8] c"------ Welcome to FancyChat multiuser SIN HILOS CHACHO!!! ------\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast i32* %2 to i8*
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 1
%14 = load i64, i64* %13
%15 = inttoptr i64 %14 to i8*
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
%17 = bitcast i32* %16 to i8*
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @memcpy(i8* %15, i8* %17, i32 %19)
%21 = call i32 @htons(i32 5432)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%27 = call i32 @connect(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %0
%30 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %0
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @str, i64 0, i64 0))
%34 = call i32 @FD_ZERO(i32* %3)
%35 = call i32 @FD_ZERO(i32* %2)
%36 = call i32 @FD_ZERO(i32* %4)
%37 = call i32 @FD_SET(i32 %25, i32* %4)
%38 = call i32 @FD_SET(i32 0, i32* %4)
%39 = add nsw i32 %25, 1
%40 = load i32, i32* %4
store i32 %40, i32* %3
store i32 %40, i32* %2
%41 = call i32 @select(i32 %39, i32* %3, i32* null, i32* null, i32* null)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %56, %32
%44 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %32, %56
%47 = call i64 @FD_ISSET(i32 %25, i32* %3)
%48 = icmp eq i64 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %46
%50 = call i32 @read_message(i32 %25)
br label %56
51: ; preds = %46
%52 = call i64 @FD_ISSET(i32 0, i32* %3)
%53 = icmp eq i64 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %51
%55 = call i32 @send_message(i32 %25)
br label %56
56: ; preds = %51, %54, %49
%57 = load i32, i32* %4
store i32 %57, i32* %3
store i32 %57, i32* %2
%58 = call i32 @select(i32 %39, i32* %3, i32* null, i32* null, i32* null)
%59 = icmp slt i32 %58, 0
br i1 %59, label %43, label %46
}