#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, ...);
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;
  unsigned int size;
  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)) {
    size = strlen(str);
    send(s, &size, sizeof(unsigned int), 0);
    if (send(s, str, size, 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    $0x90,%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    0xe20(%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,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1277 <main+0xb7>
lea    0xdbd(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdad(%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    13a9 <main+0x1e9>
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1060 <send@plt>
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    1329 <main+0x169>
lea    0xd22(%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    1371 <main+0x1b1>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
lea    -0x90(%rbp),%rsi
lea    0xcdc(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13a4 <main+0x1e4>
cmpl   $0x0,-0xc(%rbp)
jge    138c <main+0x1cc>
lea    0xcc8(%rip),%rdi
call   10b0 <perror@plt>
jmp    139a <main+0x1da>
lea    0xcbc(%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    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    0x2e00(%rip),%rbx
mov    (%rbx),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1367 <main+0x197>
mov    %eax,%ebp
lea    0xe21(%rip),%rdi
call   1050 <puts@plt>
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
lea    0xde2(%rip),%rsi
call   1040 <strcpy@plt>
call   1060 <strlen@plt>
lea    0x4(%rax),%edx
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
je     1370 <main+0x1a0>
lea    0xdfb(%rip),%rdi
call   1050 <puts@plt>
lea    0xdc4(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x2d7f(%rip),%r13
mov    0x0(%r13),%edx
lea    0x20(%rsp),%rdi
mov    $0x64,%esi
call   10a0 <fgets@plt>
mov    0x0(%r13),%edi
call   10b0 <feof@plt>
test   %eax,%eax
jne    132b <main+0x15b>
lea    0x20(%rsp),%rbx
lea    0xc(%rsp),%r14
lea    0xd89(%rip),%r15
lea    0xd7a(%rip),%r12
nopl   (%rax)
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,0xc(%rsp)
mov    $0x4,%edx
mov    %ebp,%edi
mov    %r14,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <send@plt>
mov    0xc(%rsp),%edx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
xor    %eax,%eax
call   1070 <send@plt>
cmp    $0xffffffff,%eax
je     1346 <main+0x176>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    134f <main+0x17f>
mov    %eax,%eax
movb   $0x0,0x20(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   10a0 <fgets@plt>
mov    0x0(%r13),%edi
call   10b0 <feof@plt>
test   %eax,%eax
je     12a0 <main+0xd0>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xccd(%rip),%rdi
jmp    1358 <main+0x188>
jns    1379 <main+0x1a9>
lea    0xcd0(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xc9a(%rip),%rdi
jmp    1358 <main+0x188>
lea    0xc98(%rip),%rdi
jmp    1358 <main+0x188>
lea    0xccd(%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 = [15 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 i32
%6 = alloca %struct.sockaddr_un
%7 = alloca [100 x i8]
store i32 0, i32* %1
%8 = load i32, i32* @AF_UNIX
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @AF_UNIX
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%20 = load i8*, i8** %19
%21 = call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 @strlen(i8* %23)
%25 = sext i32 %24 to i64
%26 = add i64 %25, 4
%27 = trunc i64 %26 to i32
store i32 %27, i32* %4
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%30 = load i32, i32* %4
%31 = call i32 @connect(i32 %28, %struct.sockaddr* %29, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %15
%34 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %15
%37 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %38
38: ; preds = %80, %36
%39 = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%40 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%41 = load i32, i32* @stdin
%42 = call i32 @fgets(i8* %40, i32 100, i32 %41)
%43 = load i32, i32* @stdin
%44 = call i32 @feof(i32 %43)
%45 = icmp ne i32 %44, 0
%46 = xor i1 %45, true
br i1 %46, label %47, label %81
47: ; preds = %38
%48 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%49 = call i32 @strlen(i8* %48)
store i32 %49, i32* %5
%50 = load i32, i32* %2
%51 = call i32 (i32, ...) @send(i32 %50, i32* %5, i64 4, i32 0)
%52 = load i32, i32* %2
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%54 = load i32, i32* %5
%55 = call i32 (i32, ...) @send(i32 %52, i8* %53, i32 %54, i32 0)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %47
%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 = %47
%61 = load i32, i32* %2
%62 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%63 = call i32 @recv(i32 %61, i8* %62, i32 100, i32 0)
store i32 %63, i32* %3
%64 = icmp sgt i32 %63, 0
br i1 %64, label %65, label %71
65: ; preds = %60
%66 = load i32, i32* %3
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 %67
store i8 0, i8* %68
%69 = getelementptr inbounds [100 x i8], [100 x i8]* %7, 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* %3
%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 %38
81: ; preds = %38
%82 = load i32, i32* %2
%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 = [15 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 i32
%2 = alloca %struct.sockaddr_un
%3 = alloca [100 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.sockaddr_un* %2 to i8*
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, 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 = %0
%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 = %0
%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* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @strcpy(i8* undef, i8* getelementptr inbounds ([15 x i8], [15 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* %2 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 %65
35: ; preds = %27, %47
%36 = call i32 @strlen(i8* %6)
store i32 %36, i32* %1
%37 = call i32 (i32, ...) @send(i32 %9, i32* %1, i64 4, i32 0)
%38 = load i32, i32* %1
%39 = call i32 (i32, ...) @send(i32 %9, i8* %6, i32 %38, i32 0)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %35
%45 = call i32 @recv(i32 %9, i8* %6, i32 100, i32 0)
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %57
47: ; preds = %44
%48 = zext i32 %45 to i64
%49 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %48
store i8 0, i8* %49
%50 = call i64 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %6)
%51 = call i64 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* @stdin
%53 = call i32 @fgets(i8* %6, i32 100, i32 %52)
%54 = load i32, i32* @stdin
%55 = call i32 @feof(i32 %54)
%56 = icmp eq i32 %55, 0
br i1 %56, label %35, label %65
57: ; preds = %44
%58 = icmp slt i32 %45, 0
br i1 %58, label %59, label %61
59: ; preds = %57
%60 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %63
61: ; preds = %57
%62 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.11, i64 0, i64 0))
br label %63
63: ; preds = %61, %59
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %47, %27
%66 = call i32 @close(i32 %9)
ret i32 0
}