#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_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(long);
int /*<<< orphan*/ htons(short);
int listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int server_socket(long addr, short port) {
  int s;
  struct sockaddr_in sa;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    return -1;
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = htonl(addr);
  sa.sin_port = htons(port);
  if (bind(s, (struct sockaddr *)&sa, sizeof(sa)) == -1)
    return -1;
  if (listen(s, 100) == -1)
    return -1;
  return s;
}


x86 assembly (clang, -O0):
<server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1194 <server_socket+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <server_socket+0xb6>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <htonl@plt>
mov    %eax,-0x24(%rbp)
movswl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    11de <server_socket+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <server_socket+0xb6>
mov    -0x18(%rbp),%edi
mov    $0x64,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    1200 <server_socket+0xb0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1206 <server_socket+0xb6>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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    %rdi,%r15
mov    0x2e7f(%rip),%r12
mov    (%r12),%edi
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11d1 <server_socket+0x81>
mov    %eax,%ebx
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r15,%rdi
call   1040 <htonl@plt>
mov    %eax,0x4(%rsp)
movswl %r14w,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     11d1 <server_socket+0x81>
mov    %ebx,%edi
mov    $0x64,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%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
define i32 @server_socket(i64 %0, i16 signext %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i16
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i64 %0, i64* %4
store i16 %1, i16* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %35
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i64, i64* %4
%17 = call i32 @htonl(i64 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i16, i16* %5
%21 = call i32 @htons(i16 signext %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %6
%24 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%25 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %28
27: ; preds = %13
store i32 -1, i32* %3
br label %35
28: ; preds = %13
%29 = load i32, i32* %6
%30 = call i32 @listen(i32 %29, i32 100)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 -1, i32* %3
br label %35
33: ; preds = %28
%34 = load i32, i32* %6
store i32 %34, i32* %3
br label %35
35: ; preds = %33, %32, %27, %12
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%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
define i32 @server_socket(i64 %0, i16 signext %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%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 %23, label %9
9: ; preds = %2
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = tail call i32 @htonl(i64 %0)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %12, i32* %13
%14 = tail call i32 @htons(i16 signext %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%17 = call i32 @bind(i32 %7, %struct.sockaddr* %16, i32 12)
%18 = icmp eq i32 %17, -1
br i1 %18, label %23, label %19
19: ; preds = %9
%20 = call i32 @listen(i32 %7, i32 100)
%21 = icmp eq i32 %20, -1
%22 = select i1 %21, i32 -1, i32 %7
br label %23
23: ; preds = %19, %9, %2
%24 = phi i32 [ -1, %2 ], [ -1, %9 ], [ %22, %19 ]
ret i32 %24
}