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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(void *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int server_socket(int portnum, int backlog) {
  struct sockaddr_in saddr;
  struct hostent *hp;
  char hostname[256];
  int sock_id;
  printf("portnum:[%d]\n", portnum);
  sock_id = socket(PF_INET, SOCK_STREAM, 0);
  if (sock_id == -1)
    return -1;
  bzero((void *)&saddr, sizeof(saddr));
  saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  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):
<server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe75(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    11c8 <server_socket+0x58>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <server_socket+0xe5>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2def(%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     1228 <server_socket+0xb8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <server_socket+0xe5>
mov    -0x124(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
je     124c <server_socket+0xdc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <server_socket+0xe5>
mov    -0x124(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
lea    0xe78(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     1215 <server_socket+0xa5>
mov    %eax,%ebp
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2dff(%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    1215 <server_socket+0xa5>
mov    %ebp,%edi
mov    %r14d,%esi
call   1060 <listen@plt>
neg    %rax
sbb    %r15d,%r15d
or     %ebp,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"portnum:[%d]\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
define i32 @server_socket(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* %4
%11 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 -1, i32* %3
br label %43
18: ; preds = %2
%19 = bitcast %struct.sockaddr_in* %6 to i8*
%20 = call i32 @bzero(i8* %19, i32 12)
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* %4
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %9
%31 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%32 = call i64 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %18
store i32 -1, i32* %3
br label %43
35: ; preds = %18
%36 = load i32, i32* %9
%37 = load i32, i32* %5
%38 = call i64 @listen(i32 %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 -1, i32* %3
br label %43
41: ; preds = %35
%42 = load i32, i32* %9
store i32 %42, i32* %3
br label %43
43: ; preds = %41, %40, %34, %17
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"portnum:[%d]\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
define i32 @server_socket(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %26, label %10
10: ; preds = %2
%11 = call i32 @bzero(i8* %4, i32 12)
%12 = load i32, i32* @INADDR_ANY
%13 = tail call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %0)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%20 = call i64 @bind(i32 %8, %struct.sockaddr* %19, i32 12)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %10
%23 = call i64 @listen(i32 %8, i32 %1)
%24 = icmp eq i64 %23, 0
%25 = select i1 %24, i32 %8, i32 -1
br label %26
26: ; preds = %22, %10, %2
%27 = phi i32 [ -1, %2 ], [ -1, %10 ], [ %25, %22 ]
ret i32 %27
}