#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_family;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ saddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(void *, void *, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(void *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int build_server() {
  struct sockaddr_in saddr;
  int socket_id;
  struct hostent *hp;
  char hostname[256];
  socket_id = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_id == -1) {
    perror("socket");
    exit(1);
  };
  bzero((void *)&saddr, sizeof(saddr));
  gethostname(hostname, 256);
  hp = gethostbyname(hostname);
  bcopy((void *)hp->h_addr, (void *)&saddr.sin_addr, hp->h_length);
  saddr.sin_port = htons(8080);
  saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  saddr.sin_family = AF_INET;
  if (bind(socket_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0) {
    perror("bind");
    exit(1);
  };
  if (listen(socket_id, 1) != 0) {
    perror("listen");
    exit(1);
  };
  printf("Ready for client...\n");
  return socket_id;
}


x86 assembly (clang, -O0):
<build_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1207 <build_server+0x47>
lea    0xe08(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x10(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10c0 <gethostname@plt>
lea    -0x120(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1080 <bcopy@plt>
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,-0x8(%rbp)
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     12ac <build_server+0xec>
lea    0xd6a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
je     12d9 <build_server+0x119>
lea    0xd42(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd33(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_server>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    0x2e13(%rip),%r12
mov    (%r12),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     1297 <build_server+0xd7>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x100,%esi
call   10c0 <gethostname@plt>
mov    %r15,%rdi
call   1060 <gethostbyname@plt>
mov    0x8(%rax),%rsi
lea    0x4(%rsp),%rdi
mov    (%rax),%edx
call   1090 <memmove@plt>
mov    $0x1f90,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    (%r12),%eax
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jne    12a0 <build_server+0xe0>
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
test   %rax,%rax
jne    12a9 <build_server+0xe9>
lea    0xd92(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd62(%rip),%rdi
jmp    12b0 <build_server+0xf0>
lea    0xd60(%rip),%rdi
jmp    12b0 <build_server+0xf0>
lea    0xd5c(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [21 x i8] c"Ready for client...\0A\00"
define i32 @build_server() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca %struct.hostent*
%4 = alloca [256 x i8]
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = bitcast %struct.sockaddr_in* %1 to i8*
%15 = call i32 @bzero(i8* %14, i32 12)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%17 = call i32 @gethostname(i8* %16, i32 256)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %3
%20 = load %struct.hostent*, %struct.hostent** %3
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 1
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i8*
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 1
%25 = bitcast %struct.TYPE_2__* %24 to i8*
%26 = load %struct.hostent*, %struct.hostent** %3
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @bcopy(i8* %23, i8* %25, i32 %28)
%30 = call i32 @htons(i32 8080)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i32, i32* @INADDR_ANY
%33 = call i32 @htonl(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 1
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %2
%39 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%40 = call i64 @bind(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %13
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %13
%46 = load i32, i32* %2
%47 = call i64 @listen(i32 %46, i32 1)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %45
%53 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%54 = load i32, i32* %2
ret i32 %54
}

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 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@str = [20 x i8] c"Ready for client...\00"
define i32 @build_server() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [256 x i8]
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i32 @bzero(i8* %3, i32 12)
%14 = call i32 @gethostname(i8* %4, i32 256)
%15 = call %struct.hostent* @gethostbyname(i8* %4)
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 1
%17 = load i64, i64* %16
%18 = inttoptr i64 %17 to i8*
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%20 = bitcast %struct.TYPE_2__* %19 to i8*
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @htons(i32 8080)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = call i32 @htonl(i32 %25)
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %19, i64 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%31 = call i64 @bind(i32 %7, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %12
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %12
%37 = call i64 @listen(i32 %7, i32 1)
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %36
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
ret i32 %7
}