#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int atoi(char *const);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ getIP(char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int const);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int server(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char *const argv[]) {
  char socket_host[255];
  getIP(socket_host);
  printf("%s\n", socket_host);
  const int port = atoi(argv[1]);
  struct hostent *hostinfo;
  int socket_fd;
  struct sockaddr_in name;
  int client_sent_quit_message;
  socket_fd = socket(PF_INET, SOCK_STREAM, 0);
  name.sin_family = AF_INET;
  hostinfo = gethostbyname(socket_host);
  if (hostinfo == NULL)
    return 1;
  else
    name.sin_addr = *((struct in_addr *)hostinfo->h_addr);
  name.sin_port = htons(port);
  if (bind(socket_fd, (struct sockaddr *)&name, sizeof(struct sockaddr_in)) < 0)
    printf("ERRO NO BIND \n");
  listen(socket_fd, 5);
  do {
    struct sockaddr_in client_name;
    socklen_t client_name_len;
    int client_socket_fd;
    client_socket_fd =
        accept(socket_fd, (struct sockaddr *)&client_name, &client_name_len);
    client_sent_quit_message = server(client_socket_fd);
    close(client_socket_fd);
  } while (!client_sent_quit_message);
  close(socket_fd);
  unlink(socket_host);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
call   10d0 <getIP@plt>
lea    -0x110(%rbp),%rsi
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x114(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x124(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x128(%rbp)
lea    -0x110(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
jne    1268 <main+0xa8>
movl   $0x1,-0x4(%rbp)
jmp    1337 <main+0x177>
mov    -0x120(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x12c(%rbp)
mov    -0x114(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x130(%rbp)
mov    -0x124(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12ba <main+0xfa>
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x124(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
mov    -0x124(%rbp),%edi
lea    -0x140(%rbp),%rsi
lea    -0x144(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x148(%rbp)
mov    -0x148(%rbp),%edi
call   1080 <server@plt>
mov    %eax,-0x134(%rbp)
mov    -0x148(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x134(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12ca <main+0x10a>
mov    -0x124(%rbp),%edi
call   1060 <close@plt>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x120,%rsp
mov    %rsi,%rbx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
call   10d0 <getIP@plt>
mov    %rbp,%rdi
call   1040 <puts@plt>
mov    0x8(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r14d
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r12d
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rbp,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     12ba <main+0xfa>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    1262 <main+0xa2>
lea    0xda3(%rip),%rdi
call   1040 <puts@plt>
mov    %r12d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <server@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
test   %ebx,%ebx
je     1280 <main+0xc0>
mov    %r12d,%edi
call   1060 <close@plt>
lea    0x20(%rsp),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
jmp    12bf <main+0xff>
mov    $0x1,%eax
add    $0x120,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"%s\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str.1 = [15 x i8] c"ERRO NO BIND \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [255 x i8]
%7 = alloca i32
%8 = alloca %struct.hostent*
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%16 = call i32 @getIP(i8* %15)
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %17)
%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* %7
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %9
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %26, i32* %27
%28 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%29 = call %struct.hostent* @gethostbyname(i8* %28)
store %struct.hostent* %29, %struct.hostent** %8
%30 = load %struct.hostent*, %struct.hostent** %8
%31 = icmp eq %struct.hostent* %30, null
br i1 %31, label %32, label %33
32: ; preds = %2
store i32 1, i32* %3
br label %71
33: ; preds = %2
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%35 = load %struct.hostent*, %struct.hostent** %8
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 0
%37 = load i64, i64* %36
%38 = inttoptr i64 %37 to %struct.in_addr*
%39 = bitcast %struct.in_addr* %34 to i8*
%40 = bitcast %struct.in_addr* %38 to i8*
br label %41
41: ; preds = %33
%42 = load i32, i32* %7
%43 = call i32 @htons(i32 %42)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %43, i32* %44
%45 = load i32, i32* %9
%46 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%47 = call i64 @bind(i32 %45, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %41
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %49, %41
%52 = load i32, i32* %9
%53 = call i32 @listen(i32 %52, i32 5)
br label %54
54: ; preds = %62, %51
%55 = load i32, i32* %9
%56 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%57 = call i32 @accept(i32 %55, %struct.sockaddr* %56, i32* %13)
store i32 %57, i32* %14
%58 = load i32, i32* %14
%59 = call i32 @server(i32 %58)
store i32 %59, i32* %11
%60 = load i32, i32* %14
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %54
%63 = load i32, i32* %11
%64 = icmp ne i32 %63, 0
%65 = xor i1 %64, true
br i1 %65, label %54, label %66
66: ; preds = %62
%67 = load i32, i32* %9
%68 = call i32 @close(i32 %67)
%69 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%70 = call i32 @unlink(i8* %69)
store i32 0, i32* %3
br label %71
71: ; preds = %66, %32
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@str = [14 x i8] c"ERRO NO BIND \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [255 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%8 = call i32 @getIP(i8* %7)
%9 = call i32 @puts(i8* %7)
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @atoi(i8* %11)
%13 = bitcast %struct.sockaddr_in* %4 to i8*
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %17, i32* %18
%19 = call %struct.hostent* @gethostbyname(i8* %7)
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %47, label %21
21: ; preds = %2
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 0
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to i32*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%26 = load i32, i32* %24
store i32 %26, i32* %25
%27 = call i32 @htons(i32 %12)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i64 @bind(i32 %16, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %34
34: ; preds = %32, %21
%35 = call i32 @listen(i32 %16, i32 5)
%36 = bitcast %struct.sockaddr_in* %5 to i8*
%37 = bitcast i32* %6 to i8*
%38 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %39
39: ; preds = %39, %34
%40 = call i32 @accept(i32 %16, %struct.sockaddr* %38, i32* %6)
%41 = call i32 @server(i32 %40)
%42 = call i32 @close(i32 %40)
%43 = icmp eq i32 %41, 0
br i1 %43, label %39, label %44
44: ; preds = %39
%45 = call i32 @close(i32 %16)
%46 = call i32 @unlink(i8* %7)
br label %47
47: ; preds = %2, %44
%48 = phi i32 [ 0, %44 ], [ 1, %2 ]
ret i32 %48
}