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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int setup_tcp_server_socket(char *, char *, int, int);
int setup_unix_server_socket(char const *, int, int);
char *strdup(char const *);
scalar_t__ strncmp(char const *, char *, int);
char *strrchr(char *, char);
int setup_server_socket(const char *address, const char *default_port,
                        int backlog, int nonblock) {
  char *addr, *port;
  int fd;
  if (strncmp(address, "unix://", 7) == 0) {
    return setup_unix_server_socket(address + 7, backlog, nonblock);
  } else {
    addr = strdup(address);
    if (!addr) {
      return -1;
    }
    port = strrchr(addr, ':');
    if (!port) {
      if (!default_port) {
        free(addr);
        return -1;
      }
      port = (char *)default_port;
    } else {
      *port++ = '\0';
    }
    fd = setup_tcp_server_socket(addr, port, backlog, nonblock);
    free(addr);
    return fd;
  }
}


x86 assembly (clang, -O0):
<setup_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe7f(%rip),%rsi
mov    $0x7,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    11b0 <setup_server_socket+0x50>
mov    -0x10(%rbp),%rdi
add    $0x7,%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1040 <setup_unix_server_socket@plt>
mov    %eax,-0x4(%rbp)
jmp    1255 <setup_server_socket+0xf5>
mov    -0x10(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d4 <setup_server_socket+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <setup_server_socket+0xf5>
mov    -0x28(%rbp),%rdi
mov    $0x3a,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    121e <setup_server_socket+0xbe>
cmpq   $0x0,-0x18(%rbp)
jne    1211 <setup_server_socket+0xb1>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1255 <setup_server_socket+0xf5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    1230 <setup_server_socket+0xd0>
mov    -0x30(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x30(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1070 <setup_tcp_server_socket@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_server_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe86(%rip),%rsi
mov    $0x7,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
je     11b7 <setup_server_socket+0x57>
mov    %rbp,%rdi
call   1080 <strdup@plt>
test   %rax,%rax
je     11ff <setup_server_socket+0x9f>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x3a,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11d3 <setup_server_socket+0x73>
mov    %rax,%rbx
add    $0x1,%rbx
movb   $0x0,(%rax)
jmp    11d8 <setup_server_socket+0x78>
add    $0x7,%rbp
mov    %rbp,%rdi
mov    %r15d,%esi
mov    %r14d,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <setup_unix_server_socket@plt>
test   %rbx,%rbx
je     11f7 <setup_server_socket+0x97>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
mov    %r14d,%ecx
call   1070 <setup_tcp_server_socket@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
jmp    1204 <setup_server_socket+0xa4>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"unix://\00"
define i32 @setup_server_socket(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %6
%14 = call i64 @strncmp(i8* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = getelementptr inbounds i8, i8* %17, i64 7
%19 = load i32, i32* %8
%20 = load i32, i32* %9
%21 = call i32 @setup_unix_server_socket(i8* %18, i32 %19, i32 %20)
store i32 %21, i32* %5
br label %53
22: ; preds = %4
%23 = load i8*, i8** %6
%24 = call i8* @strdup(i8* %23)
store i8* %24, i8** %10
%25 = load i8*, i8** %10
%26 = icmp ne i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %22
store i32 -1, i32* %5
br label %53
28: ; preds = %22
%29 = load i8*, i8** %10
%30 = call i8* @strrchr(i8* %29, i8 signext 58)
store i8* %30, i8** %11
%31 = load i8*, i8** %11
%32 = icmp ne i8* %31, null
br i1 %32, label %41, label %33
33: ; preds = %28
%34 = load i8*, i8** %7
%35 = icmp ne i8* %34, null
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = load i8*, i8** %10
%38 = call i32 @free(i8* %37)
store i32 -1, i32* %5
br label %53
39: ; preds = %33
%40 = load i8*, i8** %7
store i8* %40, i8** %11
br label %44
41: ; preds = %28
%42 = load i8*, i8** %11
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %11
store i8 0, i8* %42
br label %44
44: ; preds = %41, %39
%45 = load i8*, i8** %10
%46 = load i8*, i8** %11
%47 = load i32, i32* %8
%48 = load i32, i32* %9
%49 = call i32 @setup_tcp_server_socket(i8* %45, i8* %46, i32 %47, i32 %48)
store i32 %49, i32* %12
%50 = load i8*, i8** %10
%51 = call i32 @free(i8* %50)
%52 = load i32, i32* %12
store i32 %52, i32* %5
br label %53
53: ; preds = %44, %36, %27, %16
%54 = load i32, i32* %5
ret i32 %54
}

LLVM IR (-O2):
@.str = [8 x i8] c"unix://\00"
define i32 @setup_server_socket(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 7)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %0, i64 7
%9 = tail call i32 @setup_unix_server_socket(i8* %8, i32 %2, i32 %3)
br label %26
10: ; preds = %4
%11 = tail call i8* @strdup(i8* %0)
%12 = icmp eq i8* %11, null
br i1 %12, label %26, label %13
13: ; preds = %10
%14 = tail call i8* @strrchr(i8* %11, i8 signext 58)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = icmp eq i8* %1, null
br i1 %17, label %18, label %22
18: ; preds = %16
%19 = tail call i32 @free(i8* %11)
br label %26
20: ; preds = %13
%21 = getelementptr inbounds i8, i8* %14, i64 1
store i8 0, i8* %14
br label %22
22: ; preds = %16, %20
%23 = phi i8* [ %21, %20 ], [ %1, %16 ]
%24 = tail call i32 @setup_tcp_server_socket(i8* %11, i8* %23, i32 %2, i32 %3)
%25 = tail call i32 @free(i8* %11)
br label %26
26: ; preds = %10, %22, %18, %7
%27 = phi i32 [ %9, %7 ], [ %24, %22 ], [ -1, %18 ], [ -1, %10 ]
ret i32 %27
}