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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int connect(int, struct sockaddr *, unsigned int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
char *get_conndir();
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int start_server();
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
size_t strlen(char *);
int connect_server(int sock) {
  int res;
  struct sockaddr_un addr;
  unsigned int addrlen;
  const char *conndir = get_conndir();
  if (conndir == NULL)
    return -1;
  addr.sun_family = AF_UNIX;
  sprintf(addr.sun_path, "%s/server", conndir);
  addrlen =
      (size_t)(((struct sockaddr_un *)0)->sun_path) + strlen(addr.sun_path);
  res = connect(sock, (struct sockaddr *)&addr, addrlen);
  if (res == -1) {
    res = start_server();
    if (res == -1)
      return -1;
    res = connect(sock, (struct sockaddr *)&addr, addrlen);
    if (res == -1)
      fprintf(stderr, "AVFS: could not connect to server socket (%s): %s\n",
              addr.sun_path, strerror(errno));
  }
  return res;
}


x86 assembly (clang, -O0):
<connect_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <get_conndir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119d <connect_server+0x2d>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <connect_server+0x107>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xe48(%rip),%rsi
call   1050 <sprintf@plt>
xor    %eax,%eax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <connect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1271 <connect_server+0x101>
mov    $0x0,%al
call   1090 <start_server@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1218 <connect_server+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <connect_server+0x107>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1060 <connect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    126c <connect_server+0xfc>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xda3(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1271 <connect_server+0x101>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_server>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
xor    %eax,%eax
call   1060 <get_conndir@plt>
test   %rax,%rax
je     11ef <connect_server+0x8f>
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x10(%rsp)
lea    0xe77(%rip),%rsi
mov    %rax,%rdx
call   1040 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11f4 <connect_server+0x94>
xor    %eax,%eax
call   1080 <start_server@plt>
cmp    $0xffffffff,%eax
je     11ef <connect_server+0x8f>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1050 <connect@plt>
cmp    $0xffffffff,%eax
jne    11f4 <connect_server+0x94>
mov    0x2e12(%rip),%rax
mov    (%rax),%ebx
mov    0x8(%rsp),%r14
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe28(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@.str = [10 x i8] c"%s/server\00"
@stderr = global i32 0
@.str.1 = [51 x i8] c"AVFS: could not connect to server socket (%s): %s\0A\00"
@errno = global i32 0
define i32 @connect_server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %3
%8 = call i8* (...) @get_conndir()
store i8* %8, i8** %7
%9 = load i8*, i8** %7
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %54
12: ; preds = %1
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%16 = load i8*, i8** %15
%17 = load i8*, i8** %7
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i8*, i8** null
%20 = ptrtoint i8* %19 to i64
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i64 @strlen(i8* %22)
%24 = add i64 %20, %23
%25 = trunc i64 %24 to i32
store i32 %25, i32* %6
%26 = load i32, i32* %3
%27 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%28 = load i32, i32* %6
%29 = call i32 @connect(i32 %26, %struct.sockaddr* %27, i32 %28)
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %52
32: ; preds = %12
%33 = call i32 (...) @start_server()
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %32
store i32 -1, i32* %2
br label %54
37: ; preds = %32
%38 = load i32, i32* %3
%39 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%40 = load i32, i32* %6
%41 = call i32 @connect(i32 %38, %struct.sockaddr* %39, i32 %40)
store i32 %41, i32* %4
%42 = load i32, i32* %4
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %51
44: ; preds = %37
%45 = load i32, i32* @stderr
%46 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %47, i8* %49)
br label %51
51: ; preds = %44, %37
br label %52
52: ; preds = %51, %12
%53 = load i32, i32* %4
store i32 %53, i32* %2
br label %54
54: ; preds = %52, %36, %11
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@.str = [10 x i8] c"%s/server\00"
@stderr = global i32 0
@.str.1 = [51 x i8] c"AVFS: could not connect to server socket (%s): %s\0A\00"
@errno = global i32 0
define i32 @connect_server(i32 %0) {
%2 = alloca %struct.sockaddr_un
%3 = bitcast %struct.sockaddr_un* %2 to i8*
%4 = tail call i8* (...) @get_conndir()
%5 = icmp eq i8* %4, null
br i1 %5, label %26, label %6
6: ; preds = %1
%7 = load i32, i32* @AF_UNIX
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%10 = tail call i32 @sprintf(i8* dereferenceable(1) undef, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %4)
%11 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%12 = call i32 @connect(i32 %0, %struct.sockaddr* %11, i32 undef)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %26
14: ; preds = %6
%15 = call i32 (...) @start_server()
%16 = icmp eq i32 %15, -1
br i1 %16, label %26, label %17
17: ; preds = %14
%18 = call i32 @connect(i32 %0, %struct.sockaddr* %11, i32 undef)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %9
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8* %22, i8* %24)
br label %26
26: ; preds = %6, %20, %17, %14, %1
%27 = phi i32 [ -1, %1 ], [ -1, %14 ], [ -1, %20 ], [ %18, %17 ], [ %12, %6 ]
ret i32 %27
}