#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 PF_INET;
int PF_LOCAL;
int connect_local_socket(char const *);
int connect_tcp_socket(char const *);
int /*<<< orphan*/ log_err(char *, char const *);
char *strchr(char const *, char);
scalar_t__ strncmp(char *, char const *, size_t);
int connect_socket(const char *socket_spec) {
  int fd;
  const char *s;
  size_t prefix_size;
  int socket_type;
  s = strchr(socket_spec, ':');
  if (!s) {
    socket_type = PF_LOCAL;
    s = socket_spec;
  } else {
    ++s;
    prefix_size = s - socket_spec;
    if (strncmp("local:", socket_spec, prefix_size) == 0 ||
        strncmp("unix:", socket_spec, prefix_size) == 0) {
      socket_type = PF_LOCAL;
    } else if (strncmp("tcp:", socket_spec, prefix_size) == 0) {
      socket_type = PF_INET;
    } else {
      log_err("Unknown socket protocol: %s\n", socket_spec);
      goto failed;
    }
  }
  if (socket_type == PF_LOCAL) {
    fd = connect_local_socket(s);
  } else {
    fd = connect_tcp_socket(s);
  }
  if (fd < 0)
    goto failed;
  return fd;
failed:
  return -1;
}


x86 assembly (clang, -O0):
<connect_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1192 <connect_socket+0x42>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1248 <connect_socket+0xf8>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe44(%rip),%rdi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     11e9 <connect_socket+0x99>
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe2d(%rip),%rdi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    11fa <connect_socket+0xaa>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1243 <connect_socket+0xf3>
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe04(%rip),%rdi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1229 <connect_socket+0xd9>
mov    0x2dc1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    123e <connect_socket+0xee>
mov    -0x10(%rbp),%rsi
lea    0xdde(%rip),%rdi
call   1070 <log_err@plt>
jmp    1291 <connect_socket+0x141>
jmp    1243 <connect_socket+0xf3>
jmp    1248 <connect_socket+0xf8>
mov    -0x2c(%rbp),%eax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%eax
jne    126b <connect_socket+0x11b>
mov    -0x20(%rbp),%rdi
call   1060 <connect_local_socket@plt>
mov    %eax,-0x14(%rbp)
jmp    1277 <connect_socket+0x127>
mov    -0x20(%rbp),%rdi
call   1030 <connect_tcp_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1286 <connect_socket+0x136>
jmp    1291 <connect_socket+0x141>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1298 <connect_socket+0x148>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_socket>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    0x2e6c(%rip),%r12
test   %rax,%rax
je     11d4 <connect_socket+0x84>
mov    %rax,%r14
add    $0x1,%r14
mov    %r14,%rbx
sub    %r15,%rbx
lea    0xe7b(%rip),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11d7 <connect_socket+0x87>
lea    0xe6b(%rip),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <strncmp@plt>
test   %rax,%rax
je     11d7 <connect_socket+0x87>
lea    0xe5a(%rip),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <strncmp@plt>
test   %rax,%rax
je     120d <connect_socket+0xbd>
lea    0xe48(%rip),%rdi
mov    %r15,%rsi
call   1070 <log_err@plt>
jmp    11fc <connect_socket+0xac>
mov    %r15,%r14
mov    %r12,%rax
mov    (%rax),%eax
cmp    (%r12),%eax
jne    11f0 <connect_socket+0xa0>
mov    %r14,%rdi
call   1060 <connect_local_socket@plt>
test   %eax,%eax
jns    1201 <connect_socket+0xb1>
jmp    11fc <connect_socket+0xac>
mov    %r14,%rdi
call   1030 <connect_tcp_socket@plt>
test   %eax,%eax
jns    1201 <connect_socket+0xb1>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2dcc(%rip),%rax
jmp    11da <connect_socket+0x8a>

LLVM IR (-O0):
@PF_LOCAL = global i32 0
@.str = [7 x i8] c"local:\00"
@.str.1 = [6 x i8] c"unix:\00"
@.str.2 = [5 x i8] c"tcp:\00"
@PF_INET = global i32 0
@.str.3 = [29 x i8] c"Unknown socket protocol: %s\0A\00"
define i32 @connect_socket(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @strchr(i8* %8, i8 signext 58)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i32, i32* @PF_LOCAL
store i32 %13, i32* %7
%14 = load i8*, i8** %3
store i8* %14, i8** %5
br label %46
15: ; preds = %1
%16 = load i8*, i8** %5
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = load i8*, i8** %3
%20 = ptrtoint i8* %18 to i64
%21 = ptrtoint i8* %19 to i64
%22 = sub i64 %20, %21
store i64 %22, i64* %6
%23 = load i8*, i8** %3
%24 = load i64, i64* %6
%25 = call i64 @strncmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %23, i64 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %15
%28 = load i8*, i8** %3
%29 = load i64, i64* %6
%30 = call i64 @strncmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %28, i64 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27, %15
%33 = load i32, i32* @PF_LOCAL
store i32 %33, i32* %7
br label %45
34: ; preds = %27
%35 = load i8*, i8** %3
%36 = load i64, i64* %6
%37 = call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %35, i64 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = load i32, i32* @PF_INET
store i32 %40, i32* %7
br label %44
41: ; preds = %34
%42 = load i8*, i8** %3
%43 = call i32 @log_err(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %42)
br label %62
44: ; preds = %39
br label %45
45: ; preds = %44, %32
br label %46
46: ; preds = %45, %12
%47 = load i32, i32* %7
%48 = load i32, i32* @PF_LOCAL
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = load i8*, i8** %5
%52 = call i32 @connect_local_socket(i8* %51)
store i32 %52, i32* %4
br label %56
53: ; preds = %46
%54 = load i8*, i8** %5
%55 = call i32 @connect_tcp_socket(i8* %54)
store i32 %55, i32* %4
br label %56
56: ; preds = %53, %50
%57 = load i32, i32* %4
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %56
br label %62
60: ; preds = %56
%61 = load i32, i32* %4
store i32 %61, i32* %2
br label %63
62: ; preds = %59, %41
store i32 -1, i32* %2
br label %63
63: ; preds = %62, %60
%64 = load i32, i32* %2
ret i32 %64
}

LLVM IR (-O2):
@PF_LOCAL = global i32 0
@.str = [7 x i8] c"local:\00"
@.str.1 = [6 x i8] c"unix:\00"
@.str.2 = [5 x i8] c"tcp:\00"
@PF_INET = global i32 0
@.str.3 = [29 x i8] c"Unknown socket protocol: %s\0A\00"
define i32 @connect_socket(i8* %0) {
%2 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 58)
%3 = icmp eq i8* %2, null
br i1 %3, label %19, label %4
4: ; preds = %1
%5 = getelementptr inbounds i8, i8* %2, i64 1
%6 = ptrtoint i8* %5 to i64
%7 = ptrtoint i8* %0 to i64
%8 = sub i64 %6, %7
%9 = tail call i64 @strncmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %4
%12 = tail call i64 @strncmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i64 %8)
%13 = icmp eq i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = tail call i64 @strncmp(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %8)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 @log_err(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %32
19: ; preds = %14, %4, %11, %1
%20 = phi i8* [ %0, %1 ], [ %5, %11 ], [ %5, %4 ], [ %5, %14 ]
%21 = phi i32* [ @PF_LOCAL, %1 ], [ @PF_LOCAL, %11 ], [ @PF_LOCAL, %4 ], [ @PF_INET, %14 ]
%22 = load i32, i32* %21
%23 = load i32, i32* @PF_LOCAL
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = tail call i32 @connect_local_socket(i8* %20)
br label %29
27: ; preds = %19
%28 = tail call i32 @connect_tcp_socket(i8* %20)
br label %29
29: ; preds = %27, %25
%30 = phi i32 [ %26, %25 ], [ %28, %27 ]
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29, %17
br label %33
33: ; preds = %29, %32
%34 = phi i32 [ -1, %32 ], [ %30, %29 ]
ret i32 %34
}