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

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ ECONNREFUSED;
scalar_t__ ENOENT;
int /*<<< orphan*/ SOCK_STREAM;
long atol(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char const *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
long get_time_from_socket(const char *socket_name) {
  int fd;
  int len;
  struct sockaddr_un addr;
  char buf[32];
  long rem = -1;
  static int first_time = 0;
  if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, socket_name);
  len = strlen(addr.sun_path) + 1 + sizeof(addr.sun_family);
again:
  if (connect(fd, (struct sockaddr *)&addr, len) < 0) {
    if (errno == ECONNREFUSED) {
    } else if (errno == ENOENT) {
      if (!first_time) {
        struct stat statbuf;
        int i;
        first_time = 1;
        for (i = 0; i < 100; i++) {
          if (stat(socket_name, &statbuf) == 0)
            goto again;
          usleep(10000);
        }
      }
    }
    close(fd);
    return -1;
  }
  len = read(fd, buf, 31);
  close(fd);
  if (len > 0) {
    buf[len] = '\0';
    rem = atol(buf);
  }
  return rem;
}


x86 assembly (clang, -O0):
<get_time_from_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0xffffffffffffffff,-0x48(%rbp)
ff
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11e6 <get_time_from_socket+0x46>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1338 <get_time_from_socket+0x198>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%edi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
add    $0x4,%rax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    12f4 <get_time_from_socket+0x154>
mov    0x2d86(%rip),%rax
mov    (%rax),%rax
mov    0x2d94(%rip),%rcx
cmp    (%rcx),%rax
jne    125a <get_time_from_socket+0xba>
jmp    12df <get_time_from_socket+0x13f>
mov    0x2d67(%rip),%rax
mov    (%rax),%rax
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%rax
jne    12da <get_time_from_socket+0x13a>
cmpl   $0x0,0x2dfd(%rip)
jne    12d5 <get_time_from_socket+0x135>
movl   $0x1,0x2ded(%rip)
movl   $0x0,-0x54(%rbp)
cmpl   $0x64,-0x54(%rbp)
jge    12d0 <get_time_from_socket+0x130>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    12b8 <get_time_from_socket+0x118>
jmp    1222 <get_time_from_socket+0x82>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1292 <get_time_from_socket+0xf2>
jmp    12d5 <get_time_from_socket+0x135>
jmp    12da <get_time_from_socket+0x13a>
jmp    12df <get_time_from_socket+0x13f>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    1338 <get_time_from_socket+0x198>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1f,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x18(%rbp)
jle    1330 <get_time_from_socket+0x190>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rdi
call   1090 <atol@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_time_from_socket>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
mov    0x2e30(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    $0xffffffffffffffff,%r14
test   %eax,%eax
js     131b <get_time_from_socket+0x17b>
mov    %eax,%r15d
lea    0x8(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%edi
call   1040 <strlen@plt>
mov    %eax,%r12d
add    $0x5,%r12d
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r12d,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     1264 <get_time_from_socket+0xc4>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1f,%edx
call   1070 <read@plt>
mov    %eax,%ebx
mov    %r15d,%edi
call   1060 <close@plt>
test   %ebx,%ebx
jle    131b <get_time_from_socket+0x17b>
mov    %ebx,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
call   1090 <atol@plt>
mov    %rax,%r14
jmp    131b <get_time_from_socket+0x17b>
mov    0x2d5d(%rip),%rax
mov    0x2d6e(%rip),%rcx
mov    0x2d3f(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    (%rax),%rax
cmp    (%rcx),%rax
je     1313 <get_time_from_socket+0x173>
cmp    (%rdx),%rax
jne    1313 <get_time_from_socket+0x173>
testb  $0x1,0x2dde(%rip)
jne    1313 <get_time_from_socket+0x173>
movb   $0x1,0x2dd5(%rip)
mov    $0x64,%r13d
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     12e0 <get_time_from_socket+0x140>
mov    $0x2710,%edi
call   10b0 <usleep@plt>
add    $0xffffffff,%r13d
jne    12b0 <get_time_from_socket+0x110>
jmp    1313 <get_time_from_socket+0x173>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r15d,%edi
lea    0x8(%rsp),%rsi
mov    %r12d,%edx
call   10a0 <connect@plt>
test   %rax,%rax
mov    0x2cce(%rip),%rax
mov    0x2cdf(%rip),%rcx
mov    0x2cb0(%rip),%rdx
js     127e <get_time_from_socket+0xde>
jmp    1227 <get_time_from_socket+0x87>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14,%rax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.stat = type { i32 }
%struct.sockaddr = type { i32 }
@get_time_from_socket.first_time = internal global i32 0
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@ECONNREFUSED = global i64 0
@ENOENT = global i64 0
define i64 @get_time_from_socket(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_un
%7 = alloca [32 x i8]
%8 = alloca i64
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %3
store i64 -1, i64* %8
%11 = load i32, i32* @AF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i64 -1, i64* %2
br label %85
16: ; preds = %1
%17 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 8)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%21 = load i32, i32* %20
%22 = load i8*, i8** %3
%23 = call i32 @strcpy(i32 %21, i8* %22)
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @strlen(i32 %25)
%27 = add nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = add i64 %28, 4
%30 = trunc i64 %29 to i32
store i32 %30, i32* %5
br label %31
31: ; preds = %57, %16
%32 = load i32, i32* %4
%33 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%34 = load i32, i32* %5
%35 = call i64 @connect(i32 %32, %struct.sockaddr* %33, i32 %34)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %69
37: ; preds = %31
%38 = load i64, i64* @errno
%39 = load i64, i64* @ECONNREFUSED
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %37
br label %66
42: ; preds = %37
%43 = load i64, i64* @errno
%44 = load i64, i64* @ENOENT
%45 = icmp eq i64 %43, %44
br i1 %45, label %46, label %65
46: ; preds = %42
%47 = load i32, i32* @get_time_from_socket.first_time
%48 = icmp ne i32 %47, 0
br i1 %48, label %64, label %49
49: ; preds = %46
store i32 1, i32* @get_time_from_socket.first_time
store i32 0, i32* %10
br label %50
50: ; preds = %60, %49
%51 = load i32, i32* %10
%52 = icmp slt i32 %51, 100
br i1 %52, label %53, label %63
53: ; preds = %50
%54 = load i8*, i8** %3
%55 = call i64 @stat(i8* %54, %struct.stat* %9)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %53
br label %31
58: ; preds = %53
%59 = call i32 @usleep(i32 10000)
br label %60
60: ; preds = %58
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
store i32 %62, i32* %10
br label %50
63: ; preds = %50
br label %64
64: ; preds = %63, %46
br label %65
65: ; preds = %64, %42
br label %66
66: ; preds = %65, %41
%67 = load i32, i32* %4
%68 = call i32 @close(i32 %67)
store i64 -1, i64* %2
br label %85
69: ; preds = %31
%70 = load i32, i32* %4
%71 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%72 = call i32 @read(i32 %70, i8* %71, i32 31)
store i32 %72, i32* %5
%73 = load i32, i32* %4
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %5
%76 = icmp sgt i32 %75, 0
br i1 %76, label %77, label %83
77: ; preds = %69
%78 = load i32, i32* %5
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 %79
store i8 0, i8* %80
%81 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%82 = call i64 @atol(i8* %81)
store i64 %82, i64* %8
br label %83
83: ; preds = %77, %69
%84 = load i64, i64* %8
store i64 %84, i64* %2
br label %85
85: ; preds = %83, %66, %15
%86 = load i64, i64* %2
ret i64 %86
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.stat = type { i32 }
%struct.sockaddr = type { i32 }
@get_time_from_socket.first_time = internal unnamed_addr global i1 false
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i64 0
@ECONNREFUSED = global i64 0
@ENOENT = global i64 0
define i64 @get_time_from_socket(i8* %0) {
%2 = alloca %struct.sockaddr_un
%3 = alloca [32 x i8]
%4 = alloca %struct.stat
%5 = bitcast %struct.sockaddr_un* %2 to i8*
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %58, label %11
11: ; preds = %1
%12 = call i32 @memset(%struct.sockaddr_un* %2, i32 0, i32 8)
%13 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @strcpy(i32 %16, i8* %0)
%18 = load i32, i32* %15
%19 = call i32 @strlen(i32 %18)
%20 = add i32 %19, 5
%21 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%22 = call i64 @connect(i32 %9, %struct.sockaddr* %21, i32 %20)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %50
24: ; preds = %11
%25 = bitcast %struct.stat* %4 to i8*
br label %26
26: ; preds = %24, %45
%27 = load i64, i64* @errno
%28 = load i64, i64* @ECONNREFUSED
%29 = icmp eq i64 %27, %28
br i1 %29, label %48, label %30
30: ; preds = %26
%31 = load i64, i64* @ENOENT
%32 = icmp ne i64 %27, %31
%33 = load i1, i1* @get_time_from_socket.first_time
%34 = select i1 %32, i1 true, i1 %33
br i1 %34, label %48, label %35
35: ; preds = %30
store i1 true, i1* @get_time_from_socket.first_time
br label %37
36: ; preds = %41
br label %48
37: ; preds = %35, %41
%38 = phi i32 [ 0, %35 ], [ %43, %41 ]
%39 = call i64 @stat(i8* %0, %struct.stat* %4)
%40 = icmp eq i64 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %37
%42 = call i32 @usleep(i32 10000)
%43 = add nuw nsw i32 %38, 1
%44 = icmp eq i32 %43, 100
br i1 %44, label %36, label %37
45: ; preds = %37
%46 = call i64 @connect(i32 %9, %struct.sockaddr* %21, i32 %20)
%47 = icmp slt i64 %46, 0
br i1 %47, label %26, label %50
48: ; preds = %30, %26, %36
%49 = call i32 @close(i32 %9)
br label %58
50: ; preds = %45, %11
%51 = call i32 @read(i32 %9, i8* %6, i32 31)
%52 = call i32 @close(i32 %9)
%53 = icmp sgt i32 %51, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = zext i32 %51 to i64
%56 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %55
store i8 0, i8* %56
%57 = call i64 @atol(i8* %6)
br label %58
58: ; preds = %50, %54, %1, %48
%59 = phi i64 [ -1, %48 ], [ -1, %1 ], [ %57, %54 ], [ -1, %50 ]
ret i64 %59
}