#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 */
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ LOG_EMERG;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(char const *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int bird_connect(const char *socket_path) {
  int bird_socket = -1;
  struct sockaddr_un addr;
  if (strlen(socket_path) >= sizeof addr.sun_path) {
    syslog(LOG_EMERG, "Socket path too long");
    return -1;
  }
  bird_socket = socket(AF_UNIX, SOCK_STREAM, 0);
  if (bird_socket < 0) {
    syslog(LOG_EMERG, "Socket creation error: %m");
    return -1;
  }
  memset(&addr, 0, sizeof addr);
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, socket_path);
  if (connect(bird_socket, (struct sockaddr *)&addr, sizeof addr) == -1) {
    syslog(LOG_EMERG, "BIRD connection to %s failed: %m", socket_path);
    close(bird_socket);
    return -1;
  }
  return bird_socket;
}


x86 assembly (clang, -O0):
<bird_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cltq
cmp    $0x4,%rax
jb     11bb <bird_connect+0x4b>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127b <bird_connect+0x10b>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1204 <bird_connect+0x94>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127b <bird_connect+0x10b>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1275 <bird_connect+0x105>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1070 <syslog@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127b <bird_connect+0x10b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bird_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    $0x4,%eax
jb     1199 <bird_connect+0x29>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
jmp    1224 <bird_connect+0xb4>
mov    0x2e48(%rip),%r15
mov    (%r15),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1214 <bird_connect+0xa4>
mov    %eax,%ebp
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1230 <bird_connect+0xc0>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <syslog@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    122b <bird_connect+0xbb>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
xor    %eax,%eax
call   1070 <syslog@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@LOG_EMERG = global i32 0
@.str = [21 x i8] c"Socket path too long\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [26 x i8] c"Socket creation error: %m\00"
@.str.2 = [33 x i8] c"BIRD connection to %s failed: %m\00"
define i32 @bird_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
store i8* %0, i8** %3
store i32 -1, i32* %4
%6 = load i8*, i8** %3
%7 = call i32 @strlen(i8* %6)
%8 = sext i32 %7 to i64
%9 = icmp uge i64 %8, 4
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @LOG_EMERG
%12 = call i32 (i32, i8*, ...) @syslog(i32 %11, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %42
13: ; preds = %1
%14 = load i32, i32* @AF_UNIX
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @LOG_EMERG
%21 = call i32 (i32, i8*, ...) @syslog(i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %42
22: ; preds = %13
%23 = call i32 @memset(%struct.sockaddr_un* %5, i32 0, i32 8)
%24 = load i32, i32* @AF_UNIX
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i8*, i8** %3
%29 = call i32 @strcpy(i32 %27, i8* %28)
%30 = load i32, i32* %4
%31 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%32 = call i32 @connect(i32 %30, %struct.sockaddr* %31, i32 8)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %22
%35 = load i32, i32* @LOG_EMERG
%36 = load i8*, i8** %3
%37 = call i32 (i32, i8*, ...) @syslog(i32 %35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %2
br label %42
40: ; preds = %22
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %34, %19, %10
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@LOG_EMERG = global i32 0
@.str = [21 x i8] c"Socket path too long\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [26 x i8] c"Socket creation error: %m\00"
@.str.2 = [33 x i8] c"BIRD connection to %s failed: %m\00"
define i32 @bird_connect(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp ugt i32 %4, 3
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @LOG_EMERG
%8 = tail call i32 (i32, i8*, ...) @syslog(i32 %7, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %31
9: ; preds = %1
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i32, i32* @LOG_EMERG
%16 = tail call i32 (i32, i8*, ...) @syslog(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %31
17: ; preds = %9
%18 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @strcpy(i32 %22, i8* %0)
%24 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%25 = call i32 @connect(i32 %12, %struct.sockaddr* %24, i32 8)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %17
%28 = load i32, i32* @LOG_EMERG
%29 = call i32 (i32, i8*, ...) @syslog(i32 %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%30 = call i32 @close(i32 %12)
br label %31
31: ; preds = %17, %27, %14, %6
%32 = phi i32 [ -1, %6 ], [ -1, %14 ], [ -1, %27 ], [ %12, %17 ]
ret i32 %32
}