#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 {
  char *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ feof(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int main(void) {
  int s, t, len;
  struct sockaddr_un remote;
  char str[100];
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  printf("Trying to connect...\n");
  remote.sun_family = AF_UNIX;
  strcpy(remote.sun_path, "echo_socket");
  len = strlen(remote.sun_path) + sizeof(remote.sun_family);
  if (connect(s, (struct sockaddr *)&remote, len) == -1) {
    perror("connect");
    exit(1);
  }
  printf("Connected.\n");
  while (printf("> "), fgets(str, 100, stdin), !feof(stdin)) {
    if (send(s, str, strlen(str), 0) == -1) {
      perror("send");
      exit(1);
    }
    if ((t = recv(s, str, 100, 0)) > 0) {
      str[t] = '\0';
      printf("echo> %s", str);
    } else {
      if (t < 0)
        perror("recv");
      else
        printf("Server closed connection\n");
      exit(1);
    }
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    120d <main+0x4d>
lea    0xe1d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1277 <main+0xb7>
lea    0xdba(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2d3f(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1090 <fgets@plt>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
call   10a0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12d0 <main+0x110>
jmp    13a8 <main+0x1e8>
mov    -0x8(%rbp),%eax
mov    %eax,-0x9c(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x98(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x9c(%rbp),%edi
mov    -0x98(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    1328 <main+0x168>
lea    0xd20(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    1370 <main+0x1b0>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rsi
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13a3 <main+0x1e3>
cmpl   $0x0,-0xc(%rbp)
jge    138b <main+0x1cb>
lea    0xcc6(%rip),%rdi
call   10b0 <perror@plt>
jmp    1399 <main+0x1d9>
lea    0xcba(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1285 <main+0xc5>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
mov    0x2e02(%rip),%rbx
mov    (%rbx),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1346 <main+0x176>
mov    %eax,%ebp
lea    0xe20(%rip),%rdi
call   1050 <puts@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0xde4(%rip),%rsi
call   1040 <strcpy@plt>
call   1060 <strlen@plt>
lea    0x4(%rax),%edx
mov    %rsp,%rsi
mov    %ebp,%edi
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
je     134f <main+0x17f>
lea    0xdfc(%rip),%rdi
call   1050 <puts@plt>
lea    0xdc5(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2d83(%rip),%r12
mov    (%r12),%edx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
call   10a0 <fgets@plt>
mov    (%r12),%edi
call   10b0 <feof@plt>
test   %eax,%eax
jne    130c <main+0x13c>
lea    0x10(%rsp),%rbx
lea    0xd8f(%rip),%r14
lea    0xd80(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
cmp    $0xffffffff,%eax
je     1325 <main+0x155>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    132e <main+0x15e>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   10a0 <fgets@plt>
mov    (%r12),%edi
call   10b0 <feof@plt>
test   %eax,%eax
je     12a0 <main+0xd0>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xceb(%rip),%rdi
jmp    1337 <main+0x167>
jns    1358 <main+0x188>
lea    0xcee(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xcb8(%rip),%rdi
jmp    1337 <main+0x167>
lea    0xcb6(%rip),%rdi
jmp    1337 <main+0x167>
lea    0xceb(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [22 x i8] c"Trying to connect...\0A\00"
@.str.2 = [12 x i8] c"echo_socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [12 x i8] c"Connected.\0A\00"
@.str.5 = [3 x i8] c"> \00"
@stdin = global i32 0
@.str.6 = [5 x i8] c"send\00"
@.str.7 = [9 x i8] c"echo> %s\00"
@.str.8 = [5 x i8] c"recv\00"
@.str.9 = [26 x i8] c"Server closed connection\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_un
%6 = alloca [100 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* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @strcpy(i8* %19, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @strlen(i8* %22)
%24 = sext i32 %23 to i64
%25 = add i64 %24, 4
%26 = trunc i64 %25 to i32
store i32 %26, i32* %4
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%29 = load i32, i32* %4
%30 = call i32 @connect(i32 %27, %struct.sockaddr* %28, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %14
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %14
%36 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %76, %35
%38 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%40 = load i32, i32* @stdin
%41 = call i32 @fgets(i8* %39, i32 100, i32 %40)
%42 = load i32, i32* @stdin
%43 = call i32 @feof(i32 %42)
%44 = icmp ne i32 %43, 0
%45 = xor i1 %44, true
br i1 %45, label %46, label %77
46: ; preds = %37
%47 = load i32, i32* %2
%48 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%49 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = call i32 @send(i32 %47, i8* %48, i32 %50, i32 0)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %46
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %46
%57 = load i32, i32* %2
%58 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%59 = call i32 @recv(i32 %57, i8* %58, i32 100, i32 0)
store i32 %59, i32* %3
%60 = icmp sgt i32 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %56
%62 = load i32, i32* %3
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 %63
store i8 0, i8* %64
%65 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%66 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %65)
br label %76
67: ; preds = %56
%68 = load i32, i32* %3
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %67
%71 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %74
72: ; preds = %67
%73 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0))
br label %74
74: ; preds = %72, %70
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %61
br label %37
77: ; preds = %37
%78 = load i32, i32* %2
%79 = call i32 @close(i32 %78)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.2 = [12 x i8] c"echo_socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.5 = [3 x i8] c"> \00"
@stdin = global i32 0
@.str.6 = [5 x i8] c"send\00"
@.str.7 = [9 x i8] c"echo> %s\00"
@.str.8 = [5 x i8] c"recv\00"
@str = [21 x i8] c"Trying to connect...\00"
@str.10 = [11 x i8] c"Connected.\00"
@str.11 = [25 x i8] c"Server closed connection\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [100 x i8]
%3 = bitcast %struct.sockaddr_un* %1 to i8*
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = tail call i32 @strcpy(i8* undef, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @strlen(i8* undef)
%18 = add i32 %17, 4
%19 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%20 = call i32 @connect(i32 %7, %struct.sockaddr* %19, i32 %18)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %12
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.10, i64 0, i64 0))
%27 = call i64 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%28 = load i32, i32* @stdin
%29 = call i32 @fgets(i8* %4, i32 100, i32 %28)
%30 = load i32, i32* @stdin
%31 = call i32 @feof(i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %61
33: ; preds = %25, %43
%34 = call i32 @strlen(i8* %4)
%35 = call i32 @send(i32 %7, i8* %4, i32 %34, i32 0)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = call i32 @recv(i32 %7, i8* %4, i32 100, i32 0)
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %53
43: ; preds = %40
%44 = zext i32 %41 to i64
%45 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %44
store i8 0, i8* %45
%46 = call i64 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %4)
%47 = call i64 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%48 = load i32, i32* @stdin
%49 = call i32 @fgets(i8* %4, i32 100, i32 %48)
%50 = load i32, i32* @stdin
%51 = call i32 @feof(i32 %50)
%52 = icmp eq i32 %51, 0
br i1 %52, label %33, label %61
53: ; preds = %40
%54 = icmp slt i32 %41, 0
br i1 %54, label %55, label %57
55: ; preds = %53
%56 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %59
57: ; preds = %53
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.11, i64 0, i64 0))
br label %59
59: ; preds = %57, %55
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %43, %25
%62 = call i32 @close(i32 %7)
ret i32 0
}