#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_SEQPACKET;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  printf("Waiting 5 seconds...\n");
  sleep(5);
  printf("Creating socket...\n");
  int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  if (sockfd == -1) {
    perror("socket");
    return 1;
  };
  printf("Attempting to connect to /run/test.sock...\n");
  struct sockaddr_un addr;
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, "/run/test.sock");
  if (connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) !=
      0) {
    perror("connect");
    return 1;
  };
  printf("Trying to receive...\n");
  char msg[64];
  ssize_t sz = read(sockfd, msg, 64);
  if (sz == -1) {
    perror("read");
    return 1;
  };
  msg[sz] = 0;
  printf("Got message: '%s' (%ld bytes)\n", msg, sz);
  printf("Trying to reply...\n");
  write(sockfd, "TEST MSG", 9);
  printf("Done, bye bye.\n");
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1203 <main+0x73>
lea    0xe38(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <main+0x170>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe36(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     125f <main+0xcf>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <main+0x170>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x40,%edx
call   1070 <read@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    12a3 <main+0x113>
lea    0xdf8(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <main+0x170>
movslq -0x54(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
lea    -0x50(%rbp),%rsi
mov    -0x54(%rbp),%edx
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    0xde9(%rip),%rsi
mov    $0x9,%edx
call   1040 <write@plt>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x58,%rsp
lea    0xe9e(%rip),%rdi
call   1040 <puts@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
lea    0xe9d(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e19(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     12a2 <main+0x102>
mov    %eax,%ebx
lea    0xe7f(%rip),%rdi
call   1040 <puts@plt>
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
lea    0xe00(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   10a0 <connect@plt>
test   %rax,%rax
jne    12ab <main+0x10b>
lea    0xe71(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x40,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     12b4 <main+0x114>
movslq %eax,%rcx
movb   $0x0,0x10(%rsp,%rcx,1)
lea    0xdcc(%rip),%rdi
xor    %ebp,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe45(%rip),%rdi
call   1040 <puts@plt>
lea    0xdc8(%rip),%rsi
mov    %ebx,%edi
mov    $0x9,%edx
call   1050 <write@plt>
lea    0xe39(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x58,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd57(%rip),%rdi
jmp    12bb <main+0x11b>
lea    0xd64(%rip),%rdi
jmp    12bb <main+0x11b>
lea    0xd63(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebp
jmp    1299 <main+0xf9>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Waiting 5 seconds...\0A\00"
@.str.1 = [20 x i8] c"Creating socket...\0A\00"
@AF_UNIX = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [44 x i8] c"Attempting to connect to /run/test.sock...\0A\00"
@.str.4 = [15 x i8] c"/run/test.sock\00"
@.str.5 = [8 x i8] c"connect\00"
@.str.6 = [22 x i8] c"Trying to receive...\0A\00"
@.str.7 = [5 x i8] c"read\00"
@.str.8 = [31 x i8] c"Got message: '%s' (%ld bytes)\0A\00"
@.str.9 = [20 x i8] c"Trying to reply...\0A\00"
@.str.10 = [9 x i8] c"TEST MSG\00"
@.str.11 = [16 x i8] c"Done, bye bye.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca [64 x i8]
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @sleep(i32 5)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_SEQPACKET
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %51
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @strcpy(i32 %21, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%23 = load i32, i32* %2
%24 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 8)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %51
29: ; preds = %16
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0))
%31 = load i32, i32* %2
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 64)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %51
38: ; preds = %29
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 %40
store i8 0, i8* %41
%42 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%43 = load i32, i32* %5
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %42, i32 %43)
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
%46 = load i32, i32* %2
%47 = call i32 @write(i32 %46, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0), i32 9)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.11, i64 0, i64 0))
%49 = load i32, i32* %2
%50 = call i32 @close(i32 %49)
store i32 0, i32* %1
br label %51
51: ; preds = %38, %36, %27, %14
%52 = load i32, i32* %1
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.4 = [15 x i8] c"/run/test.sock\00"
@.str.5 = [8 x i8] c"connect\00"
@.str.7 = [5 x i8] c"read\00"
@.str.8 = [31 x i8] c"Got message: '%s' (%ld bytes)\0A\00"
@.str.10 = [9 x i8] c"TEST MSG\00"
@str = [21 x i8] c"Waiting 5 seconds...\00"
@str.12 = [19 x i8] c"Creating socket...\00"
@str.13 = [43 x i8] c"Attempting to connect to /run/test.sock...\00"
@str.14 = [21 x i8] c"Trying to receive...\00"
@str.15 = [19 x i8] c"Trying to reply...\00"
@str.16 = [15 x i8] c"Done, bye bye.\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [64 x i8]
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @sleep(i32 5)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.12, i64 0, i64 0))
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_SEQPACKET
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %42
12: ; preds = %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.13, i64 0, i64 0))
%14 = bitcast %struct.sockaddr_un* %1 to i8*
%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 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%18 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%19 = call i64 @connect(i32 %8, %struct.sockaddr* %18, i32 8)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %40
23: ; preds = %12
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.14, i64 0, i64 0))
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%26 = call i32 @read(i32 %8, i8* %25, i32 64)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
br label %38
30: ; preds = %23
%31 = sext i32 %26 to i64
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 %31
store i8 0, i8* %32
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.8, i64 0, i64 0), i8* %25, i32 %26)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.15, i64 0, i64 0))
%35 = call i32 @write(i32 %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0), i32 9)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.16, i64 0, i64 0))
%37 = call i32 @close(i32 %8)
br label %38
38: ; preds = %30, %28
%39 = phi i32 [ 1, %28 ], [ 0, %30 ]
br label %40
40: ; preds = %38, %21
%41 = phi i32 [ 1, %21 ], [ %39, %38 ]
br label %42
42: ; preds = %40, %10
%43 = phi i32 [ 1, %10 ], [ %41, %40 ]
ret i32 %43
}