#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;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
char *getenv(char *);
int read(int, char *, int);
scalar_t__ snprintf(int /*<<< orphan*/, int, char *, char const *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
int write(int, char *, int);
int socket_sucker() {
  int socket_fd = 0;
  int servlen = 0;
  struct sockaddr_un serv_addr;
  bzero((char *)&serv_addr, sizeof(serv_addr));
  const char *home = getenv("HOME");
  if (home == NULL) {
    return -1;
  }
  serv_addr.sun_family = AF_UNIX;
  long unsigned int printsize = (long unsigned int)snprintf(
      serv_addr.sun_path, sizeof(serv_addr.sun_path) - 1, "%s/%s", home,
      ".x2go-socket");
  if (printsize > sizeof(serv_addr.sun_path) - 1 || printsize < 0) {
    return -1;
  }
  servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
  if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  if (connect(socket_fd, (struct sockaddr *)&serv_addr, servlen) < 0) {
    return -1;
  }
  char buffer[512 + 2];
  int in = 0;
  int out = 0;
  in = read(socket_fd, buffer, 512);
  if (in > 0) {
    out = write(1, buffer, in);
  }
  close(socket_fd);
  if (in > 0 && out > 0 && in == out) {
    return 0;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<socket_sucker>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
lea    0xe42(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11de <socket_sucker+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <socket_sucker+0x1a5>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rcx
mov    $0x3,%esi
lea    0xe08(%rip),%rdx
lea    0xe07(%rip),%r8
call   1060 <snprintf@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x3,-0x28(%rbp)
ja     1223 <socket_sucker+0x93>
cmpq   $0x0,-0x28(%rbp)
jae    122f <socket_sucker+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <socket_sucker+0x1a5>
mov    -0x18(%rbp),%edi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0xc(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    1271 <socket_sucker+0xe1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <socket_sucker+0x1a5>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1296 <socket_sucker+0x106>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <socket_sucker+0x1a5>
movl   $0x0,-0x234(%rbp)
movl   $0x0,-0x238(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
jle    12ee <socket_sucker+0x15e>
lea    -0x230(%rbp),%rsi
mov    -0x234(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    %eax,-0x238(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
cmpl   $0x0,-0x234(%rbp)
jle    132e <socket_sucker+0x19e>
cmpl   $0x0,-0x238(%rbp)
jle    132e <socket_sucker+0x19e>
mov    -0x234(%rbp),%eax
cmp    -0x238(%rbp),%eax
jne    132e <socket_sucker+0x19e>
movl   $0x0,-0x4(%rbp)
jmp    1335 <socket_sucker+0x1a5>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_sucker>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
call   1090 <bzero@plt>
lea    0xe4d(%rip),%rdi
call   1030 <getenv@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1289 <socket_sucker+0xf9>
mov    0x2e1b(%rip),%r14
mov    (%r14),%ecx
mov    %ecx,0xc(%rsp)
mov    0x8(%rsp),%ebp
lea    0xe26(%rip),%rdx
lea    0xe25(%rip),%r8
mov    %ebp,%edi
mov    $0x3,%esi
mov    %rax,%rcx
call   1060 <snprintf@plt>
cmp    $0x3,%rax
ja     1289 <socket_sucker+0xf9>
mov    %ebp,%edi
call   1050 <strlen@plt>
mov    %eax,%ebp
mov    (%r14),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1289 <socket_sucker+0xf9>
mov    %eax,%r14d
add    $0x4,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    %ebp,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     1289 <socket_sucker+0xf9>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1265 <socket_sucker+0xd5>
lea    0x10(%rsp),%rsi
mov    $0x1,%edi
mov    %ebx,%edx
call   1040 <write@plt>
mov    %eax,%ebp
jmp    1267 <socket_sucker+0xd7>
xor    %ebp,%ebp
test   %ebx,%ebx
setle  %r15b
mov    %r14d,%edi
call   1070 <close@plt>
test   %ebp,%ebp
setle  %al
or     %r15b,%al
cmp    %ebp,%ebx
setne  %cl
or     %al,%cl
movzbl %cl,%ebx
neg    %ebx
mov    %ebx,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"HOME\00"
@AF_UNIX = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [13 x i8] c".x2go-socket\00"
@SOCK_STREAM = global i32 0
define i32 @socket_sucker() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [514 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %2
store i32 0, i32* %3
%10 = bitcast %struct.sockaddr_un* %4 to i8*
%11 = call i32 @bzero(i8* %10, i32 8)
%12 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %0
store i32 -1, i32* %1
br label %72
16: ; preds = %0
%17 = load i32, i32* @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i8*, i8** %5
%22 = call i64 @snprintf(i32 %20, i32 3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i64 %22, i64* %6
%23 = load i64, i64* %6
%24 = icmp ugt i64 %23, 3
br i1 %24, label %28, label %25
25: ; preds = %16
%26 = load i64, i64* %6
%27 = icmp ult i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25, %16
store i32 -1, i32* %1
br label %72
29: ; preds = %25
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @strlen(i32 %31)
%33 = sext i32 %32 to i64
%34 = add i64 %33, 4
%35 = trunc i64 %34 to i32
store i32 %35, i32* %3
%36 = load i32, i32* @AF_UNIX
%37 = load i32, i32* @SOCK_STREAM
%38 = call i32 @socket(i32 %36, i32 %37, i32 0)
store i32 %38, i32* %2
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %29
store i32 -1, i32* %1
br label %72
41: ; preds = %29
%42 = load i32, i32* %2
%43 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%44 = load i32, i32* %3
%45 = call i64 @connect(i32 %42, %struct.sockaddr* %43, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %41
store i32 -1, i32* %1
br label %72
48: ; preds = %41
store i32 0, i32* %8
store i32 0, i32* %9
%49 = load i32, i32* %2
%50 = getelementptr inbounds [514 x i8], [514 x i8]* %7, i64 0, i64 0
%51 = call i32 @read(i32 %49, i8* %50, i32 512)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %48
%55 = getelementptr inbounds [514 x i8], [514 x i8]* %7, i64 0, i64 0
%56 = load i32, i32* %8
%57 = call i32 @write(i32 1, i8* %55, i32 %56)
store i32 %57, i32* %9
br label %58
58: ; preds = %54, %48
%59 = load i32, i32* %2
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* %8
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %71
63: ; preds = %58
%64 = load i32, i32* %9
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %71
66: ; preds = %63
%67 = load i32, i32* %8
%68 = load i32, i32* %9
%69 = icmp eq i32 %67, %68
br i1 %69, label %70, label %71
70: ; preds = %66
store i32 0, i32* %1
br label %72
71: ; preds = %66, %63, %58
store i32 -1, i32* %1
br label %72
72: ; preds = %71, %70, %47, %40, %28, %15
%73 = load i32, i32* %1
ret i32 %73
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [5 x i8] c"HOME\00"
@AF_UNIX = global i32 0
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [13 x i8] c".x2go-socket\00"
@SOCK_STREAM = global i32 0
define i32 @socket_sucker() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [514 x i8]
%3 = bitcast %struct.sockaddr_un* %1 to i8*
%4 = call i32 @bzero(i8* %3, i32 8)
%5 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %40, label %7
7: ; preds = %0
%8 = load i32, i32* @AF_UNIX
%9 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i64 @snprintf(i32 %11, i32 3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%13 = icmp ugt i64 %12, 3
br i1 %13, label %40, label %14
14: ; preds = %7
%15 = tail call i32 @strlen(i32 %11)
%16 = load i32, i32* @AF_UNIX
%17 = load i32, i32* @SOCK_STREAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %40, label %20
20: ; preds = %14
%21 = add i32 %15, 4
%22 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%23 = call i64 @connect(i32 %18, %struct.sockaddr* %22, i32 %21)
%24 = icmp slt i64 %23, 0
br i1 %24, label %40, label %25
25: ; preds = %20
%26 = getelementptr inbounds [514 x i8], [514 x i8]* %2, i64 0, i64 0
%27 = call i32 @read(i32 %18, i8* %26, i32 512)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @write(i32 1, i8* %26, i32 %27)
br label %31
31: ; preds = %29, %25
%32 = phi i32 [ %30, %29 ], [ 0, %25 ]
%33 = call i32 @close(i32 %18)
%34 = icmp sgt i32 %32, 0
%35 = and i1 %28, %34
%36 = icmp eq i32 %27, %32
%37 = and i1 %36, %35
%38 = xor i1 %37, true
%39 = sext i1 %38 to i32
br label %40
40: ; preds = %31, %7, %14, %20, %0
%41 = phi i32 [ -1, %0 ], [ %39, %31 ], [ -1, %7 ], [ -1, %14 ], [ -1, %20 ]
ret i32 %41
}