#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(int argc, char **argv) {
  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\n");
  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    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1214 <main+0x54>
lea    0xdfb(%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    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    127e <main+0xbe>
lea    0xdbb(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdad(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0xa0(%rbp),%rdi
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1090 <fgets@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   10a0 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12d7 <main+0x117>
jmp    13af <main+0x1ef>
mov    -0x14(%rbp),%eax
mov    %eax,-0xac(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    132f <main+0x16f>
lea    0xd21(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1377 <main+0x1b7>
movslq -0x18(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
lea    -0xa0(%rbp),%rsi
lea    0xcdb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13aa <main+0x1ea>
cmpl   $0x0,-0x18(%rbp)
jge    1392 <main+0x1d2>
lea    0xcc7(%rip),%rdi
call   10b0 <perror@plt>
jmp    13a0 <main+0x1e0>
lea    0xcbb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    128c <main+0xcc>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0xb0,%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    0xe28(%rip),%rdi
call   1050 <puts@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0xdeb(%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    0xe04(%rip),%rdi
call   1050 <puts@plt>
lea    0xdcd(%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    0xd97(%rip),%r14
lea    0xd88(%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    0xcf3(%rip),%rdi
jmp    1337 <main+0x167>
jns    1358 <main+0x188>
lea    0xcf6(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xcb3(%rip),%rdi
jmp    1337 <main+0x167>
lea    0xcbe(%rip),%rdi
jmp    1337 <main+0x167>
lea    0xcf3(%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 = [13 x i8] c"echo_socket\0A\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(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_un
%10 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @AF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @strlen(i8* %26)
%28 = sext i32 %27 to i64
%29 = add i64 %28, 4
%30 = trunc i64 %29 to i32
store i32 %30, i32* %8
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%33 = load i32, i32* %8
%34 = call i32 @connect(i32 %31, %struct.sockaddr* %32, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %18
%37 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %18
%40 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %80, %39
%42 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%43 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%44 = load i32, i32* @stdin
%45 = call i32 @fgets(i8* %43, i32 100, i32 %44)
%46 = load i32, i32* @stdin
%47 = call i32 @feof(i32 %46)
%48 = icmp ne i32 %47, 0
%49 = xor i1 %48, true
br i1 %49, label %50, label %81
50: ; preds = %41
%51 = load i32, i32* %6
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%54 = call i32 @strlen(i8* %53)
%55 = call i32 @send(i32 %51, i8* %52, i32 %54, i32 0)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %50
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %50
%61 = load i32, i32* %6
%62 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%63 = call i32 @recv(i32 %61, i8* %62, i32 100, i32 0)
store i32 %63, i32* %7
%64 = icmp sgt i32 %63, 0
br i1 %64, label %65, label %71
65: ; preds = %60
%66 = load i32, i32* %7
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 %67
store i8 0, i8* %68
%69 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%70 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %69)
br label %80
71: ; preds = %60
%72 = load i32, i32* %7
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %71
%75 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %78
76: ; preds = %71
%77 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0))
br label %78
78: ; preds = %76, %74
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %65
br label %41
81: ; preds = %41
%82 = load i32, i32* %6
%83 = call i32 @close(i32 %82)
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 = [13 x i8] c"echo_socket\0A\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(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca [100 x i8]
%5 = bitcast %struct.sockaddr_un* %3 to i8*
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @strcpy(i8* undef, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @strlen(i8* undef)
%20 = add i32 %19, 4
%21 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%22 = call i32 @connect(i32 %9, %struct.sockaddr* %21, i32 %20)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %14
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.10, i64 0, i64 0))
%29 = call i64 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%30 = load i32, i32* @stdin
%31 = call i32 @fgets(i8* %6, i32 100, i32 %30)
%32 = load i32, i32* @stdin
%33 = call i32 @feof(i32 %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %63
35: ; preds = %27, %45
%36 = call i32 @strlen(i8* %6)
%37 = call i32 @send(i32 %9, i8* %6, i32 %36, i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %35
%43 = call i32 @recv(i32 %9, i8* %6, i32 100, i32 0)
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %42
%46 = zext i32 %43 to i64
%47 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %46
store i8 0, i8* %47
%48 = call i64 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %6)
%49 = call i64 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%50 = load i32, i32* @stdin
%51 = call i32 @fgets(i8* %6, i32 100, i32 %50)
%52 = load i32, i32* @stdin
%53 = call i32 @feof(i32 %52)
%54 = icmp eq i32 %53, 0
br i1 %54, label %35, label %63
55: ; preds = %42
%56 = icmp slt i32 %43, 0
br i1 %56, label %57, label %59
57: ; preds = %55
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %61
59: ; preds = %55
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.11, i64 0, i64 0))
br label %61
61: ; preds = %59, %57
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %45, %27
%64 = call i32 @close(i32 %9)
ret i32 0
}