#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ sa;
typedef char pid_t;
typedef int /*<<< orphan*/ pid;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int SOCK_STREAM;
int /*<<< orphan*/ add_field(int, char *, size_t, char **, size_t *, size_t *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr const *, int);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ free(char *);
char *get_cmdline(size_t *);
char *getenv(char *);
char getpid();
char getppid();
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, size_t *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
size_t strlen(char *);
scalar_t__ write(int, char *, size_t);
void init() {
  char *logp_socket_path;
  int fd;
  struct sockaddr_un sa;
  char *output;
  size_t output_size;
  size_t output_allocated;
  logp_socket_path = getenv("LOGP_SOCKET_PATH");
  if (!logp_socket_path)
    return;
  int sock_type = SOCK_STREAM;
  fd = socket(AF_UNIX, sock_type, 0);
  if (fd == -1)
    return;
  fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
  sa.sun_family = AF_UNIX;
  size_t path_len = strlen(logp_socket_path);
  if (path_len >= sizeof(sa.sun_path))
    return;
  strcpy(sa.sun_path, logp_socket_path);
  if (connect(fd, (const struct sockaddr *)&sa, sizeof(sa)) == -1) {
    close(fd);
    return;
  }
  output_allocated = 4096;
  output_size = 0;
  output = malloc(output_allocated);
  if (!output) {
    close(fd);
    return;
  }
  output_size += sizeof(size_t);
  {
    pid_t pid = getpid();
    add_field(1, &pid, sizeof(pid), &output, &output_size, &output_allocated);
  }
  {
    pid_t pid = getppid();
    add_field(2, &pid, sizeof(pid), &output, &output_size, &output_allocated);
  }
  {
    size_t len;
    char *cmdline = get_cmdline(&len);
    if (cmdline) {
      add_field(3, cmdline, len, &output, &output_size, &output_allocated);
      free(cmdline);
    }
  }
  memcpy(output, &output_size, sizeof(size_t));
  ssize_t write_ret = write(fd, output, output_size);
  if (write_ret <= 0) {
    close(fd);
    return;
  }
}


x86 assembly (clang, -O0):
<init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    0xe01(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1218 <init+0x28>
jmp    13ed <init+0x1fd>
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1249 <init+0x59>
jmp    13ed <init+0x1fd>
mov    -0xc(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1070 <fcntl@plt>
mov    -0x68(%rbp),%edi
mov    -0x64(%rbp),%esi
mov    %eax,%edx
mov    0x2d4b(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1070 <fcntl@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <strlen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x4,-0x40(%rbp)
jb     12af <init+0xbf>
jmp    13ed <init+0x1fd>
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
jne    12e2 <init+0xf2>
mov    -0xc(%rbp),%edi
call   10b0 <close@plt>
jmp    13ed <init+0x1fd>
movq   $0x1000,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   10d0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1317 <init+0x127>
mov    -0xc(%rbp),%edi
call   10b0 <close@plt>
jmp    13ed <init+0x1fd>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1090 <getpid@plt>
mov    %al,-0x41(%rbp)
mov    $0x1,%edi
lea    -0x41(%rbp),%rsi
mov    $0x1,%edx
lea    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%r8
lea    -0x30(%rbp),%r9
call   1060 <add_field@plt>
mov    $0x0,%al
call   10f0 <getppid@plt>
mov    %al,-0x42(%rbp)
mov    $0x2,%edi
lea    -0x42(%rbp),%rsi
mov    $0x1,%edx
lea    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%r8
lea    -0x30(%rbp),%r9
call   1060 <add_field@plt>
lea    -0x50(%rbp),%rdi
call   10e0 <get_cmdline@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     13b4 <init+0x1c4>
mov    -0x58(%rbp),%rsi
mov    -0x50(%rbp),%rdx
mov    $0x3,%edi
lea    -0x20(%rbp),%rcx
lea    -0x28(%rbp),%r8
lea    -0x30(%rbp),%r9
call   1060 <add_field@plt>
mov    -0x58(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   10c0 <memcpy@plt>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1080 <write@plt>
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jg     13ed <init+0x1fd>
mov    -0xc(%rbp),%edi
call   10b0 <close@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0xdff(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1395 <init+0x1a5>
mov    %rax,%rbx
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%r15
mov    (%r15),%edi
xor    %edx,%edx
call   1110 <socket@plt>
cmp    $0xffffffff,%eax
je     1395 <init+0x1a5>
mov    %eax,%r14d
mov    0x2d99(%rip),%rax
mov    (%rax),%ebp
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %eax,%eax
call   1070 <fcntl@plt>
mov    0x2d6d(%rip),%rcx
or     (%rcx),%eax
mov    %r14d,%edi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fcntl@plt>
mov    (%r15),%eax
mov    %eax,0x24(%rsp)
mov    %rbx,%rdi
call   10a0 <strlen@plt>
cmp    $0x3,%rax
ja     1395 <init+0x1a5>
mov    %rbx,%rsi
call   1050 <strcpy@plt>
lea    0x20(%rsp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
je     138d <init+0x19d>
movq   $0x1000,0x18(%rsp)
mov    $0x1000,%edi
call   10d0 <malloc@plt>
mov    %rax,0x10(%rsp)
test   %rax,%rax
je     138d <init+0x19d>
movq   $0x8,0x8(%rsp)
xor    %eax,%eax
call   1090 <getpid@plt>
mov    %al,(%rsp)
mov    %rsp,%rsi
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%rbp
lea    0x18(%rsp),%r15
mov    $0x1,%edx
mov    $0x1,%edi
mov    %rbx,%rcx
mov    %rbp,%r8
mov    %r15,%r9
call   1060 <add_field@plt>
xor    %eax,%eax
call   10f0 <getppid@plt>
mov    %al,(%rsp)
mov    %rsp,%rsi
mov    $0x1,%edx
mov    $0x2,%edi
mov    %rbx,%rcx
mov    %rbp,%r8
mov    %r15,%r9
call   1060 <add_field@plt>
mov    %rsp,%rdi
call   10e0 <get_cmdline@plt>
test   %rax,%rax
je     1362 <init+0x172>
mov    %rax,%rbx
mov    (%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0x8(%rsp),%r8
lea    0x18(%rsp),%r9
mov    $0x3,%edi
mov    %rax,%rsi
call   1060 <add_field@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
call   10c0 <memcpy@plt>
mov    0x10(%rsp),%rsi
mov    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1080 <write@plt>
test   %rax,%rax
jg     1395 <init+0x1a5>
mov    %r14d,%edi
call   10b0 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [17 x i8] c"LOGP_SOCKET_PATH\00"
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
define void @init() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8
%10 = alloca i8
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = call i8* @getenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %1
%15 = load i8*, i8** %1
%16 = icmp ne i8* %15, null
br i1 %16, label %18, label %17
17: ; preds = %0
br label %90
18: ; preds = %0
%19 = load i32, i32* @SOCK_STREAM
store i32 %19, i32* %7
%20 = load i32, i32* @AF_UNIX
%21 = load i32, i32* %7
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %2
%23 = load i32, i32* %2
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %18
br label %90
26: ; preds = %18
%27 = load i32, i32* %2
%28 = load i32, i32* @F_SETFD
%29 = load i32, i32* %2
%30 = load i32, i32* @F_GETFD
%31 = call i32 (i32, i32, ...) @fcntl(i32 %29, i32 %30)
%32 = load i32, i32* @FD_CLOEXEC
%33 = or i32 %31, %32
%34 = call i32 (i32, i32, ...) @fcntl(i32 %27, i32 %28, i32 %33)
%35 = load i32, i32* @AF_UNIX
%36 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i8*, i8** %1
%38 = call i64 @strlen(i8* %37)
store i64 %38, i64* %8
%39 = load i64, i64* %8
%40 = icmp uge i64 %39, 4
br i1 %40, label %41, label %42
41: ; preds = %26
br label %90
42: ; preds = %26
%43 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%44 = load i32, i32* %43
%45 = load i8*, i8** %1
%46 = call i32 @strcpy(i32 %44, i8* %45)
%47 = load i32, i32* %2
%48 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%49 = call i32 @connect(i32 %47, %struct.sockaddr* %48, i32 8)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %42
%52 = load i32, i32* %2
%53 = call i32 @close(i32 %52)
br label %90
54: ; preds = %42
store i64 4096, i64* %6
store i64 0, i64* %5
%55 = load i64, i64* %6
%56 = call i8* @malloc(i64 %55)
store i8* %56, i8** %4
%57 = load i8*, i8** %4
%58 = icmp ne i8* %57, null
br i1 %58, label %62, label %59
59: ; preds = %54
%60 = load i32, i32* %2
%61 = call i32 @close(i32 %60)
br label %90
62: ; preds = %54
%63 = load i64, i64* %5
%64 = add i64 %63, 8
store i64 %64, i64* %5
%65 = call signext i8 (...) @getpid()
store i8 %65, i8* %9
%66 = call i32 @add_field(i32 1, i8* %9, i64 1, i8** %4, i64* %5, i64* %6)
%67 = call signext i8 (...) @getppid()
store i8 %67, i8* %10
%68 = call i32 @add_field(i32 2, i8* %10, i64 1, i8** %4, i64* %5, i64* %6)
%69 = call i8* @get_cmdline(i64* %11)
store i8* %69, i8** %12
%70 = load i8*, i8** %12
%71 = icmp ne i8* %70, null
br i1 %71, label %72, label %78
72: ; preds = %62
%73 = load i8*, i8** %12
%74 = load i64, i64* %11
%75 = call i32 @add_field(i32 3, i8* %73, i64 %74, i8** %4, i64* %5, i64* %6)
%76 = load i8*, i8** %12
%77 = call i32 @free(i8* %76)
br label %78
78: ; preds = %72, %62
%79 = load i8*, i8** %4
%80 = call i32 @memcpy(i8* %79, i64* %5, i32 8)
%81 = load i32, i32* %2
%82 = load i8*, i8** %4
%83 = load i64, i64* %5
%84 = call i64 @write(i32 %81, i8* %82, i64 %83)
store i64 %84, i64* %13
%85 = load i64, i64* %13
%86 = icmp sle i64 %85, 0
br i1 %86, label %87, label %90
87: ; preds = %78
%88 = load i32, i32* %2
%89 = call i32 @close(i32 %88)
br label %90
90: ; preds = %17, %25, %41, %51, %59, %87, %78
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [17 x i8] c"LOGP_SOCKET_PATH\00"
@SOCK_STREAM = global i32 0
@AF_UNIX = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
define void @init() {
%1 = alloca %struct.sockaddr_un
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8
%6 = alloca i8
%7 = alloca i64
%8 = bitcast %struct.sockaddr_un* %1 to i8*
%9 = bitcast i8** %2 to i8*
%10 = bitcast i64* %3 to i8*
%11 = bitcast i64* %4 to i8*
%12 = tail call i8* @getenv(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %59, label %14
14: ; preds = %0
%15 = load i32, i32* @SOCK_STREAM
%16 = load i32, i32* @AF_UNIX
%17 = tail call i32 @socket(i32 %16, i32 %15, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %59, label %19
19: ; preds = %14
%20 = load i32, i32* @F_SETFD
%21 = load i32, i32* @F_GETFD
%22 = tail call i32 (i32, i32, ...) @fcntl(i32 %17, i32 %21)
%23 = load i32, i32* @FD_CLOEXEC
%24 = or i32 %23, %22
%25 = tail call i32 (i32, i32, ...) @fcntl(i32 %17, i32 %20, i32 %24)
%26 = load i32, i32* @AF_UNIX
%27 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %26, i32* %27
%28 = tail call i64 @strlen(i8* %12)
%29 = icmp ugt i64 %28, 3
br i1 %29, label %59, label %30
30: ; preds = %19
%31 = tail call i32 @strcpy(i32 undef, i8* %12)
%32 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%33 = call i32 @connect(i32 %17, %struct.sockaddr* %32, i32 8)
%34 = icmp eq i32 %33, -1
br i1 %34, label %57, label %35
35: ; preds = %30
store i64 4096, i64* %4
%36 = call dereferenceable_or_null(4096) i8* @malloc(i64 4096)
store i8* %36, i8** %2
%37 = icmp eq i8* %36, null
br i1 %37, label %57, label %38
38: ; preds = %35
store i64 8, i64* %3
%39 = call signext i8 (...) @getpid()
store i8 %39, i8* %5
%40 = call i32 @add_field(i32 1, i8* %5, i64 1, i8** %2, i64* %3, i64* %4)
%41 = call signext i8 (...) @getppid()
store i8 %41, i8* %6
%42 = call i32 @add_field(i32 2, i8* %6, i64 1, i8** %2, i64* %3, i64* %4)
%43 = bitcast i64* %7 to i8*
%44 = call i8* @get_cmdline(i64* %7)
%45 = icmp eq i8* %44, null
br i1 %45, label %50, label %46
46: ; preds = %38
%47 = load i64, i64* %7
%48 = call i32 @add_field(i32 3, i8* %44, i64 %47, i8** %2, i64* %3, i64* %4)
%49 = call i32 @free(i8* %44)
br label %50
50: ; preds = %46, %38
%51 = load i8*, i8** %2
%52 = call i32 @memcpy(i8* %51, i64* %3, i32 8)
%53 = load i8*, i8** %2
%54 = load i64, i64* %3
%55 = call i64 @write(i32 %17, i8* %53, i64 %54)
%56 = icmp slt i64 %55, 1
br i1 %56, label %57, label %59
57: ; preds = %50, %35, %30
%58 = call i32 @close(i32 %17)
br label %59
59: ; preds = %57, %14, %50, %19, %0
ret void
}