#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;
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_SEQPACKET;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  printf("Creating socket...\n");
  int sockfd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
  if (sockfd == -1) {
    perror("socket");
    return 1;
  };
  printf("Binding to /run/test.sock...\n");
  struct sockaddr_un addr;
  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, "/run/test.sock");
  if (bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) != 0) {
    perror("bind");
    return 1;
  };
  printf("Listening...\n");
  if (listen(sockfd, 5) != 0) {
    perror("listen");
    return 1;
  };
  printf("Accepting...\n");
  while (1) {
    struct sockaddr_un cliaddr;
    socklen_t addrlen = sizeof(struct sockaddr_un);
    int client = accept(sockfd, (struct sockaddr *)&cliaddr, &addrlen);
    if (client == -1) {
      perror("accept");
      return 1;
    };
    printf("Sending message...\n");
    write(client, "hello world", 12);
    printf("Waiting for message back...\n");
    char msgback[64];
    ssize_t sz = read(client, msgback, 64);
    if (sz == -1) {
      perror("read");
    } else {
      msgback[sz] = 0;
      printf("Message: '%s' (%ld bytes)\n", msgback, sz);
    };
    close(client);
    printf("OK, looping again...\n");
  };
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11fb <main+0x5b>
lea    0xe2a(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1376 <main+0x1d6>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe1a(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     1257 <main+0xb7>
lea    0xe02(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1376 <main+0x1d6>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     1294 <main+0xf4>
lea    0xdd8(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1376 <main+0x1d6>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x8,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12de <main+0x13e>
lea    0xda3(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1376 <main+0x1d6>
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
lea    0xd95(%rip),%rsi
mov    $0xc,%edx
call   1040 <write@plt>
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1070 <read@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    133d <main+0x19d>
lea    0xd81(%rip),%rdi
call   10a0 <perror@plt>
jmp    135b <main+0x1bb>
movslq -0x64(%rbp),%rax
movb   $0x0,-0x60(%rbp,%rax,1)
lea    -0x60(%rbp),%rsi
mov    -0x64(%rbp),%edx
lea    0xd65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12a2 <main+0x102>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
lea    0xe90(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e17(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
jne    11f4 <main+0x44>
lea    0xe11(%rip),%rdi
jmp    128a <main+0xda>
mov    %eax,%ebp
lea    0xe6b(%rip),%rdi
call   1040 <puts@plt>
mov    (%rbx),%eax
mov    %eax,0x1c(%rsp)
lea    0xdf8(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jne    127a <main+0xca>
lea    0xe54(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
jne    1283 <main+0xd3>
lea    0xe44(%rip),%rdi
call   1040 <puts@plt>
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   10c0 <accept@plt>
cmp    $0xffffffff,%eax
jne    12a3 <main+0xf3>
lea    0xdaa(%rip),%rdi
jmp    128a <main+0xda>
lea    0xd95(%rip),%rdi
jmp    128a <main+0xda>
lea    0xd91(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r12d
lea    0x20(%rsp),%rbx
lea    0xe1c(%rip),%r13
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
jmp    130c <main+0x15c>
xchg   %ax,%ax
movslq %eax,%rcx
movb   $0x0,0x20(%rsp,%rcx,1)
lea    0xd6b(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    %r13,%rdi
call   1040 <puts@plt>
movl   $0x8,0xc(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <accept@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     1271 <main+0xc1>
lea    0xd8c(%rip),%rdi
call   1040 <puts@plt>
mov    %r12d,%edi
lea    0xd07(%rip),%rsi
mov    $0xc,%edx
call   1050 <write@plt>
lea    0xd7f(%rip),%rdi
call   1040 <puts@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
jne    12c0 <main+0x110>
lea    0xcdd(%rip),%rdi
call   10b0 <perror@plt>
jmp    12db <main+0x12b>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"Creating socket...\0A\00"
@AF_UNIX = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [30 x i8] c"Binding to /run/test.sock...\0A\00"
@.str.3 = [15 x i8] c"/run/test.sock\00"
@.str.4 = [5 x i8] c"bind\00"
@.str.5 = [14 x i8] c"Listening...\0A\00"
@.str.6 = [7 x i8] c"listen\00"
@.str.7 = [14 x i8] c"Accepting...\0A\00"
@.str.8 = [7 x i8] c"accept\00"
@.str.9 = [20 x i8] c"Sending message...\0A\00"
@.str.10 = [12 x i8] c"hello world\00"
@.str.11 = [29 x i8] c"Waiting for message back...\0A\00"
@.str.12 = [5 x i8] c"read\00"
@.str.13 = [27 x i8] c"Message: '%s' (%ld bytes)\0A\00"
@.str.14 = [22 x i8] c"OK, looping again...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
%7 = alloca [64 x i8]
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_SEQPACKET
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %70
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @strcpy(i32 %22, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 8)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %70
30: ; preds = %17
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%32 = load i32, i32* %2
%33 = call i64 @listen(i32 %32, i32 5)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %70
37: ; preds = %30
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
br label %39
39: ; preds = %37, %66
store i32 8, i32* %5
%40 = load i32, i32* %2
%41 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%42 = call i32 @accept(i32 %40, %struct.sockaddr* %41, i32* %5)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %1
br label %70
47: ; preds = %39
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.9, i64 0, i64 0))
%49 = load i32, i32* %6
%50 = call i32 @write(i32 %49, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i32 12)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.11, i64 0, i64 0))
%52 = load i32, i32* %6
%53 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 64)
store i32 %54, i32* %8
%55 = load i32, i32* %8
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %47
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0))
br label %66
59: ; preds = %47
%60 = load i32, i32* %8
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 %61
store i8 0, i8* %62
%63 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%64 = load i32, i32* %8
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.13, i64 0, i64 0), i8* %63, i32 %64)
br label %66
66: ; preds = %59, %57
%67 = load i32, i32* %6
%68 = call i32 @close(i32 %67)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.14, i64 0, i64 0))
br label %39
70: ; preds = %45, %35, %28, %15
%71 = load i32, i32* %1
ret i32 %71
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_SEQPACKET = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.3 = [15 x i8] c"/run/test.sock\00"
@.str.4 = [5 x i8] c"bind\00"
@.str.6 = [7 x i8] c"listen\00"
@.str.8 = [7 x i8] c"accept\00"
@.str.10 = [12 x i8] c"hello world\00"
@.str.12 = [5 x i8] c"read\00"
@.str.13 = [27 x i8] c"Message: '%s' (%ld bytes)\0A\00"
@str = [19 x i8] c"Creating socket...\00"
@str.15 = [29 x i8] c"Binding to /run/test.sock...\00"
@str.16 = [13 x i8] c"Listening...\00"
@str.17 = [13 x i8] c"Accepting...\00"
@str.18 = [19 x i8] c"Sending message...\00"
@str.19 = [28 x i8] c"Waiting for message back...\00"
@str.20 = [21 x i8] c"OK, looping again...\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, 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.1, i64 0, i64 0))
br label %59
12: ; preds = %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.15, 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.3, i64 0, i64 0))
%18 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%19 = call i64 @bind(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 ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %58
23: ; preds = %12
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.16, i64 0, i64 0))
%25 = call i64 @listen(i32 %8, i32 5)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %58
29: ; preds = %23
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.17, i64 0, i64 0))
%31 = bitcast %struct.sockaddr_un* %2 to i8*
%32 = bitcast i32* %3 to i8*
%33 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
store i32 8, i32* %3
%34 = call i32 @accept(i32 %8, %struct.sockaddr* %33, i32* %3)
%35 = icmp eq i32 %34, -1
br i1 %35, label %38, label %36
36: ; preds = %29
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
br label %40
38: ; preds = %53, %29
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.8, i64 0, i64 0))
br label %58
40: ; preds = %36, %53
%41 = phi i32 [ %34, %36 ], [ %56, %53 ]
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.18, i64 0, i64 0))
%43 = call i32 @write(i32 %41, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i32 12)
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.19, i64 0, i64 0))
%45 = call i32 @read(i32 %41, i8* %37, i32 64)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0))
br label %53
49: ; preds = %40
%50 = sext i32 %45 to i64
%51 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 %50
store i8 0, i8* %51
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.13, i64 0, i64 0), i8* %37, i32 %45)
br label %53
53: ; preds = %47, %49
%54 = call i32 @close(i32 %41)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.20, i64 0, i64 0))
store i32 8, i32* %3
%56 = call i32 @accept(i32 %8, %struct.sockaddr* %33, i32* %3)
%57 = icmp eq i32 %56, -1
br i1 %57, label %38, label %40
58: ; preds = %38, %27, %21
br label %59
59: ; preds = %58, %10
ret i32 1
}