#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 {
  char *h_name;
  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 /*<<< orphan*/ printf(char *, char *);
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);
  printf("ip: %s\n", hp->h_name);
  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    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    11d9 <make_server_socket_q+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <make_server_socket_q+0x114>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
lea    -0x120(%rbp),%rdi
mov    $0x100,%esi
call   10a0 <gethostname@plt>
lea    -0x120(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    0xdea(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%edi
lea    -0x18(%rbp),%rsi
add    $0x8,%rsi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <bcopy@plt>
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x124(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
je     1277 <make_server_socket_q+0xe7>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <make_server_socket_q+0x114>
mov    -0x124(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
je     129b <make_server_socket_q+0x10b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a4 <make_server_socket_q+0x114>
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    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    $0xffffffff,%r13d
cmp    $0xffffffff,%eax
je     125c <make_server_socket_q+0xcc>
mov    %eax,%ebp
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   10a0 <gethostname@plt>
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
mov    %rax,%rbx
mov    (%rax),%rsi
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%edi
lea    0x8(%rsp),%rsi
call   1070 <bcopy@plt>
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jne    125c <make_server_socket_q+0xcc>
mov    %ebp,%edi
mov    %r14d,%esi
call   1060 <listen@plt>
neg    %rax
sbb    %r13d,%r13d
or     %ebp,%r13d
mov    %r13d,%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 { i8*, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"ip: %s\0A\00"
@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 %52
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 0
%24 = load i8*, i8** %23
%25 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = load %struct.hostent*, %struct.hostent** %7
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 2
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%30 = load %struct.hostent*, %struct.hostent** %7
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @bcopy(i32 %28, i32* %29, i32 %32)
%34 = load i32, i32* %4
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* %9
%40 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%41 = call i64 @bind(i32 %39, %struct.sockaddr* %40, i32 12)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %16
store i32 -1, i32* %3
br label %52
44: ; preds = %16
%45 = load i32, i32* %9
%46 = load i32, i32* %5
%47 = call i64 @listen(i32 %45, i32 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %44
store i32 -1, i32* %3
br label %52
50: ; preds = %44
%51 = load i32, i32* %9
store i32 %51, i32* %3
br label %52
52: ; preds = %50, %49, %43, %15
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i8*, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [8 x i8] c"ip: %s\0A\00"
@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 %35, 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 0
%16 = load i8*, i8** %15
%17 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 2
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%22 = load i32, i32* %21
%23 = call i32 @bcopy(i32 %19, i32* %20, i32 %22)
%24 = call i32 @htons(i32 %0)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i64 @bind(i32 %9, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %11
%32 = call i64 @listen(i32 %9, i32 %1)
%33 = icmp eq i64 %32, 0
%34 = select i1 %33, i32 %9, i32 -1
br label %35
35: ; preds = %31, %11, %2
%36 = phi i32 [ -1, %2 ], [ -1, %11 ], [ %34, %31 ]
ret i32 %36
}