#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 {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
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 /*<<< orphan*/ getservport(int /*<<< orphan*/, char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
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);
  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;
  addr.sin_port = htons(getservport(number, proto));
  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    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x8,-0x24(%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,-0x38(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   10b0 <socket@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1206 <svc_socket+0x76>
lea    0xe12(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1366 <svc_socket+0x1d6>
cmpl   $0x0,-0x14(%rbp)
je     1264 <svc_socket+0xd4>
movl   $0x1,-0x2c(%rbp)
mov    -0x28(%rbp),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
lea    -0x2c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    125f <svc_socket+0xcf>
lea    0xddd(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1366 <svc_socket+0x1d6>
jmp    1264 <svc_socket+0xd4>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1080 <getservport@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12ca <svc_socket+0x13a>
lea    0xd97(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jl     1360 <svc_socket+0x1d0>
mov    -0x28(%rbp),%edi
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    1313 <svc_socket+0x183>
lea    0xd6c(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    135b <svc_socket+0x1cb>
mov    -0x28(%rbp),%edi
mov    0x2ca3(%rip),%rax
mov    (%rax),%esi
mov    -0x3c(%rbp),%edx
mov    0x2caf(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
cmp    $0x0,%eax
jge    1356 <svc_socket+0x1c6>
lea    0xd47(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x28(%rbp)
jmp    135b <svc_socket+0x1cb>
jmp    1360 <svc_socket+0x1d0>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<svc_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebx
mov    %edi,%r14d
mov    0x2e28(%rip),%rax
cmp    %edx,(%rax)
lea    0xe4f(%rip),%rax
lea    0xe4c(%rip),%rbp
cmove  %rax,%rbp
mov    0x2e1d(%rip),%r12
mov    (%r12),%edi
call   10b0 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
js     12a4 <svc_socket+0x114>
test   %ebx,%ebx
je     1214 <svc_socket+0x84>
movl   $0x1,0x4(%rsp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %r15d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
js     12e0 <svc_socket+0x150>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
call   1080 <getservport@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     12b2 <svc_socket+0x122>
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
js     12bb <svc_socket+0x12b>
mov    0x2d47(%rip),%rcx
mov    (%rcx),%esi
mov    0x2d56(%rip),%rcx
or     (%rcx),%eax
mov    %r15d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
js     12c4 <svc_socket+0x134>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5d(%rip),%rdi
call   10a0 <perror@plt>
jmp    1294 <svc_socket+0x104>
lea    0xd94(%rip),%rdi
jmp    12cb <svc_socket+0x13b>
lea    0xda4(%rip),%rdi
jmp    12cb <svc_socket+0x13b>
lea    0xdbe(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    $0xffffffff,%r15d
jmp    1294 <svc_socket+0x104>
lea    0xd45(%rip),%rdi
call   10a0 <perror@plt>
mov    0x4(%rsp),%r15d
jmp    1294 <svc_socket+0x104>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%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 %56
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 %56
27: ; preds = %19, %17
%28 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 8)
%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 i32 @getservport(i32 %0, i8* %11)
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%35 = call i64 @bind(i32 %13, %struct.sockaddr* %34, i32 8)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %27
%38 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
%39 = call i32 @close(i32 %13)
br label %56
40: ; preds = %27
%41 = load i32, i32* @F_GETFL
%42 = call i32 (i32, i32, ...) @fcntl(i32 %13, i32 %41)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0))
%46 = call i32 @close(i32 %13)
br label %56
47: ; preds = %40
%48 = load i32, i32* @F_SETFL
%49 = load i32, i32* @O_NONBLOCK
%50 = or i32 %49, %42
%51 = call i32 (i32, i32, ...) @fcntl(i32 %13, i32 %48, i32 %50)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %47
%54 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 @close(i32 %13)
br label %56
56: ; preds = %37, %47, %53, %44, %24, %15
%57 = phi i32 [ %13, %15 ], [ %26, %24 ], [ -1, %37 ], [ -1, %44 ], [ -1, %53 ], [ %13, %47 ]
ret i32 %57
}