#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 */

/* Type definitions */
typedef int /*<<< orphan*/ u_long;
struct sockaddr_in {
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct servent {
  scalar_t__ s_port;
};
struct rpcent {
  char const *r_name;
  scalar_t__ r_aliases;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ ret;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int IPPROTO_TCP;
int O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, ...);
int getrpcbynumber_r(int /*<<< orphan*/, struct rpcent *, char *, int,
                     struct rpcent **);
int getservbyname_r(char const *, char const *, struct servent *, char *, int,
                    struct servent **);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int, int);
int svc_socket(u_long number, int type, int protocol, int reuse) {
  struct sockaddr_in addr;
  socklen_t len = sizeof(struct sockaddr_in);
  char rpcdata[1024], servdata[1024];
  struct rpcent rpcbuf, *rpcp;
  struct servent servbuf, *servp = NULL;
  int sock, ret;
  const char *proto = protocol == IPPROTO_TCP ? "tcp" : "udp";
  if ((sock = socket((AF_INET), (type), (protocol))) < 0) {
    perror(("svc_socket: socket creation problem"));
    return sock;
  }
  if (reuse) {
    ret = 1;
    ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &ret, sizeof(ret));
    if (ret < 0) {
      perror(("svc_socket: socket reuse problem"));
      return ret;
    }
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  ret = getrpcbynumber_r(number, &rpcbuf, rpcdata, sizeof rpcdata, &rpcp);
  if (ret == 0 && rpcp != NULL) {
    ret = getservbyname_r(rpcp->r_name, proto, &servbuf, servdata,
                          sizeof servdata, &servp);
    if ((ret != 0 || servp == NULL) && rpcp->r_aliases) {
      const char **a;
      for (a = (const char **)rpcp->r_aliases; *a != NULL; a++) {
        ret = getservbyname_r(*a, proto, &servbuf, servdata, sizeof servdata,
                              &servp);
        if (ret == 0 && servp != NULL)
          break;
      }
    }
  }
  if (ret == 0 && servp != NULL) {
    addr.sin_port = servp->s_port;
    if (bind(sock, (struct sockaddr *)&addr, len) < 0) {
      perror(("svc_socket: bind problem"));
      (void)close((sock));
      sock = -1;
    }
  } else {
    addr.sin_port = 0;
    if (bind(sock, (struct sockaddr *)&addr, len) < 0) {
      perror(("svc_socket: bind problem"));
      (void)close((sock));
      sock = -1;
    }
  }
  if (sock >= 0) {
    int flags;
    if ((flags = fcntl(sock, F_GETFL)) < 0) {
      perror(("svc_socket: can't get socket flags"));
      (void)close((sock));
      sock = -1;
    } else if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
      perror(("svc_socket: can't set socket flags"));
      (void)close((sock));
      sock = -1;
    }
  }
  return sock;
}


x86 assembly (clang, -O0):
<svc_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x880,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x10,-0x2c(%rbp)
movq   $0x0,-0x858(%rbp)
mov    -0x10(%rbp),%edx
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
lea    0xe38(%rip),%rax
lea    0xe2d(%rip),%rcx
cmp    %esi,%edx
cmove  %rcx,%rax
mov    %rax,-0x868(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   10b0 <socket@plt>
mov    %eax,-0x85c(%rbp)
cmp    $0x0,%eax
jge    121d <svc_socket+0x8d>
lea    0xdfe(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x85c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    155f <svc_socket+0x3cf>
cmpl   $0x0,-0x14(%rbp)
je     128d <svc_socket+0xfd>
movl   $0x1,-0x860(%rbp)
mov    -0x85c(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
lea    -0x860(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %eax,-0x860(%rbp)
cmpl   $0x0,-0x860(%rbp)
jge    1288 <svc_socket+0xf8>
lea    0xdb7(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x860(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    155f <svc_socket+0x3cf>
jmp    128d <svc_socket+0xfd>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x430(%rbp),%rdx
lea    -0x840(%rbp),%rsi
mov    $0x400,%ecx
lea    -0x848(%rbp),%r8
call   1030 <getrpcbynumber_r@plt>
mov    %eax,-0x860(%rbp)
cmpl   $0x0,-0x860(%rbp)
jne    13f0 <svc_socket+0x260>
cmpq   $0x0,-0x848(%rbp)
je     13f0 <svc_socket+0x260>
mov    -0x848(%rbp),%rax
mov    (%rax),%rdi
mov    -0x868(%rbp),%rsi
lea    -0x830(%rbp),%rcx
lea    -0x850(%rbp),%rdx
mov    $0x400,%r8d
lea    -0x858(%rbp),%r9
call   1080 <getservbyname_r@plt>
mov    %eax,-0x860(%rbp)
cmpl   $0x0,-0x860(%rbp)
jne    133e <svc_socket+0x1ae>
cmpq   $0x0,-0x858(%rbp)
jne    13eb <svc_socket+0x25b>
mov    -0x848(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     13eb <svc_socket+0x25b>
mov    -0x848(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x870(%rbp)
mov    -0x870(%rbp),%rax
cmpq   $0x0,(%rax)
je     13e6 <svc_socket+0x256>
mov    -0x870(%rbp),%rax
mov    (%rax),%rdi
mov    -0x868(%rbp),%rsi
lea    -0x830(%rbp),%rcx
lea    -0x850(%rbp),%rdx
mov    $0x400,%r8d
lea    -0x858(%rbp),%r9
call   1080 <getservbyname_r@plt>
mov    %eax,-0x860(%rbp)
cmpl   $0x0,-0x860(%rbp)
jne    13ca <svc_socket+0x23a>
cmpq   $0x0,-0x858(%rbp)
je     13ca <svc_socket+0x23a>
jmp    13e6 <svc_socket+0x256>
jmp    13cf <svc_socket+0x23f>
mov    -0x870(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x870(%rbp)
jmp    1362 <svc_socket+0x1d2>
jmp    13eb <svc_socket+0x25b>
jmp    13f0 <svc_socket+0x260>
cmpl   $0x0,-0x860(%rbp)
jne    145b <svc_socket+0x2cb>
cmpq   $0x0,-0x858(%rbp)
je     145b <svc_socket+0x2cb>
mov    -0x858(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x85c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1456 <svc_socket+0x2c6>
lea    0xc11(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x85c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x85c(%rbp)
ff ff ff
jmp    14a5 <svc_socket+0x315>
movq   $0x0,-0x28(%rbp)
mov    -0x85c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    14a0 <svc_socket+0x310>
lea    0xbc7(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x85c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x85c(%rbp)
ff ff ff
jmp    14a5 <svc_socket+0x315>
cmpl   $0x0,-0x85c(%rbp)
jl     1556 <svc_socket+0x3c6>
mov    -0x85c(%rbp),%edi
mov    0x2af9(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1050 <fcntl@plt>
mov    %eax,-0x874(%rbp)
cmp    $0x0,%eax
jge    14fd <svc_socket+0x36d>
lea    0xb88(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x85c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x85c(%rbp)
ff ff ff
jmp    1551 <svc_socket+0x3c1>
mov    -0x85c(%rbp),%edi
mov    0x2ab6(%rip),%rax
mov    (%rax),%esi
mov    -0x874(%rbp),%edx
mov    0x2abf(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1050 <fcntl@plt>
cmp    $0x0,%eax
jge    154c <svc_socket+0x3bc>
lea    0xb57(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x85c(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x85c(%rbp)
ff ff ff
jmp    1551 <svc_socket+0x3c1>
jmp    1556 <svc_socket+0x3c6>
mov    -0x85c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x880,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<svc_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x848,%rsp
mov    %ecx,%ebp
mov    %edi,%r14d
movq   $0x0,0x8(%rsp)
mov    0x2e1a(%rip),%rax
cmp    %edx,(%rax)
lea    0xe41(%rip),%rax
lea    0xe3e(%rip),%rbx
cmove  %rax,%rbx
mov    0x2e0f(%rip),%r15
mov    (%r15),%edi
call   10b0 <socket@plt>
mov    %eax,%r13d
test   %eax,%eax
js     1396 <svc_socket+0x206>
test   %ebp,%ebp
je     1221 <svc_socket+0x91>
movl   $0x1,0x4(%rsp)
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
js     13ad <svc_socket+0x21d>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x20(%rsp)
lea    0x30(%rsp),%rsi
lea    0x440(%rsp),%rdx
lea    0x10(%rsp),%r8
mov    %r14d,%edi
mov    $0x400,%ecx
call   1030 <getrpcbynumber_r@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
jne    132b <svc_socket+0x19b>
mov    0x10(%rsp),%rax
test   %rax,%rax
je     132b <svc_socket+0x19b>
mov    (%rax),%rdi
lea    0x28(%rsp),%rdx
lea    0x40(%rsp),%rcx
lea    0x8(%rsp),%r9
mov    %rbx,%rsi
mov    $0x400,%r8d
call   1080 <getservbyname_r@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rsp),%rcx
test   %eax,%eax
jne    12a8 <svc_socket+0x118>
test   %rcx,%rcx
je     12a8 <svc_socket+0x118>
xor    %eax,%eax
jmp    1318 <svc_socket+0x188>
mov    0x10(%rsp),%rdx
mov    0x8(%rdx),%rbp
test   %rbp,%rbp
je     1318 <svc_socket+0x188>
mov    0x0(%rbp),%rdi
test   %rdi,%rdi
je     1318 <svc_socket+0x188>
add    $0x8,%rbp
lea    0x28(%rsp),%r14
lea    0x40(%rsp),%r15
lea    0x8(%rsp),%r12
jmp    12ed <svc_socket+0x15d>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%rbp),%rdi
add    $0x8,%rbp
test   %rdi,%rdi
je     1318 <svc_socket+0x188>
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    $0x400,%r8d
mov    %r12,%r9
call   1080 <getservbyname_r@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rsp),%rcx
test   %eax,%eax
jne    12e0 <svc_socket+0x150>
test   %rcx,%rcx
je     12e0 <svc_socket+0x150>
jmp    12a4 <svc_socket+0x114>
test   %eax,%eax
jne    132b <svc_socket+0x19b>
test   %rcx,%rcx
je     132b <svc_socket+0x19b>
mov    (%rcx),%rax
mov    %rax,0x18(%rsp)
jmp    1334 <svc_socket+0x1a4>
movq   $0x0,0x18(%rsp)
lea    0x18(%rsp),%rsi
mov    %r13d,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     13a4 <svc_socket+0x214>
test   %r13d,%r13d
js     13d4 <svc_socket+0x244>
mov    0x2c5d(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
xor    %eax,%eax
call   1050 <fcntl@plt>
test   %eax,%eax
js     13c0 <svc_socket+0x230>
mov    0x2c4e(%rip),%rcx
mov    (%rcx),%esi
mov    0x2c5d(%rip),%rcx
or     (%rcx),%eax
mov    %r13d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fcntl@plt>
test   %eax,%eax
jns    13da <svc_socket+0x24a>
lea    0xcf5(%rip),%rdi
jmp    13c7 <svc_socket+0x237>
lea    0xc6b(%rip),%rdi
call   10a0 <perror@plt>
jmp    13da <svc_socket+0x24a>
lea    0xca2(%rip),%rdi
jmp    13c7 <svc_socket+0x237>
lea    0xc78(%rip),%rdi
call   10a0 <perror@plt>
mov    0x4(%rsp),%r13d
jmp    13da <svc_socket+0x24a>
lea    0xc9f(%rip),%rdi
call   10a0 <perror@plt>
mov    %r13d,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r13d
mov    %r13d,%eax
add    $0x848,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i64, i32 }
%struct.rpcent = type { i8*, i64 }
%struct.servent = type { i64 }
%struct.sockaddr = type { i32 }
@IPPROTO_TCP = global i32 0
@.str = [4 x i8] c"tcp\00"
@.str.1 = [4 x i8] c"udp\00"
@AF_INET = global i32 0
@.str.2 = [36 x i8] c"svc_socket: socket creation problem\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [33 x i8] c"svc_socket: socket reuse problem\00"
@.str.4 = [25 x i8] c"svc_socket: bind problem\00"
@F_GETFL = global i32 0
@.str.5 = [35 x i8] c"svc_socket: can't get socket flags\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.6 = [35 x i8] c"svc_socket: can't set socket flags\00"
define i32 @svc_socket(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca [1024 x i8]
%13 = alloca [1024 x i8]
%14 = alloca %struct.rpcent
%15 = alloca %struct.rpcent*
%16 = alloca %struct.servent
%17 = alloca %struct.servent*
%18 = alloca i32
%19 = alloca i32
%20 = alloca i8*
%21 = alloca i8**
%22 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 16, i32* %11
store %struct.servent* null, %struct.servent** %17
%23 = load i32, i32* %8
%24 = load i32, i32* @IPPROTO_TCP
%25 = icmp eq i32 %23, %24
%26 = zext i1 %25 to i64
%27 = select i1 %25, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
store i8* %27, i8** %20
%28 = load i32, i32* @AF_INET
%29 = load i32, i32* %7
%30 = load i32, i32* %8
%31 = call i32 @socket(i32 %28, i32 %29, i32 %30)
store i32 %31, i32* %18
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %4
%34 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* %18
store i32 %35, i32* %5
br label %167
36: ; preds = %4
%37 = load i32, i32* %9
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %50
39: ; preds = %36
store i32 1, i32* %19
%40 = load i32, i32* %18
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_REUSEADDR
%43 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i32* %19, i32 4)
store i32 %43, i32* %19
%44 = load i32, i32* %19
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* %19
store i32 %48, i32* %5
br label %167
49: ; preds = %39
br label %50
50: ; preds = %49, %36
%51 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 16)
%52 = load i32, i32* @AF_INET
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %52, i32* %53
%54 = load i32, i32* %6
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%56 = call i32 @getrpcbynumber_r(i32 %54, %struct.rpcent* %14, i8* %55, i32 1024, %struct.rpcent** %15)
store i32 %56, i32* %19
%57 = load i32, i32* %19
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %106
59: ; preds = %50
%60 = load %struct.rpcent*, %struct.rpcent** %15
%61 = icmp ne %struct.rpcent* %60, null
br i1 %61, label %62, label %106
62: ; preds = %59
%63 = load %struct.rpcent*, %struct.rpcent** %15
%64 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %63, i32 0, i32 0
%65 = load i8*, i8** %64
%66 = load i8*, i8** %20
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%68 = call i32 @getservbyname_r(i8* %65, i8* %66, %struct.servent* %16, i8* %67, i32 1024, %struct.servent** %17)
store i32 %68, i32* %19
%69 = load i32, i32* %19
%70 = icmp ne i32 %69, 0
br i1 %70, label %74, label %71
71: ; preds = %62
%72 = load %struct.servent*, %struct.servent** %17
%73 = icmp eq %struct.servent* %72, null
br i1 %73, label %74, label %105
74: ; preds = %71, %62
%75 = load %struct.rpcent*, %struct.rpcent** %15
%76 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %75, i32 0, i32 1
%77 = load i64, i64* %76
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %105
79: ; preds = %74
%80 = load %struct.rpcent*, %struct.rpcent** %15
%81 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %80, i32 0, i32 1
%82 = load i64, i64* %81
%83 = inttoptr i64 %82 to i8**
store i8** %83, i8*** %21
br label %84
84: ; preds = %101, %79
%85 = load i8**, i8*** %21
%86 = load i8*, i8** %85
%87 = icmp ne i8* %86, null
br i1 %87, label %88, label %104
88: ; preds = %84
%89 = load i8**, i8*** %21
%90 = load i8*, i8** %89
%91 = load i8*, i8** %20
%92 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%93 = call i32 @getservbyname_r(i8* %90, i8* %91, %struct.servent* %16, i8* %92, i32 1024, %struct.servent** %17)
store i32 %93, i32* %19
%94 = load i32, i32* %19
%95 = icmp eq i32 %94, 0
br i1 %95, label %96, label %100
96: ; preds = %88
%97 = load %struct.servent*, %struct.servent** %17
%98 = icmp ne %struct.servent* %97, null
br i1 %98, label %99, label %100
99: ; preds = %96
br label %104
100: ; preds = %96, %88
br label %101
101: ; preds = %100
%102 = load i8**, i8*** %21
%103 = getelementptr inbounds i8*, i8** %102, i32 1
store i8** %103, i8*** %21
br label %84
104: ; preds = %99, %84
br label %105
105: ; preds = %104, %74, %71
br label %106
106: ; preds = %105, %59, %50
%107 = load i32, i32* %19
%108 = icmp eq i32 %107, 0
br i1 %108, label %109, label %127
109: ; preds = %106
%110 = load %struct.servent*, %struct.servent** %17
%111 = icmp ne %struct.servent* %110, null
br i1 %111, label %112, label %127
112: ; preds = %109
%113 = load %struct.servent*, %struct.servent** %17
%114 = getelementptr inbounds %struct.servent, %struct.servent* %113, i32 0, i32 0
%115 = load i64, i64* %114
%116 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i64 %115, i64* %116
%117 = load i32, i32* %18
%118 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%119 = load i32, i32* %11
%120 = call i64 @bind(i32 %117, %struct.sockaddr* %118, i32 %119)
%121 = icmp slt i64 %120, 0
br i1 %121, label %122, label %126
122: ; preds = %112
%123 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%124 = load i32, i32* %18
%125 = call i32 @close(i32 %124)
store i32 -1, i32* %18
br label %126
126: ; preds = %122, %112
br label %139
127: ; preds = %109, %106
%128 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i64 0, i64* %128
%129 = load i32, i32* %18
%130 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%131 = load i32, i32* %11
%132 = call i64 @bind(i32 %129, %struct.sockaddr* %130, i32 %131)
%133 = icmp slt i64 %132, 0
br i1 %133, label %134, label %138
134: ; preds = %127
%135 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%136 = load i32, i32* %18
%137 = call i32 @close(i32 %136)
store i32 -1, i32* %18
br label %138
138: ; preds = %134, %127
br label %139
139: ; preds = %138, %126
%140 = load i32, i32* %18
%141 = icmp sge i32 %140, 0
br i1 %141, label %142, label %165
142: ; preds = %139
%143 = load i32, i32* %18
%144 = load i32, i32* @F_GETFL
%145 = call i32 (i32, i32, ...) @fcntl(i32 %143, i32 %144)
store i32 %145, i32* %22
%146 = icmp slt i32 %145, 0
br i1 %146, label %147, label %151
147: ; preds = %142
%148 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%149 = load i32, i32* %18
%150 = call i32 @close(i32 %149)
store i32 -1, i32* %18
br label %164
151: ; preds = %142
%152 = load i32, i32* %18
%153 = load i32, i32* @F_SETFL
%154 = load i32, i32* %22
%155 = load i32, i32* @O_NONBLOCK
%156 = or i32 %154, %155
%157 = call i32 (i32, i32, ...) @fcntl(i32 %152, i32 %153, i32 %156)
%158 = icmp slt i32 %157, 0
br i1 %158, label %159, label %163
159: ; preds = %151
%160 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%161 = load i32, i32* %18
%162 = call i32 @close(i32 %161)
store i32 -1, i32* %18
br label %163
163: ; preds = %159, %151
br label %164
164: ; preds = %163, %147
br label %165
165: ; preds = %164, %139
%166 = load i32, i32* %18
store i32 %166, i32* %5
br label %167
167: ; preds = %165, %46, %33
%168 = load i32, i32* %5
ret i32 %168
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i64, i32 }
%struct.rpcent = type { i8*, i64 }
%struct.servent = type { i64 }
%struct.sockaddr = type { i32 }
@IPPROTO_TCP = global i32 0
@.str = [4 x i8] c"tcp\00"
@.str.1 = [4 x i8] c"udp\00"
@AF_INET = global i32 0
@.str.2 = [36 x i8] c"svc_socket: socket creation problem\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [33 x i8] c"svc_socket: socket reuse problem\00"
@.str.4 = [25 x i8] c"svc_socket: bind problem\00"
@F_GETFL = global i32 0
@.str.5 = [35 x i8] c"svc_socket: can't get socket flags\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.6 = [35 x i8] c"svc_socket: can't set socket flags\00"
define i32 @svc_socket(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = alloca %struct.rpcent
%9 = alloca %struct.rpcent*
%10 = alloca %struct.servent
%11 = alloca %struct.servent*
%12 = alloca i32
%13 = bitcast %struct.sockaddr_in* %5 to i8*
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%16 = bitcast %struct.rpcent* %8 to i8*
%17 = bitcast %struct.rpcent** %9 to i8*
%18 = bitcast %struct.servent* %10 to i8*
%19 = bitcast %struct.servent** %11 to i8*
store %struct.servent* null, %struct.servent** %11
%20 = bitcast i32* %12 to i8*
%21 = load i32, i32* @IPPROTO_TCP
%22 = icmp eq i32 %21, %2
%23 = select i1 %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%24 = load i32, i32* @AF_INET
%25 = tail call i32 @socket(i32 %24, i32 %1, i32 %2)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %4
%28 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %119
29: ; preds = %4
%30 = icmp eq i32 %3, 0
br i1 %30, label %39, label %31
31: ; preds = %29
store i32 1, i32* %12
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = call i32 @setsockopt(i32 %25, i32 %32, i32 %33, i32* %12, i32 4)
store i32 %34, i32* %12
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* %12
br label %119
39: ; preds = %31, %29
%40 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 16)
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %41, i32* %42
%43 = call i32 @getrpcbynumber_r(i32 %0, %struct.rpcent* %8, i8* %14, i32 1024, %struct.rpcent** %9)
store i32 %43, i32* %12
%44 = icmp eq i32 %43, 0
%45 = load %struct.rpcent*, %struct.rpcent** %9
%46 = icmp ne %struct.rpcent* %45, null
%47 = select i1 %44, i1 %46, i1 false
br i1 %47, label %48, label %93
48: ; preds = %39
%49 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %45, i64 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 @getservbyname_r(i8* %50, i8* %23, %struct.servent* %10, i8* %15, i32 1024, %struct.servent** %11)
store i32 %51, i32* %12
%52 = icmp ne i32 %51, 0
%53 = load %struct.servent*, %struct.servent** %11
%54 = icmp eq %struct.servent* %53, null
%55 = select i1 %52, i1 true, i1 %54
br i1 %55, label %56, label %77
56: ; preds = %48
%57 = load %struct.rpcent*, %struct.rpcent** %9
%58 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %57, i64 0, i32 1
%59 = load i64, i64* %58
%60 = icmp eq i64 %59, 0
br i1 %60, label %77, label %61
61: ; preds = %56
%62 = inttoptr i64 %59 to i8**
%63 = load i8*, i8** %62
%64 = icmp eq i8* %63, null
br i1 %64, label %77, label %68
65: ; preds = %68
%66 = load i8*, i8** %76
%67 = icmp eq i8* %66, null
br i1 %67, label %77, label %68
68: ; preds = %61, %65
%69 = phi i8* [ %66, %65 ], [ %63, %61 ]
%70 = phi i8** [ %76, %65 ], [ %62, %61 ]
%71 = call i32 @getservbyname_r(i8* %69, i8* %23, %struct.servent* %10, i8* %15, i32 1024, %struct.servent** %11)
store i32 %71, i32* %12
%72 = icmp eq i32 %71, 0
%73 = load %struct.servent*, %struct.servent** %11
%74 = icmp ne %struct.servent* %73, null
%75 = select i1 %72, i1 %74, i1 false
%76 = getelementptr inbounds i8*, i8** %70, i64 1
br i1 %75, label %77, label %65
77: ; preds = %68, %65, %61, %56, %48
%78 = phi %struct.servent* [ %53, %61 ], [ %53, %56 ], [ %53, %48 ], [ %73, %65 ], [ %73, %68 ]
%79 = phi i32 [ %51, %61 ], [ %51, %56 ], [ 0, %48 ], [ 0, %68 ], [ %71, %65 ]
%80 = icmp eq i32 %79, 0
%81 = icmp ne %struct.servent* %78, null
%82 = select i1 %80, i1 %81, i1 false
br i1 %82, label %83, label %93
83: ; preds = %77
%84 = getelementptr inbounds %struct.servent, %struct.servent* %78, i64 0, i32 0
%85 = load i64, i64* %84
%86 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i64 %85, i64* %86
%87 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%88 = call i64 @bind(i32 %25, %struct.sockaddr* %87, i32 16)
%89 = icmp slt i64 %88, 0
br i1 %89, label %90, label %101
90: ; preds = %83
%91 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%92 = call i32 @close(i32 %25)
br label %119
93: ; preds = %39, %77
%94 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i64 0, i64* %94
%95 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%96 = call i64 @bind(i32 %25, %struct.sockaddr* %95, i32 16)
%97 = icmp slt i64 %96, 0
br i1 %97, label %98, label %101
98: ; preds = %93
%99 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%100 = call i32 @close(i32 %25)
br label %119
101: ; preds = %93, %83
%102 = icmp sgt i32 %25, -1
br i1 %102, label %103, label %119
103: ; preds = %101
%104 = load i32, i32* @F_GETFL
%105 = call i32 (i32, i32, ...) @fcntl(i32 %25, i32 %104)
%106 = icmp slt i32 %105, 0
br i1 %106, label %107, label %110
107: ; preds = %103
%108 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%109 = call i32 @close(i32 %25)
br label %119
110: ; preds = %103
%111 = load i32, i32* @F_SETFL
%112 = load i32, i32* @O_NONBLOCK
%113 = or i32 %112, %105
%114 = call i32 (i32, i32, ...) @fcntl(i32 %25, i32 %111, i32 %113)
%115 = icmp slt i32 %114, 0
br i1 %115, label %116, label %119
116: ; preds = %110
%117 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%118 = call i32 @close(i32 %25)
br label %119
119: ; preds = %98, %90, %101, %110, %116, %107, %36, %27
%120 = phi i32 [ %25, %27 ], [ %38, %36 ], [ -1, %101 ], [ -1, %107 ], [ -1, %116 ], [ %25, %110 ], [ -1, %90 ], [ -1, %98 ]
ret i32 %120
}