#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*/, ...);
struct rpcent *getrpcbynumber(int /*<<< orphan*/);
struct servent *getservbyname(char const *, char const *);
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);
  struct rpcent *rpcp;
  struct servent *servp;
  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 = ((rpcp = getrpcbynumber(number)) == NULL);
  if (ret == 0 && rpcp != NULL) {
    ret = ((servp = getservbyname(rpcp->r_name, proto)) == NULL);
    if ((ret != 0 || servp == NULL) && rpcp->r_aliases) {
      const char **a;
      for (a = (const char **)rpcp->r_aliases; *a != NULL; a++) {
        ret = ((servp = getservbyname(*a, proto)) == NULL);
        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    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x10,-0x2c(%rbp)
mov    -0x10(%rbp),%edx
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
lea    0xe46(%rip),%rax
lea    0xe3b(%rip),%rcx
cmp    %esi,%edx
cmove  %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   10b0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jge    1206 <svc_socket+0x76>
lea    0xe12(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    14a4 <svc_socket+0x314>
cmpl   $0x0,-0x14(%rbp)
je     1264 <svc_socket+0xd4>
movl   $0x1,-0x48(%rbp)
mov    -0x44(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jge    125f <svc_socket+0xcf>
lea    0xddd(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    14a4 <svc_socket+0x314>
jmp    1264 <svc_socket+0xd4>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <getrpcbynumber@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jne    136e <svc_socket+0x1de>
cmpq   $0x0,-0x38(%rbp)
je     136e <svc_socket+0x1de>
mov    -0x38(%rbp),%rax
mov    (%rax),%rdi
mov    -0x50(%rbp),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jne    12e8 <svc_socket+0x158>
cmpq   $0x0,-0x40(%rbp)
jne    1369 <svc_socket+0x1d9>
mov    -0x38(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1369 <svc_socket+0x1d9>
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmpq   $0x0,(%rax)
je     1364 <svc_socket+0x1d4>
mov    -0x58(%rbp),%rax
mov    (%rax),%rdi
mov    -0x50(%rbp),%rsi
call   1050 <getservbyname@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
jne    134e <svc_socket+0x1be>
cmpq   $0x0,-0x40(%rbp)
je     134e <svc_socket+0x1be>
jmp    1364 <svc_socket+0x1d4>
jmp    1353 <svc_socket+0x1c3>
mov    -0x58(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x58(%rbp)
jmp    1303 <svc_socket+0x173>
jmp    1369 <svc_socket+0x1d9>
jmp    136e <svc_socket+0x1de>
cmpl   $0x0,-0x48(%rbp)
jne    13c7 <svc_socket+0x237>
cmpq   $0x0,-0x40(%rbp)
je     13c7 <svc_socket+0x237>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    13c2 <svc_socket+0x232>
lea    0xc9f(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    1408 <svc_socket+0x278>
movq   $0x0,-0x28(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1403 <svc_socket+0x273>
lea    0xc5e(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    1408 <svc_socket+0x278>
cmpl   $0x0,-0x44(%rbp)
jl     149e <svc_socket+0x30e>
mov    -0x44(%rbp),%edi
mov    0x2b9c(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x5c(%rbp)
cmp    $0x0,%eax
jge    1451 <svc_socket+0x2c1>
lea    0xc2e(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    1499 <svc_socket+0x309>
mov    -0x44(%rbp),%edi
mov    0x2b65(%rip),%rax
mov    (%rax),%esi
mov    -0x5c(%rbp),%edx
mov    0x2b71(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    1494 <svc_socket+0x304>
lea    0xc09(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x44(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x44(%rbp)
jmp    1499 <svc_socket+0x309>
jmp    149e <svc_socket+0x30e>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<svc_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %ecx,%ebp
mov    %edi,%r14d
mov    0x2e28(%rip),%rax
cmp    %edx,(%rax)
lea    0xe4f(%rip),%rax
lea    0xe4c(%rip),%rbx
cmove  %rax,%rbx
mov    0x2e1d(%rip),%r12
mov    (%r12),%edi
call   10b0 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
js     130e <svc_socket+0x17e>
test   %ebp,%ebp
je     1214 <svc_socket+0x84>
movl   $0x1,0xc(%rsp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r15d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     1325 <svc_socket+0x195>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1080 <getrpcbynumber@plt>
mov    %rax,%rbp
xor    %eax,%eax
test   %rbp,%rbp
sete   %al
mov    %eax,0xc(%rsp)
je     12a3 <svc_socket+0x113>
mov    0x0(%rbp),%rdi
mov    %rbx,%rsi
call   1050 <getservbyname@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
mov    %ecx,0xc(%rsp)
je     126a <svc_socket+0xda>
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
jmp    12ac <svc_socket+0x11c>
mov    0x8(%rbp),%rbp
test   %rbp,%rbp
je     12a3 <svc_socket+0x113>
mov    0x0(%rbp),%rdi
test   %rdi,%rdi
je     12a3 <svc_socket+0x113>
add    $0x8,%rbp
mov    %rbx,%rsi
call   1050 <getservbyname@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
mov    %ecx,0xc(%rsp)
jne    1260 <svc_socket+0xd0>
mov    0x0(%rbp),%rdi
add    $0x8,%rbp
test   %rdi,%rdi
jne    1280 <svc_socket+0xf0>
movq   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     131c <svc_socket+0x18c>
test   %r15d,%r15d
js     134c <svc_socket+0x1bc>
mov    0x2ce5(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
js     1338 <svc_socket+0x1a8>
mov    0x2cd6(%rip),%rcx
mov    (%rcx),%esi
mov    0x2ce5(%rip),%rcx
or     (%rcx),%eax
mov    %r15d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
jns    1352 <svc_socket+0x1c2>
lea    0xd7d(%rip),%rdi
jmp    133f <svc_socket+0x1af>
lea    0xcf3(%rip),%rdi
call   10a0 <perror@plt>
jmp    1352 <svc_socket+0x1c2>
lea    0xd2a(%rip),%rdi
jmp    133f <svc_socket+0x1af>
lea    0xd00(%rip),%rdi
call   10a0 <perror@plt>
mov    0xc(%rsp),%r15d
jmp    1352 <svc_socket+0x1c2>
lea    0xd27(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
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 %struct.rpcent*
%13 = alloca %struct.servent*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i8**
%18 = 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
%19 = load i32, i32* %8
%20 = load i32, i32* @IPPROTO_TCP
%21 = icmp eq i32 %19, %20
%22 = zext i1 %21 to i64
%23 = select i1 %21, 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* %23, i8** %16
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* %7
%26 = load i32, i32* %8
%27 = call i32 @socket(i32 %24, i32 %25, i32 %26)
store i32 %27, i32* %14
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %4
%30 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %14
store i32 %31, i32* %5
br label %166
32: ; preds = %4
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %46
35: ; preds = %32
store i32 1, i32* %15
%36 = load i32, i32* %14
%37 = load i32, i32* @SOL_SOCKET
%38 = load i32, i32* @SO_REUSEADDR
%39 = call i32 @setsockopt(i32 %36, i32 %37, i32 %38, i32* %15, i32 4)
store i32 %39, i32* %15
%40 = load i32, i32* %15
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* %15
store i32 %44, i32* %5
br label %166
45: ; preds = %35
br label %46
46: ; preds = %45, %32
%47 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 16)
%48 = load i32, i32* @AF_INET
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %48, i32* %49
%50 = load i32, i32* %6
%51 = call %struct.rpcent* @getrpcbynumber(i32 %50)
store %struct.rpcent* %51, %struct.rpcent** %12
%52 = icmp eq %struct.rpcent* %51, null
%53 = zext i1 %52 to i32
store i32 %53, i32* %15
%54 = load i32, i32* %15
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %105
56: ; preds = %46
%57 = load %struct.rpcent*, %struct.rpcent** %12
%58 = icmp ne %struct.rpcent* %57, null
br i1 %58, label %59, label %105
59: ; preds = %56
%60 = load %struct.rpcent*, %struct.rpcent** %12
%61 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %60, i32 0, i32 0
%62 = load i8*, i8** %61
%63 = load i8*, i8** %16
%64 = call %struct.servent* @getservbyname(i8* %62, i8* %63)
store %struct.servent* %64, %struct.servent** %13
%65 = icmp eq %struct.servent* %64, null
%66 = zext i1 %65 to i32
store i32 %66, i32* %15
%67 = load i32, i32* %15
%68 = icmp ne i32 %67, 0
br i1 %68, label %72, label %69
69: ; preds = %59
%70 = load %struct.servent*, %struct.servent** %13
%71 = icmp eq %struct.servent* %70, null
br i1 %71, label %72, label %104
72: ; preds = %69, %59
%73 = load %struct.rpcent*, %struct.rpcent** %12
%74 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %73, i32 0, i32 1
%75 = load i64, i64* %74
%76 = icmp ne i64 %75, 0
br i1 %76, label %77, label %104
77: ; preds = %72
%78 = load %struct.rpcent*, %struct.rpcent** %12
%79 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %78, i32 0, i32 1
%80 = load i64, i64* %79
%81 = inttoptr i64 %80 to i8**
store i8** %81, i8*** %17
br label %82
82: ; preds = %100, %77
%83 = load i8**, i8*** %17
%84 = load i8*, i8** %83
%85 = icmp ne i8* %84, null
br i1 %85, label %86, label %103
86: ; preds = %82
%87 = load i8**, i8*** %17
%88 = load i8*, i8** %87
%89 = load i8*, i8** %16
%90 = call %struct.servent* @getservbyname(i8* %88, i8* %89)
store %struct.servent* %90, %struct.servent** %13
%91 = icmp eq %struct.servent* %90, null
%92 = zext i1 %91 to i32
store i32 %92, i32* %15
%93 = load i32, i32* %15
%94 = icmp eq i32 %93, 0
br i1 %94, label %95, label %99
95: ; preds = %86
%96 = load %struct.servent*, %struct.servent** %13
%97 = icmp ne %struct.servent* %96, null
br i1 %97, label %98, label %99
98: ; preds = %95
br label %103
99: ; preds = %95, %86
br label %100
100: ; preds = %99
%101 = load i8**, i8*** %17
%102 = getelementptr inbounds i8*, i8** %101, i32 1
store i8** %102, i8*** %17
br label %82
103: ; preds = %98, %82
br label %104
104: ; preds = %103, %72, %69
br label %105
105: ; preds = %104, %56, %46
%106 = load i32, i32* %15
%107 = icmp eq i32 %106, 0
br i1 %107, label %108, label %126
108: ; preds = %105
%109 = load %struct.servent*, %struct.servent** %13
%110 = icmp ne %struct.servent* %109, null
br i1 %110, label %111, label %126
111: ; preds = %108
%112 = load %struct.servent*, %struct.servent** %13
%113 = getelementptr inbounds %struct.servent, %struct.servent* %112, i32 0, i32 0
%114 = load i64, i64* %113
%115 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i64 %114, i64* %115
%116 = load i32, i32* %14
%117 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%118 = load i32, i32* %11
%119 = call i64 @bind(i32 %116, %struct.sockaddr* %117, i32 %118)
%120 = icmp slt i64 %119, 0
br i1 %120, label %121, label %125
121: ; preds = %111
%122 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%123 = load i32, i32* %14
%124 = call i32 @close(i32 %123)
store i32 -1, i32* %14
br label %125
125: ; preds = %121, %111
br label %138
126: ; preds = %108, %105
%127 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i64 0, i64* %127
%128 = load i32, i32* %14
%129 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%130 = load i32, i32* %11
%131 = call i64 @bind(i32 %128, %struct.sockaddr* %129, i32 %130)
%132 = icmp slt i64 %131, 0
br i1 %132, label %133, label %137
133: ; preds = %126
%134 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%135 = load i32, i32* %14
%136 = call i32 @close(i32 %135)
store i32 -1, i32* %14
br label %137
137: ; preds = %133, %126
br label %138
138: ; preds = %137, %125
%139 = load i32, i32* %14
%140 = icmp sge i32 %139, 0
br i1 %140, label %141, label %164
141: ; preds = %138
%142 = load i32, i32* %14
%143 = load i32, i32* @F_GETFL
%144 = call i32 (i32, i32, ...) @fcntl(i32 %142, i32 %143)
store i32 %144, i32* %18
%145 = icmp slt i32 %144, 0
br i1 %145, label %146, label %150
146: ; preds = %141
%147 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%148 = load i32, i32* %14
%149 = call i32 @close(i32 %148)
store i32 -1, i32* %14
br label %163
150: ; preds = %141
%151 = load i32, i32* %14
%152 = load i32, i32* @F_SETFL
%153 = load i32, i32* %18
%154 = load i32, i32* @O_NONBLOCK
%155 = or i32 %153, %154
%156 = call i32 (i32, i32, ...) @fcntl(i32 %151, i32 %152, i32 %155)
%157 = icmp slt i32 %156, 0
br i1 %157, label %158, label %162
158: ; preds = %150
%159 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%160 = load i32, i32* %14
%161 = call i32 @close(i32 %160)
store i32 -1, i32* %14
br label %162
162: ; preds = %158, %150
br label %163
163: ; preds = %162, %146
br label %164
164: ; preds = %163, %138
%165 = load i32, i32* %14
store i32 %165, i32* %5
br label %166
166: ; preds = %164, %42, %29
%167 = load i32, i32* %5
ret i32 %167
}

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 i32
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = load i32, i32* @IPPROTO_TCP
%10 = icmp eq i32 %9, %2
%11 = select i1 %10, 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)
%12 = load i32, i32* @AF_INET
%13 = tail call i32 @socket(i32 %12, i32 %1, i32 %2)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
br label %95
17: ; preds = %4
%18 = icmp eq i32 %3, 0
br i1 %18, label %27, label %19
19: ; preds = %17
store i32 1, i32* %6
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = call i32 @setsockopt(i32 %13, i32 %20, i32 %21, i32* %6, i32 4)
store i32 %22, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* %6
br label %95
27: ; preds = %19, %17
%28 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 16)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %29, i32* %30
%31 = call %struct.rpcent* @getrpcbynumber(i32 %0)
%32 = icmp eq %struct.rpcent* %31, null
%33 = zext i1 %32 to i32
store i32 %33, i32* %6
br i1 %32, label %69, label %34
34: ; preds = %27
%35 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %31, i64 0, i32 0
%36 = load i8*, i8** %35
%37 = call %struct.servent* @getservbyname(i8* %36, i8* %11)
%38 = icmp eq %struct.servent* %37, null
%39 = zext i1 %38 to i32
store i32 %39, i32* %6
br i1 %38, label %40, label %58
40: ; preds = %34
%41 = getelementptr inbounds %struct.rpcent, %struct.rpcent* %31, i64 0, i32 1
%42 = load i64, i64* %41
%43 = icmp eq i64 %42, 0
br i1 %43, label %69, label %44
44: ; preds = %40
%45 = inttoptr i64 %42 to i8**
%46 = load i8*, i8** %45
%47 = icmp eq i8* %46, null
br i1 %47, label %69, label %51
48: ; preds = %51
%49 = load i8*, i8** %57
%50 = icmp eq i8* %49, null
br i1 %50, label %69, label %51
51: ; preds = %44, %48
%52 = phi i8* [ %49, %48 ], [ %46, %44 ]
%53 = phi i8** [ %57, %48 ], [ %45, %44 ]
%54 = call %struct.servent* @getservbyname(i8* %52, i8* %11)
%55 = icmp eq %struct.servent* %54, null
%56 = zext i1 %55 to i32
store i32 %56, i32* %6
%57 = getelementptr inbounds i8*, i8** %53, i64 1
br i1 %55, label %48, label %58
58: ; preds = %51, %34
%59 = phi %struct.servent* [ %37, %34 ], [ %54, %51 ]
%60 = getelementptr inbounds %struct.servent, %struct.servent* %59, i64 0, i32 0
%61 = load i64, i64* %60
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i64 %61, i64* %62
%63 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%64 = call i64 @bind(i32 %13, %struct.sockaddr* %63, i32 16)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %77
66: ; preds = %58
%67 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%68 = call i32 @close(i32 %13)
br label %95
69: ; preds = %48, %44, %27, %40
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i64 0, i64* %70
%71 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%72 = call i64 @bind(i32 %13, %struct.sockaddr* %71, i32 16)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %69
%75 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%76 = call i32 @close(i32 %13)
br label %95
77: ; preds = %69, %58
%78 = icmp sgt i32 %13, -1
br i1 %78, label %79, label %95
79: ; preds = %77
%80 = load i32, i32* @F_GETFL
%81 = call i32 (i32, i32, ...) @fcntl(i32 %13, i32 %80)
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %79
%84 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%85 = call i32 @close(i32 %13)
br label %95
86: ; preds = %79
%87 = load i32, i32* @F_SETFL
%88 = load i32, i32* @O_NONBLOCK
%89 = or i32 %88, %81
%90 = call i32 (i32, i32, ...) @fcntl(i32 %13, i32 %87, i32 %89)
%91 = icmp slt i32 %90, 0
br i1 %91, label %92, label %95
92: ; preds = %86
%93 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%94 = call i32 @close(i32 %13)
br label %95
95: ; preds = %74, %66, %77, %86, %92, %83, %24, %15
%96 = phi i32 [ %13, %15 ], [ %26, %24 ], [ -1, %77 ], [ -1, %83 ], [ -1, %92 ], [ %13, %86 ], [ -1, %66 ], [ -1, %74 ]
ret i32 %96
}