#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 {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ in_addr_t;
typedef int /*<<< orphan*/ i;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_SCTP;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_SEQPACKET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int xyz_sock_listen(char *addr, int port, int type) {
  struct sockaddr_in sin;
  int sockfd;
  struct hostent *ht;
  memset(&sin, 0, sizeof(sin));
  if (addr != NULL) {
    memset(&sin, 0, sizeof(sin));
    ht = gethostbyname(addr);
    if (ht == NULL) {
      perror("gethostbyname() error");
      return -1;
    }
    sin.sin_addr.s_addr = *((in_addr_t *)(ht->h_addr));
  } else {
    sin.sin_addr.s_addr = INADDR_ANY;
  }
  sin.sin_port = htons(port);
  sin.sin_family = AF_INET;
  switch (type) {
  case 1:
    sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    break;
  case 2:
    sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
    break;
  case 3:
    sockfd = socket(PF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
    break;
  default:
    return -1;
  }
  if (sockfd == -1) {
    perror("socket() error");
    return -1;
  }
  int i = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
  if (bind(sockfd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    perror("bind() error");
    return -1;
  }
  if (listen(sockfd, 128) == -1) {
    perror("listen() error");
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<xyz_sock_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
je     11fe <xyz_sock_listen+0x7e>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ed <xyz_sock_listen+0x6d>
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138e <xyz_sock_listen+0x20e>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    120a <xyz_sock_listen+0x8a>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
sub    $0x1,%eax
je     1257 <xyz_sock_listen+0xd7>
jmp    1235 <xyz_sock_listen+0xb5>
mov    -0x40(%rbp),%eax
sub    $0x2,%eax
je     127f <xyz_sock_listen+0xff>
jmp    1246 <xyz_sock_listen+0xc6>
mov    -0x40(%rbp),%eax
sub    $0x3,%eax
je     12a7 <xyz_sock_listen+0x127>
jmp    12cf <xyz_sock_listen+0x14f>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
jmp    12db <xyz_sock_listen+0x15b>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
mov    0x2d39(%rip),%rax
mov    (%rax),%esi
mov    0x2cf0(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
jmp    12db <xyz_sock_listen+0x15b>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
mov    0x2ce8(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
jmp    12db <xyz_sock_listen+0x15b>
movl   $0xffffffff,-0x4(%rbp)
jmp    138e <xyz_sock_listen+0x20e>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12fd <xyz_sock_listen+0x17d>
lea    0xd2a(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138e <xyz_sock_listen+0x20e>
movl   $0x1,-0x3c(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
mov    0x2c89(%rip),%rax
mov    (%rax),%edx
lea    -0x3c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
jne    135a <xyz_sock_listen+0x1da>
lea    0xcdc(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138e <xyz_sock_listen+0x20e>
mov    -0x2c(%rbp),%edi
mov    $0x80,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
jne    1388 <xyz_sock_listen+0x208>
lea    0xcbb(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138e <xyz_sock_listen+0x20e>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xyz_sock_listen>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
test   %rbp,%rbp
je     11c9 <xyz_sock_listen+0x49>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     128f <xyz_sock_listen+0x10f>
mov    (%rax),%rax
jmp    11d0 <xyz_sock_listen+0x50>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
add    $0xffffffff,%ebx
mov    $0xffffffff,%ebp
cmp    $0x2,%ebx
ja     1284 <xyz_sock_listen+0x104>
movslq %ebx,%rax
lea    0x2bbf(%rip),%rcx
mov    (%rcx,%rax,8),%rcx
lea    0x2bcc(%rip),%rdx
mov    (%rdx,%rax,8),%rax
mov    0x2da9(%rip),%rdx
mov    (%rdx),%edi
mov    (%rcx),%esi
mov    (%rax),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     12a2 <xyz_sock_listen+0x122>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0xffffffff,%eax
je     12b0 <xyz_sock_listen+0x130>
mov    %ebx,%edi
mov    $0x80,%esi
call   1070 <listen@plt>
cmp    $0xffffffff,%eax
je     12b9 <xyz_sock_listen+0x139>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd6a(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1284 <xyz_sock_listen+0x104>
lea    0xd6d(%rip),%rdi
call   1090 <perror@plt>
jmp    1284 <xyz_sock_listen+0x104>
lea    0xd6e(%rip),%rdi
jmp    12c0 <xyz_sock_listen+0x140>
lea    0xd72(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1282 <xyz_sock_listen+0x102>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"gethostbyname() error\00"
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOCK_SEQPACKET = global i32 0
@IPPROTO_SCTP = global i32 0
@.str.1 = [15 x i8] c"socket() error\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [13 x i8] c"bind() error\00"
@.str.3 = [15 x i8] c"listen() error\00"
define i32 @xyz_sock_listen(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca %struct.hostent*
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%12 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %31
15: ; preds = %3
%16 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%17 = load i8*, i8** %5
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %10
%19 = load %struct.hostent*, %struct.hostent** %10
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %82
23: ; preds = %15
%24 = load %struct.hostent*, %struct.hostent** %10
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = inttoptr i64 %26 to i32*
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
br label %35
31: ; preds = %3
%32 = load i32, i32* @INADDR_ANY
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store i32 %32, i32* %34
br label %35
35: ; preds = %31, %23
%36 = load i32, i32* %6
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %37, i32* %38
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* %7
switch i32 %41, label %57 [
i32 1, label %42
i32 2, label %47
i32 3, label %52
]
42: ; preds = %35
%43 = load i32, i32* @PF_INET
%44 = load i32, i32* @SOCK_STREAM
%45 = load i32, i32* @IPPROTO_TCP
%46 = call i32 @socket(i32 %43, i32 %44, i32 %45)
store i32 %46, i32* %9
br label %58
47: ; preds = %35
%48 = load i32, i32* @PF_INET
%49 = load i32, i32* @SOCK_DGRAM
%50 = load i32, i32* @IPPROTO_UDP
%51 = call i32 @socket(i32 %48, i32 %49, i32 %50)
store i32 %51, i32* %9
br label %58
52: ; preds = %35
%53 = load i32, i32* @PF_INET
%54 = load i32, i32* @SOCK_SEQPACKET
%55 = load i32, i32* @IPPROTO_SCTP
%56 = call i32 @socket(i32 %53, i32 %54, i32 %55)
store i32 %56, i32* %9
br label %58
57: ; preds = %35
store i32 -1, i32* %4
br label %82
58: ; preds = %52, %47, %42
%59 = load i32, i32* %9
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %82
63: ; preds = %58
store i32 1, i32* %11
%64 = load i32, i32* %9
%65 = load i32, i32* @SOL_SOCKET
%66 = load i32, i32* @SO_REUSEADDR
%67 = call i32 @setsockopt(i32 %64, i32 %65, i32 %66, i32* %11, i32 4)
%68 = load i32, i32* %9
%69 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%70 = call i32 @bind(i32 %68, %struct.sockaddr* %69, i32 12)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %74
72: ; preds = %63
%73 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %82
74: ; preds = %63
%75 = load i32, i32* %9
%76 = call i32 @listen(i32 %75, i32 128)
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %82
80: ; preds = %74
%81 = load i32, i32* %9
store i32 %81, i32* %4
br label %82
82: ; preds = %80, %78, %72, %61, %57, %21
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"gethostbyname() error\00"
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@SOCK_SEQPACKET = global i32 0
@IPPROTO_SCTP = global i32 0
@.str.1 = [15 x i8] c"socket() error\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [13 x i8] c"bind() error\00"
@.str.3 = [15 x i8] c"listen() error\00"
@switch.table.xyz_sock_listen = [3 x i32*] [i32* @SOCK_STREAM, i32* @SOCK_DGRAM, i32* @SOCK_SEQPACKET]
@switch.table.xyz_sock_listen.4 = [3 x i32*] [i32* @IPPROTO_TCP, i32* @IPPROTO_UDP, i32* @IPPROTO_SCTP]
define i32 @xyz_sock_listen(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%8 = icmp eq i8* %0, null
br i1 %8, label %19, label %9
9: ; preds = %3
%10 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%11 = call %struct.hostent* @gethostbyname(i8* %0)
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %59
15: ; preds = %9
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%17 = load i64, i64* %16
%18 = inttoptr i64 %17 to i32*
br label %19
19: ; preds = %3, %15
%20 = phi i32* [ %18, %15 ], [ @INADDR_ANY, %3 ]
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2, i32 0
store i32 %21, i32* %22
%23 = call i32 @htons(i32 %1)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %25, i32* %26
%27 = add i32 %2, -1
%28 = icmp ult i32 %27, 3
br i1 %28, label %29, label %59
29: ; preds = %19
%30 = sext i32 %27 to i64
%31 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.xyz_sock_listen, i64 0, i64 %30
%32 = load i32*, i32** %31
%33 = sext i32 %27 to i64
%34 = getelementptr inbounds [3 x i32*], [3 x i32*]* @switch.table.xyz_sock_listen.4, i64 0, i64 %33
%35 = load i32*, i32** %34
%36 = load i32, i32* @PF_INET
%37 = load i32, i32* %32
%38 = load i32, i32* %35
%39 = call i32 @socket(i32 %36, i32 %37, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %29
%42 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %59
43: ; preds = %29
%44 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%45 = load i32, i32* @SOL_SOCKET
%46 = load i32, i32* @SO_REUSEADDR
%47 = call i32 @setsockopt(i32 %39, i32 %45, i32 %46, i32* %5, i32 4)
%48 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%49 = call i32 @bind(i32 %39, %struct.sockaddr* %48, i32 12)
%50 = icmp eq i32 %49, -1
br i1 %50, label %54, label %51
51: ; preds = %43
%52 = call i32 @listen(i32 %39, i32 128)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %51, %43
%55 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), %43 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), %51 ]
%56 = call i32 @perror(i8* %55)
br label %57
57: ; preds = %54, %51
%58 = phi i32 [ %39, %51 ], [ -1, %54 ]
br label %59
59: ; preds = %19, %57, %41, %13
%60 = phi i32 [ -1, %13 ], [ -1, %41 ], [ %58, %57 ], [ -1, %19 ]
ret i32 %60
}