#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 */
typedef int /*<<< orphan*/ yes;
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 socklen_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ throw(char *);
int make_server_socket() {
  int sockfd, newsockfd, portno;
  struct hostent *server;
  int yes = 1;
  socklen_t clilen;
  struct sockaddr_in serv_addr, cli_addr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    throw("ERROR opening socket");
  if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) <
      0)
    throw("ERROR while setting socket option");
  server = gethostbyname("localhost");
  if (server == NULL) {
    fprintf(stderr, "ERROR, no such host\n");
    exit(0);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  portno = 7373;
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(portno);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    throw("ERROR on binding");
  printf("server waiting\n");
  listen(sockfd, 5);
  clilen = sizeof(cli_addr);
  newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
  if (newsockfd < 0)
    throw("ERROR on accept");
  return newsockfd;
}


x86 assembly (clang, -O0):
<make_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x1,-0x1c(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    1211 <make_server_socket+0x41>
lea    0xdf4(%rip),%rdi
call   1080 <throw@plt>
mov    -0x4(%rbp),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    124b <make_server_socket+0x7b>
lea    0xdcf(%rip),%rdi
call   1080 <throw@plt>
lea    0xde5(%rip),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1282 <make_server_socket+0xb2>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
movl   $0x1ccd,-0xc(%rbp)
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bcopy@plt>
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12f0 <make_server_socket+0x120>
lea    0xd6b(%rip),%rdi
call   1080 <throw@plt>
lea    0xd70(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1339 <make_server_socket+0x169>
lea    0xd43(%rip),%rdi
call   1080 <throw@plt>
mov    -0x8(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_server_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
movl   $0x1,0x4(%rsp)
mov    0x2df7(%rip),%r15
mov    (%r15),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    120e <make_server_socket+0x3e>
lea    0xdf7(%rip),%rdi
call   1080 <throw@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jns    1243 <make_server_socket+0x73>
lea    0xdd7(%rip),%rdi
call   1080 <throw@plt>
lea    0xded(%rip),%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     12ff <make_server_socket+0x12f>
mov    %rax,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    0x8(%rbx),%rsi
lea    0xc(%rsp),%rdi
mov    (%rbx),%edx
call   10b0 <memmove@plt>
mov    $0x1ccd,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    12b2 <make_server_socket+0xe2>
lea    0xda9(%rip),%rdi
call   1080 <throw@plt>
lea    0xdbe(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0xc,(%rsp)
lea    0x18(%rsp),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
call   10d0 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    12f2 <make_server_socket+0x122>
lea    0xd7a(%rip),%rdi
call   1080 <throw@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
lea    0xd32(%rip),%rsi
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32, i64 }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [34 x i8] c"ERROR while setting socket option\00"
@.str.2 = [10 x i8] c"localhost\00"
@stderr = global i32 0
@.str.3 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.4 = [17 x i8] c"ERROR on binding\00"
@.str.5 = [16 x i8] c"server waiting\0A\00"
@.str.6 = [16 x i8] c"ERROR on accept\00"
define i32 @make_server_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.hostent*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
store i32 1, i32* %5
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %1
%12 = load i32, i32* %1
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @throw(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = load i32, i32* %1
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_REUSEADDR
%20 = bitcast i32* %5 to i8*
%21 = call i64 @setsockopt(i32 %17, i32 %18, i32 %19, i8* %20, i32 4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = call i32 @throw(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %16
%26 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store %struct.hostent* %26, %struct.hostent** %4
%27 = load %struct.hostent*, %struct.hostent** %4
%28 = icmp eq %struct.hostent* %27, null
br i1 %28, label %29, label %33
29: ; preds = %25
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %25
%34 = bitcast %struct.sockaddr_in* %7 to i8*
%35 = call i32 @bzero(i8* %34, i32 12)
store i32 7373, i32* %3
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %36, i32* %37
%38 = load %struct.hostent*, %struct.hostent** %4
%39 = getelementptr inbounds %struct.hostent, %struct.hostent* %38, i32 0, i32 1
%40 = load i64, i64* %39
%41 = inttoptr i64 %40 to i8*
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
%44 = bitcast i32* %43 to i8*
%45 = load %struct.hostent*, %struct.hostent** %4
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @bcopy(i8* %41, i8* %44, i32 %47)
%49 = load i32, i32* %3
%50 = call i32 @htons(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %50, i32* %51
%52 = load i32, i32* %1
%53 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%54 = call i64 @bind(i32 %52, %struct.sockaddr* %53, i32 12)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %33
%57 = call i32 @throw(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %56, %33
%59 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%60 = load i32, i32* %1
%61 = call i32 @listen(i32 %60, i32 5)
store i32 12, i32* %6
%62 = load i32, i32* %1
%63 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%64 = call i32 @accept(i32 %62, %struct.sockaddr* %63, i32* %6)
store i32 %64, i32* %2
%65 = load i32, i32* %2
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %58
%68 = call i32 @throw(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %69
69: ; preds = %67, %58
%70 = load i32, i32* %2
ret i32 %70
}

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 = [21 x i8] c"ERROR opening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [34 x i8] c"ERROR while setting socket option\00"
@.str.2 = [10 x i8] c"localhost\00"
@stderr = global i32 0
@.str.3 = [21 x i8] c"ERROR, no such host\0A\00"
@.str.4 = [17 x i8] c"ERROR on binding\00"
@.str.6 = [16 x i8] c"ERROR on accept\00"
@str = [15 x i8] c"server waiting\00"
define i32 @make_server_socket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%6 = bitcast i32* %2 to i8*
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @throw(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i64 @setsockopt(i32 %11, i32 %16, i32 %17, i8* %5, i32 4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @throw(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %22
%30 = call i32 @bzero(i8* %7, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i64 0, i32 1
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to i8*
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%37 = bitcast i32* %36 to i8*
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @htons(i32 7373)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %40, i32* %41
%42 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%43 = call i64 @bind(i32 %11, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %29
%46 = call i32 @throw(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %45, %29
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%49 = call i32 @listen(i32 %11, i32 5)
store i32 12, i32* %2
%50 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%51 = call i32 @accept(i32 %11, %struct.sockaddr* %50, i32* %2)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @throw(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %47
ret i32 %51
}