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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, int /*<<< orphan*/ *,
                         int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int make_server_socket_q(int portnum, int backlog) {
  struct sockaddr_in saddr;
  struct hostent *hp;
  char hostname[256];
  int sock_id;
  sock_id = socket(PF_INET, SOCK_STREAM, 0);
  if (sock_id == -1)
    return -1;
  bzero(&saddr, sizeof(saddr));
  gethostname(hostname, 256);
  hp = gethostbyname(hostname);
  bcopy(hp->h_addr, &saddr.sin_addr, hp->h_length);
  saddr.sin_port = htons(portnum);
  saddr.sin_family = AF_INET;
  if (bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0)
    return -1;
  if (listen(sock_id, backlog) != 0)
    return -1;
  return sock_id;
}


x86 assembly (clang, -O0):
<make_server_socket_q>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    11c9 <make_server_socket_q+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <make_server_socket_q+0x100>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   1090 <gethostname@plt>
lea    -0x120(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x18(%rbp),%rsi
add    $0x8,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1060 <bcopy@plt>
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x124(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     1253 <make_server_socket_q+0xd3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <make_server_socket_q+0x100>
mov    -0x124(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
je     1277 <make_server_socket_q+0xf7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1280 <make_server_socket_q+0x100>
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_server_socket_q>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1232 <make_server_socket_q+0xb2>
mov    %eax,%ebp
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
lea    0x10(%rsp),%r13
mov    %r13,%rdi
mov    $0x100,%esi
call   1090 <gethostname@plt>
mov    %r13,%rdi
call   1040 <gethostbyname@plt>
mov    (%rax),%edx
mov    0x4(%rax),%edi
lea    0x8(%rsp),%rsi
call   1060 <bcopy@plt>
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    1232 <make_server_socket_q+0xb2>
mov    %ebp,%edi
mov    %r14d,%esi
call   1050 <listen@plt>
neg    %rax
sbb    %ebx,%ebx
or     %ebp,%ebx
mov    %ebx,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
define i32 @make_server_socket_q(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca [256 x i8]
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %48
16: ; preds = %2
%17 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%19 = call i32 @gethostname(i8* %18, i32 256)
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%21 = call %struct.hostent* @gethostbyname(i8* %20)
store %struct.hostent* %21, %struct.hostent** %7
%22 = load %struct.hostent*, %struct.hostent** %7
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%26 = load %struct.hostent*, %struct.hostent** %7
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @bcopy(i32 %24, i32* %25, i32 %28)
%30 = load i32, i32* %4
%31 = call i32 @htons(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %9
%36 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%37 = call i64 @bind(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %16
store i32 -1, i32* %3
br label %48
40: ; preds = %16
%41 = load i32, i32* %9
%42 = load i32, i32* %5
%43 = call i64 @listen(i32 %41, i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
store i32 -1, i32* %3
br label %48
46: ; preds = %40
%47 = load i32, i32* %9
store i32 %47, i32* %3
br label %48
48: ; preds = %46, %45, %39, %15
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
define i32 @make_server_socket_q(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [256 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %32, label %11
11: ; preds = %2
%12 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%13 = call i32 @gethostname(i8* %6, i32 256)
%14 = call %struct.hostent* @gethostbyname(i8* %6)
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @bcopy(i32 %16, i32* %17, i32 %19)
%21 = call i32 @htons(i32 %0)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %9, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %11
%29 = call i64 @listen(i32 %9, i32 %1)
%30 = icmp eq i64 %29, 0
%31 = select i1 %30, i32 %9, i32 -1
br label %32
32: ; preds = %28, %11, %2
%33 = phi i32 [ -1, %2 ], [ -1, %11 ], [ %31, %28 ]
ret i32 %33
}