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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(short const);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int begin_listen(const short port) {
  int sockfd;
  struct sockaddr_in seraddr;
  bzero(&seraddr, sizeof(seraddr));
  if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
    perror("socket");
    return -1;
  }
  seraddr.sin_family = AF_INET;
  seraddr.sin_port = htons(port);
  seraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr)) == -1) {
    perror("bind");
    return -1;
  }
  if (listen(sockfd, 10) == -1) {
    perror("listen");
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<begin_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %di,%ax
mov    %ax,-0x6(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11d1 <begin_listen+0x61>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <begin_listen+0xf0>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movswl -0x6(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    122c <begin_listen+0xbc>
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <begin_listen+0xf0>
mov    -0xc(%rbp),%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
jne    125a <begin_listen+0xea>
lea    0xdc3(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1260 <begin_listen+0xf0>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<begin_listen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    %rsp,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e5a(%rip),%r14
mov    (%r14),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1203 <begin_listen+0x93>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x8(%rsp)
movswl %bp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
je     120c <begin_listen+0x9c>
mov    %ebx,%edi
mov    $0xa,%esi
call   1050 <listen@plt>
cmp    $0xffffffff,%eax
je     1215 <begin_listen+0xa5>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
jmp    121c <begin_listen+0xac>
lea    0xdf4(%rip),%rdi
jmp    121c <begin_listen+0xac>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11f8 <begin_listen+0x88>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
define i32 @begin_listen(i16 signext %0) {
%2 = alloca i32
%3 = alloca i16
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
store i16 %0, i16* %3
%6 = call i32 @bzero(%struct.sockaddr_in* %5, i32 12)
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = load i32, i32* @IPPROTO_TCP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %38
14: ; preds = %1
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i16, i16* %3
%18 = call i32 @htons(i16 signext %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %4
%25 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%26 = call i32 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %14
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %38
30: ; preds = %14
%31 = load i32, i32* %4
%32 = call i32 @listen(i32 %31, i32 10)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %38
36: ; preds = %30
%37 = load i32, i32* %4
store i32 %37, i32* %2
br label %38
38: ; preds = %36, %34, %28, %12
%39 = load i32, i32* %2
ret i32 %39
}

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