#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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ 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, done;
  struct sockaddr_un server;
  char request[10000];
  char response[10000];
  if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    exit(1);
  }
  printf("Trying to connect");
  server.sun_family = AF_UNIX;
  strcpy(server.sun_path, "/tmp/circuits");
  len = strlen(server.sun_path) + sizeof(server.sun_family);
  if (connect(s, (struct sockaddr *)&server, len) == -1) {
    perror("connect");
    exit(1);
  }
  printf("\nconnected\n");
  while (1) {
    done = 0;
    printf(">");
    fgets(request, 1000, stdin);
    if (send(s, request, strlen(request), 0) == -1) {
      perror("send");
      exit(1);
    }
    if ((t = recv(s, response, 100, 0)) > 0) {
      response[t] = '\0';
      printf("echo> %s\n", response);
      done = 1;
    } else {
      if (t < 0)
        perror("recv");
      else
        printf("server closed connection");
      exit(1);
    }
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4e60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11ed <main+0x4d>
lea    0xe22(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe07(%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   10b0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1257 <main+0xb7>
lea    0xdc9(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x14(%rbp)
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x2740(%rbp),%rdi
mov    0x2d58(%rip),%rax
mov    (%rax),%edx
mov    $0x3e8,%esi
call   1080 <fgets@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4e5c(%rbp)
lea    -0x2740(%rbp),%rax
mov    %rax,-0x4e58(%rbp)
lea    -0x2740(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x4e5c(%rbp),%edi
mov    -0x4e58(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
jne    12ec <main+0x14c>
lea    0xd58(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x4e50(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    133b <main+0x19b>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x4e50(%rbp,%rax,1)
lea    -0x4e50(%rbp),%rsi
lea    0xd12(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x14(%rbp)
jmp    136e <main+0x1ce>
cmpl   $0x0,-0xc(%rbp)
jge    1356 <main+0x1b6>
lea    0xcf8(%rip),%rdi
call   1090 <perror@plt>
jmp    1364 <main+0x1c4>
lea    0xcec(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1265 <main+0xc5>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x4e30,%rsp
mov    0x2e12(%rip),%rbx
mov    (%rbx),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    11f8 <main+0x38>
lea    0xe0d(%rip),%rdi
jmp    1321 <main+0x161>
mov    %eax,%ebp
lea    0xe06(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0xe04(%rip),%rsi
call   1050 <strcpy@plt>
call   1070 <strlen@plt>
lea    0x4(%rax),%edx
mov    %rsp,%rsi
mov    %ebp,%edi
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    123d <main+0x7d>
lea    0xdd9(%rip),%rdi
jmp    1321 <main+0x161>
lea    0xe10(%rip),%rdi
call   1060 <puts@plt>
mov    $0x3e,%edi
call   1040 <putchar@plt>
mov    0x2d86(%rip),%r12
mov    (%r12),%edx
lea    0x2720(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x3e8,%esi
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0xffffffff,%eax
je     130f <main+0x14f>
lea    0x10(%rsp),%r14
lea    0xd8e(%rip),%r15
lea    0x2720(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1318 <main+0x158>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0x3e,%edi
call   1040 <putchar@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x3e8,%esi
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1080 <send@plt>
cmp    $0xffffffff,%eax
jne    12b0 <main+0xf0>
lea    0xd11(%rip),%rdi
jmp    1321 <main+0x161>
jns    1330 <main+0x170>
lea    0xd15(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd04(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
mov    $0x1,%edi
call   10c0 <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 = [18 x i8] c"Trying to connect\00"
@.str.2 = [14 x i8] c"/tmp/circuits\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [12 x i8] c"\0Aconnected\0A\00"
@.str.5 = [2 x i8] c">\00"
@stdin = global i32 0
@.str.6 = [5 x i8] c"send\00"
@.str.7 = [10 x i8] c"echo> %s\0A\00"
@.str.8 = [5 x i8] c"recv\00"
@.str.9 = [25 x i8] c"server closed connection\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 [10000 x i8]
%8 = alloca [10000 x i8]
store i32 0, i32* %1
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%23 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i32 @strlen(i8* %24)
%26 = sext i32 %25 to i64
%27 = add i64 %26, 4
%28 = trunc i64 %27 to i32
store i32 %28, i32* %4
%29 = load i32, i32* %2
%30 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%31 = load i32, i32* %4
%32 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %16
%35 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %16
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %73
store i32 0, i32* %5
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%41 = getelementptr inbounds [10000 x i8], [10000 x i8]* %7, i64 0, i64 0
%42 = load i32, i32* @stdin
%43 = call i32 @fgets(i8* %41, i32 1000, i32 %42)
%44 = load i32, i32* %2
%45 = getelementptr inbounds [10000 x i8], [10000 x i8]* %7, i64 0, i64 0
%46 = getelementptr inbounds [10000 x i8], [10000 x i8]* %7, i64 0, i64 0
%47 = call i32 @strlen(i8* %46)
%48 = call i32 @send(i32 %44, i8* %45, i32 %47, i32 0)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %53
50: ; preds = %39
%51 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %39
%54 = load i32, i32* %2
%55 = getelementptr inbounds [10000 x i8], [10000 x i8]* %8, i64 0, i64 0
%56 = call i32 @recv(i32 %54, i8* %55, i32 100, i32 0)
store i32 %56, i32* %3
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %53
%59 = load i32, i32* %3
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [10000 x i8], [10000 x i8]* %8, i64 0, i64 %60
store i8 0, i8* %61
%62 = getelementptr inbounds [10000 x i8], [10000 x i8]* %8, i64 0, i64 0
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8* %62)
store i32 1, i32* %5
br label %73
64: ; preds = %53
%65 = load i32, i32* %3
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %64
%68 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %71
69: ; preds = %64
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
br label %71
71: ; preds = %69, %67
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %58
br label %39
}

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.1 = [18 x i8] c"Trying to connect\00"
@.str.2 = [14 x i8] c"/tmp/circuits\00"
@.str.3 = [8 x i8] c"connect\00"
@stdin = global i32 0
@.str.6 = [5 x i8] c"send\00"
@.str.7 = [10 x i8] c"echo> %s\0A\00"
@.str.8 = [5 x i8] c"recv\00"
@.str.9 = [25 x i8] c"server closed connection\00"
@str = [11 x i8] c"\0Aconnected\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [10000 x i8]
%3 = alloca [10000 x i8]
%4 = bitcast %struct.sockaddr_un* %1 to i8*
%5 = getelementptr inbounds [10000 x i8], [10000 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [10000 x i8], [10000 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 slt i32 %9, 0
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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @strcpy(i8* undef, i8* getelementptr inbounds ([14 x i8], [14 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* %1 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, i64 0, i64 0))
%29 = call i32 @putchar(i32 62)
%30 = load i32, i32* @stdin
%31 = call i32 @fgets(i8* %5, i32 1000, i32 %30)
%32 = call i32 @strlen(i8* %5)
%33 = call i32 @send(i32 %9, i8* %5, i32 %32, i32 0)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %41, %27
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %27, %41
%39 = call i32 @recv(i32 %9, i8* %6, i32 100, i32 0)
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %51
41: ; preds = %38
%42 = zext i32 %39 to i64
%43 = getelementptr inbounds [10000 x i8], [10000 x i8]* %3, i64 0, i64 %42
store i8 0, i8* %43
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i8* %6)
%45 = call i32 @putchar(i32 62)
%46 = load i32, i32* @stdin
%47 = call i32 @fgets(i8* %5, i32 1000, i32 %46)
%48 = call i32 @strlen(i8* %5)
%49 = call i32 @send(i32 %9, i8* %5, i32 %48, i32 0)
%50 = icmp eq i32 %49, -1
br i1 %50, label %35, label %38
51: ; preds = %38
%52 = icmp slt i32 %39, 0
br i1 %52, label %53, label %55
53: ; preds = %51
%54 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
br label %57
55: ; preds = %51
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.9, i64 0, i64 0))
br label %57
57: ; preds = %55, %53
%58 = call i32 @exit(i32 1)
unreachable
}