#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;
};
struct protoent {
  int /*<<< orphan*/ p_proto;
};
typedef int /*<<< orphan*/ servaddr;

/* 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(struct sockaddr_in *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal(char *, int /*<<< orphan*/);
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listen_sock(void) {
  int sock_fd;
  int proto;
  struct protoent *my_proto;
  struct sockaddr_in servaddr;
  my_proto = getprotobyname("tcp");
  if ((sock_fd = socket(PF_INET, SOCK_STREAM, my_proto->p_proto)) < 0) {
    fatal("socket(): %s", errno);
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(2222);
  if (bind(sock_fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    fatal("bind(): %s", errno);
  }
  if (listen(sock_fd, 2) < 0) {
    fatal("listen(): %s", errno);
  }
  return sock_fd;
}


x86 assembly (clang, -O0):
<listen_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe71(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    11d6 <listen_sock+0x56>
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    0xe33(%rip),%rdi
call   1090 <fatal@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x8ae,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    123e <listen_sock+0xbe>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
lea    0xdd8(%rip),%rdi
call   1090 <fatal@plt>
mov    -0x4(%rbp),%edi
mov    $0x2,%esi
call   1050 <listen@plt>
cmp    $0x0,%rax
jge    126a <listen_sock+0xea>
mov    0x2d6c(%rip),%rax
mov    (%rax),%esi
lea    0xdb7(%rip),%rdi
call   1090 <fatal@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_sock>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe72(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    0x2e26(%rip),%rcx
mov    (%rcx),%edi
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11c7 <listen_sock+0x47>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
call   1090 <fatal@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1040 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x8ae,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    122f <listen_sock+0xaf>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
lea    0xde7(%rip),%rdi
call   1090 <fatal@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <listen@plt>
test   %rax,%rax
jns    1255 <listen_sock+0xd5>
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
lea    0xdcc(%rip),%rdi
call   1090 <fatal@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.protoent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"tcp\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"socket(): %s\00"
@errno = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [11 x i8] c"bind(): %s\00"
@.str.3 = [13 x i8] c"listen(): %s\00"
define i32 @listen_sock() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = tail call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = getelementptr inbounds %struct.protoent, %struct.protoent* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = tail call i32 @socket(i32 %4, i32 %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @errno
%12 = tail call i32 @fatal(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %10, %0
%14 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
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* %1, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 2222)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%23 = call i64 @bind(i32 %8, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %13
%26 = load i32, i32* @errno
%27 = call i32 @fatal(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %13
%29 = call i64 @listen(i32 %8, i32 2)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @errno
%33 = call i32 @fatal(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %31, %28
ret i32 %8
}