#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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_un *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(void *, char *);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  int sockfd;
  struct sockaddr_un addr;
  char path[104] = "/tmp/unix_socket";
  int len;
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd == -1) {
    printf("create socket failed.\n");
    return -1;
  }
  bzero(&addr, sizeof(addr));
  addr.sun_family = AF_UNIX;
  strcpy((void *)&addr.sun_path, "/tmp/unix_socket");
  if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    printf("connect error.\n");
    return 1;
  }
  printf("write messages to echo server.\n");
  do {
    char buf[512];
    int n;
    printf("> ");
    fgets(buf, 512, stdin);
    if (send(sockfd, buf, strlen(buf), 0) == -1) {
      printf("send error\n");
      break;
    }
    if ((n = recv(sockfd, buf, 512, 0)) <= 0) {
      printf("receive error\n");
      break;
    } else {
      buf[n] = '\0';
      printf("server> %s", buf);
    }
  } while (1);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2c0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x90(%rbp),%rdi
lea    0xe29(%rip),%rsi
mov    $0x68,%edx
call   10a0 <memcpy@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1221 <main+0x71>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1389 <main+0x1d9>
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    127f <main+0xcf>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1389 <main+0x1d9>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x2a0(%rbp),%rdi
mov    0x2d37(%rip),%rax
mov    (%rax),%edx
mov    $0x200,%esi
call   1090 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2b4(%rbp)
lea    -0x2a0(%rbp),%rax
mov    %rax,-0x2b0(%rbp)
lea    -0x2a0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x2b4(%rbp),%edi
mov    -0x2b0(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    130a <main+0x15a>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    137a <main+0x1ca>
mov    -0x14(%rbp),%edi
lea    -0x2a0(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x2a4(%rbp)
cmp    $0x0,%eax
jg     1342 <main+0x192>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    137a <main+0x1ca>
movslq -0x2a4(%rbp),%rax
movb   $0x0,-0x2a0(%rbp,%rax,1)
lea    -0x2a0(%rbp),%rsi
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    136b <main+0x1bb>
mov    $0x1,%al
test   $0x1,%al
jne    128d <main+0xdd>
jmp    137a <main+0x1ca>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    0x2e20(%rip),%r14
mov    (%r14),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x157>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
lea    0xdfa(%rip),%rsi
mov    %rbp,%rdi
call   1040 <strcpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x8,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
je     131b <main+0x16b>
lea    0xdf2(%rip),%rdi
call   1050 <puts@plt>
lea    0xdd8(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2d96(%rip),%r14
mov    (%r14),%edx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%esi
call   10a0 <fgets@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
lea    0xdd0(%rip),%rdi
cmp    $0xffffffff,%eax
je     1336 <main+0x186>
lea    0x10(%rsp),%rbp
lea    0xd83(%rip),%r12
lea    0xd79(%rip),%r13
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    132f <main+0x17f>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r12,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r13,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    (%r14),%edx
mov    %rbp,%rdi
mov    $0x200,%esi
call   10a0 <fgets@plt>
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0xffffffff,%eax
jne    12a0 <main+0xf0>
lea    0xd47(%rip),%rdi
jmp    1336 <main+0x186>
lea    0xd58(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%r15d
jmp    1342 <main+0x192>
lea    0xd35(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%r15d
jmp    1342 <main+0x192>
lea    0xd08(%rip),%rdi
call   1050 <puts@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %r15d,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.path = [104 x i8] c"/tmp/unix_socket\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [23 x i8] c"create socket failed.\0A\00"
@.str.1 = [17 x i8] c"/tmp/unix_socket\00"
@.str.2 = [16 x i8] c"connect error.\0A\00"
@.str.3 = [32 x i8] c"write messages to echo server.\0A\00"
@.str.4 = [3 x i8] c"> \00"
@stdin = global i32 0
@.str.5 = [12 x i8] c"send error\0A\00"
@.str.6 = [15 x i8] c"receive error\0A\00"
@.str.7 = [11 x i8] c"server> %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
%8 = alloca [104 x i8]
%9 = alloca i32
%10 = alloca [512 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [104 x i8]* %8 to i8*
%13 = load i32, i32* @AF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %66
20: ; preds = %2
%21 = call i32 @bzero(%struct.sockaddr_un* %7, i32 8)
%22 = load i32, i32* @AF_UNIX
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%25 = bitcast i32* %24 to i8*
%26 = call i32 @strcpy(i8* %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* %6
%28 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%29 = call i32 @connect(i32 %27, %struct.sockaddr* %28, i32 8)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %66
33: ; preds = %20
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %62, %33
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%38 = load i32, i32* @stdin
%39 = call i32 @fgets(i8* %37, i32 512, i32 %38)
%40 = load i32, i32* %6
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%42 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%43 = call i32 @strlen(i8* %42)
%44 = call i32 @send(i32 %40, i8* %41, i32 %43, i32 0)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %35
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %63
48: ; preds = %35
%49 = load i32, i32* %6
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%51 = call i32 @recv(i32 %49, i8* %50, i32 512, i32 0)
store i32 %51, i32* %11
%52 = icmp sle i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %63
55: ; preds = %48
%56 = load i32, i32* %11
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %57
store i8 0, i8* %58
%59 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %55
br label %62
62: ; preds = %61
br i1 true, label %35, label %63
63: ; preds = %62, %53, %46
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
store i32 0, i32* %3
br label %66
66: ; preds = %63, %31, %18
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [17 x i8] c"/tmp/unix_socket\00"
@.str.4 = [3 x i8] c"> \00"
@stdin = global i32 0
@.str.7 = [11 x i8] c"server> %s\00"
@str = [31 x i8] c"write messages to echo server.\00"
@str.8 = [14 x i8] c"receive error\00"
@str.9 = [11 x i8] c"send error\00"
@str.10 = [15 x i8] c"connect error.\00"
@str.11 = [22 x i8] c"create socket failed.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca [512 x i8]
%5 = bitcast %struct.sockaddr_un* %3 to i8*
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%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 = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.11, i64 0, i64 0))
br label %48
12: ; preds = %2
%13 = call i32 @bzero(%struct.sockaddr_un* %3, i32 8)
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%17 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%18 = call i32 @connect(i32 %8, %struct.sockaddr* %17, i32 8)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.10, i64 0, i64 0))
br label %48
22: ; preds = %12
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%26 = load i32, i32* @stdin
%27 = call i32 @fgets(i8* %24, i32 512, i32 %26)
%28 = call i32 @strlen(i8* %24)
%29 = call i32 @send(i32 %8, i8* %24, i32 %28, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %44, label %31
31: ; preds = %22, %34
%32 = call i32 @recv(i32 %8, i8* %24, i32 512, i32 0)
%33 = icmp slt i32 %32, 1
br i1 %33, label %44, label %34
34: ; preds = %31
%35 = zext i32 %32 to i64
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %35
store i8 0, i8* %36
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* %24)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* @stdin
%40 = call i32 @fgets(i8* %24, i32 512, i32 %39)
%41 = call i32 @strlen(i8* %24)
%42 = call i32 @send(i32 %8, i8* %24, i32 %41, i32 0)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %31
44: ; preds = %31, %34, %22
%45 = phi i8* [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.9, i64 0, i64 0), %22 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @str.9, i64 0, i64 0), %34 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0), %31 ]
%46 = call i32 @puts(i8* dereferenceable(1) %45)
%47 = call i32 @close(i32 %8)
br label %48
48: ; preds = %44, %20, %10
%49 = phi i32 [ -1, %10 ], [ 1, %20 ], [ 0, %44 ]
ret i32 %49
}