#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 */
typedef scalar_t__ u_short;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  void *sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct servent {
  scalar_t__ s_port;
};
typedef int /*<<< orphan*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ atoi(char const *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
struct servent *getservbyname(char const *, char const *);
void *htons(scalar_t__);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ ntohs(scalar_t__);
int /*<<< orphan*/ printf(char *, char const *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int passive_socket(const char *service, const char *transport, int qlen) {
  struct servent *pse;
  struct sockaddr_in sin;
  int s;
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  if ((pse = getservbyname(service, transport)))
    sin.sin_port = htons(ntohs((u_short)pse->s_port) + 1000);
  else if ((sin.sin_port = htons((u_short)atoi(service))) == 0)
    printf("can't get \"%s\" service entry\n", service);
  s = socket(PF_INET, SOCK_STREAM, 0);
  if (s < 0)
    printf("can't create socket: %s\n", strerror(errno));
  if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
    printf("can't bind to %s port: %s\n", service, strerror(errno));
  if (listen(s, qlen) < 0)
    printf("can't listen on %s port: %s\n", service, strerror(errno));
  return s;
}


x86 assembly (clang, -O0):
<passive_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     121a <passive_socket+0x7a>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1080 <ntohs@plt>
mov    %rax,%rdi
add    $0x3e8,%rdi
call   1030 <htons@plt>
mov    %rax,-0x30(%rbp)
jmp    1250 <passive_socket+0xb0>
mov    -0x8(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %rax,%rdi
call   1030 <htons@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    124b <passive_socket+0xab>
mov    -0x8(%rbp),%rsi
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1250 <passive_socket+0xb0>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1295 <passive_socket+0xf5>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12db <passive_socket+0x13b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    131b <passive_socket+0x17b>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %rax,%rdx
lea    0xd3e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<passive_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <getservbyname@plt>
test   %rax,%rax
je     1204 <passive_socket+0x64>
mov    (%rax),%rdi
call   1080 <ntohs@plt>
lea    0x3e8(%rax),%rdi
call   1030 <htons@plt>
mov    %rax,(%rsp)
jmp    122e <passive_socket+0x8e>
mov    %rbx,%rdi
call   10a0 <atoi@plt>
mov    %rax,%rdi
call   1030 <htons@plt>
mov    %rax,(%rsp)
test   %rax,%rax
jne    122e <passive_socket+0x8e>
lea    0xddc(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    126c <passive_socket+0xcc>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdbc(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    12a2 <passive_socket+0x102>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xda2(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %r14d,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    12d3 <passive_socket+0x133>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd8c(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.servent = type { i64 }
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [30 x i8] c"can't get \22%s\22 service entry\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [25 x i8] c"can't create socket: %s\0A\00"
@errno = global i32 0
@.str.2 = [27 x i8] c"can't bind to %s port: %s\0A\00"
@.str.3 = [29 x i8] c"can't listen on %s port: %s\0A\00"
define i32 @passive_socket(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.servent*
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 16)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i8*, i8** %4
%17 = load i8*, i8** %5
%18 = call %struct.servent* @getservbyname(i8* %16, i8* %17)
store %struct.servent* %18, %struct.servent** %7
%19 = icmp ne %struct.servent* %18, null
br i1 %19, label %20, label %28
20: ; preds = %3
%21 = load %struct.servent*, %struct.servent** %7
%22 = getelementptr inbounds %struct.servent, %struct.servent* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = call i64 @ntohs(i64 %23)
%25 = add nsw i64 %24, 1000
%26 = call i8* @htons(i64 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i8* %26, i8** %27
br label %38
28: ; preds = %3
%29 = load i8*, i8** %4
%30 = call i64 @atoi(i8* %29)
%31 = call i8* @htons(i64 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i8* %31, i8** %32
%33 = icmp eq i8* %31, null
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = load i8*, i8** %4
%36 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %34, %28
br label %38
38: ; preds = %37, %20
%39 = load i32, i32* @PF_INET
%40 = load i32, i32* @SOCK_STREAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %44, %38
%49 = load i32, i32* %9
%50 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%51 = call i64 @bind(i32 %49, %struct.sockaddr* %50, i32 16)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %48
%54 = load i8*, i8** %4
%55 = load i32, i32* @errno
%56 = call i8* @strerror(i32 %55)
%57 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %54, i8* %56)
br label %58
58: ; preds = %53, %48
%59 = load i32, i32* %9
%60 = load i32, i32* %6
%61 = call i64 @listen(i32 %59, i32 %60)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %58
%64 = load i8*, i8** %4
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %64, i8* %66)
br label %68
68: ; preds = %63, %58
%69 = load i32, i32* %9
ret i32 %69
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.servent = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [30 x i8] c"can't get \22%s\22 service entry\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [25 x i8] c"can't create socket: %s\0A\00"
@errno = global i32 0
@.str.2 = [27 x i8] c"can't bind to %s port: %s\0A\00"
@.str.3 = [29 x i8] c"can't listen on %s port: %s\0A\00"
define i32 @passive_socket(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 16)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = call %struct.servent* @getservbyname(i8* %0, i8* %1)
%12 = icmp eq %struct.servent* %11, null
br i1 %12, label %20, label %13
13: ; preds = %3
%14 = getelementptr inbounds %struct.servent, %struct.servent* %11, i64 0, i32 0
%15 = load i64, i64* %14
%16 = call i64 @ntohs(i64 %15)
%17 = add nsw i64 %16, 1000
%18 = call i8* @htons(i64 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i8* %18, i8** %19
br label %27
20: ; preds = %3
%21 = call i64 @atoi(i8* %0)
%22 = call i8* @htons(i64 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i8* %22, i8** %23
%24 = icmp eq i8* %22, null
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %27
27: ; preds = %20, %25, %13
%28 = load i32, i32* @PF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %32, %27
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = call i64 @bind(i32 %30, %struct.sockaddr* %37, i32 16)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %42)
br label %44
44: ; preds = %40, %36
%45 = call i64 @listen(i32 %30, i32 %2)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %49)
br label %51
51: ; preds = %47, %44
ret i32 %30
}