#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*/ debug_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ EINTR;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(int /*<<< orphan*/, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main() {
  const char *socket_dir;
  int debug_client;
  struct sockaddr_un debug_addr;
  int n_read;
  debug_client = socket(AF_UNIX, SOCK_STREAM, 0);
  if (debug_client < 0) {
    perror("socket");
    return EXIT_FAILURE;
  }
  socket_dir = getenv("LXK_RUNTIME_DIR");
  if (!socket_dir)
    socket_dir = ".";
  memset(&debug_addr, 0, sizeof(debug_addr));
  debug_addr.sun_family = AF_UNIX;
  snprintf(debug_addr.sun_path, sizeof(debug_addr.sun_path),
           "%s/membroker.debug", socket_dir);
  if (0 > connect(debug_client, (struct sockaddr *)&debug_addr,
                  sizeof(debug_addr))) {
    perror("connect");
    return EXIT_FAILURE;
  }
  do {
    char buf[1024];
    do {
      n_read = read(debug_client, buf, sizeof(buf));
    } while (n_read != -1 && errno == EINTR);
    fwrite(buf, n_read, 1, stdout);
  } while (n_read > 0);
  close(debug_client);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e5 <main+0x55>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1314 <main+0x184>
lea    0xe1b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    120b <main+0x7b>
lea    0xe10(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rcx
mov    $0x4,%esi
lea    0xddf(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jle    127b <main+0xeb>
lea    0xdc7(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1314 <main+0x184>
jmp    1280 <main+0xf0>
jmp    1285 <main+0xf5>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x24(%rbp)
mov    %al,-0x431(%rbp)
je     12cb <main+0x13b>
mov    0x2d1b(%rip),%rax
mov    (%rax),%rax
mov    0x2d01(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x431(%rbp)
mov    -0x431(%rbp),%al
test   $0x1,%al
jne    1285 <main+0xf5>
lea    -0x430(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    0x2cce(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   10a0 <fwrite@plt>
cmpl   $0x0,-0x24(%rbp)
jg     1280 <main+0xf0>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    0x2e40(%rip),%r15
mov    (%r15),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     12a3 <main+0x113>
mov    %eax,%ebx
lea    0xe3b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe3c(%rip),%rbp
cmovne %rax,%rbp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rsp),%edi
lea    0xe14(%rip),%rdx
mov    $0x4,%esi
mov    %rbp,%rcx
call   1040 <snprintf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     12ac <main+0x11c>
lea    0x10(%rsp),%r14
mov    0x2d9a(%rip),%r15
mov    0x2d83(%rip),%r12
mov    0x2d74(%rip),%r13
jmp    1260 <main+0xd0>
mov    0x0(%r13),%ecx
mov    %r14,%rdi
mov    %ebp,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
test   %ebp,%ebp
jle    1281 <main+0xf1>
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1246 <main+0xb6>
mov    (%r12),%rax
cmp    %rax,(%r15)
je     1260 <main+0xd0>
jmp    1246 <main+0xb6>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2d21(%rip),%rax
mov    (%rax),%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd56(%rip),%rdi
jmp    12b3 <main+0x123>
lea    0xd79(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d09(%rip),%rax
jmp    128f <main+0xff>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"LXK_RUNTIME_DIR\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [19 x i8] c"%s/membroker.debug\00"
@.str.4 = [8 x i8] c"connect\00"
@errno = global i64 0
@EINTR = global i64 0
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca [1024 x i8]
store i32 0, i32* %1
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
store i32 %14, i32* %1
br label %62
15: ; preds = %0
%16 = call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %2
%17 = load i8*, i8** %2
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %15
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %2
br label %20
20: ; preds = %19, %15
%21 = call i32 @memset(%struct.sockaddr_un* %4, i32 0, i32 8)
%22 = load i32, i32* @AF_UNIX
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i8*, i8** %2
%27 = call i32 @snprintf(i32 %25, i32 4, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = load i32, i32* %3
%29 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%30 = call i64 @connect(i32 %28, %struct.sockaddr* %29, i32 8)
%31 = icmp sgt i64 0, %30
br i1 %31, label %32, label %35
32: ; preds = %20
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
store i32 %34, i32* %1
br label %62
35: ; preds = %20
br label %36
36: ; preds = %55, %35
br label %37
37: ; preds = %48, %36
%38 = load i32, i32* %3
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 1024)
store i32 %40, i32* %5
br label %41
41: ; preds = %37
%42 = load i32, i32* %5
%43 = icmp ne i32 %42, -1
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = load i64, i64* @errno
%46 = load i64, i64* @EINTR
%47 = icmp eq i64 %45, %46
br label %48
48: ; preds = %44, %41
%49 = phi i1 [ false, %41 ], [ %47, %44 ]
br i1 %49, label %37, label %50
50: ; preds = %48
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%52 = load i32, i32* %5
%53 = load i32, i32* @stdout
%54 = call i32 @fwrite(i8* %51, i32 %52, i32 1, i32 %53)
br label %55
55: ; preds = %50
%56 = load i32, i32* %5
%57 = icmp sgt i32 %56, 0
br i1 %57, label %36, label %58
58: ; preds = %55
%59 = load i32, i32* %3
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* @EXIT_SUCCESS
store i32 %61, i32* %1
br label %62
62: ; preds = %58, %32, %12
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"LXK_RUNTIME_DIR\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [19 x i8] c"%s/membroker.debug\00"
@.str.4 = [8 x i8] c"connect\00"
@errno = global i64 0
@EINTR = global i64 0
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [1024 x i8]
%3 = bitcast %struct.sockaddr_un* %1 to i8*
%4 = load i32, i32* @AF_UNIX
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %41
10: ; preds = %0
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i8* %11, null
%13 = select i1 %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %11
%14 = call i32 @memset(%struct.sockaddr_un* %1, i32 0, i32 8)
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @snprintf(i32 %18, i32 4, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%20 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%21 = call i64 @connect(i32 %6, %struct.sockaddr* %20, i32 8)
%22 = icmp slt i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %10
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %27
25: ; preds = %10
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
br label %41
27: ; preds = %23, %35
br label %28
28: ; preds = %28, %27
%29 = call i32 @read(i32 %6, i8* %24, i32 1024)
%30 = icmp ne i32 %29, -1
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
%34 = select i1 %30, i1 %33, i1 false
br i1 %34, label %28, label %35
35: ; preds = %28
%36 = load i32, i32* @stdout
%37 = call i32 @fwrite(i8* %24, i32 %29, i32 1, i32 %36)
%38 = icmp sgt i32 %29, 0
br i1 %38, label %27, label %39
39: ; preds = %35
%40 = call i32 @close(i32 %6)
br label %41
41: ; preds = %39, %25, %8
%42 = phi i32* [ @EXIT_FAILURE, %8 ], [ @EXIT_FAILURE, %25 ], [ @EXIT_SUCCESS, %39 ]
%43 = load i32, i32* %42
ret i32 %43
}