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

/* 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*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int write(int, char *, int);
int main() {
  int sockfd;
  int len;
  struct sockaddr_un address;
  int result;
  int i, byte;
  char ch_recv, ch_send;
  sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("socket");
    exit(1);
  }
  address.sun_family = AF_UNIX;
  strcpy(address.sun_path, "server_socket");
  len = sizeof(address);
  result = connect(sockfd, (struct sockaddr *)&address, len);
  if (result == -1) {
    printf("ensure the server is up\n");
    perror("connect");
    exit(1);
  }
  for (i = 0, ch_send = 'A'; i < 5; i++, ch_send++) {
    if (write(sockfd, &ch_send, 1) == -1) {
      perror("write");
      exit(1);
    }
    if (read(sockfd, &ch_recv, 1) == -1) {
      perror("read");
      exit(1);
    }
    printf("receive from server is %c\n", ch_recv);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11db <main+0x4b>
lea    0xe3b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
lea    0xe0f(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x8,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   10a0 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    123d <main+0xad>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x20(%rbp)
movb   $0x41,-0x26(%rbp)
cmpl   $0x5,-0x20(%rbp)
jge    12da <main+0x14a>
mov    -0x8(%rbp),%edi
lea    -0x26(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1282 <main+0xf2>
lea    0xdbc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
jne    12b2 <main+0x122>
lea    0xd92(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movsbl -0x25(%rbp),%esi
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x26(%rbp),%al
add    $0x1,%al
mov    %al,-0x26(%rbp)
jmp    1248 <main+0xb8>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e3a(%rip),%r14
mov    (%r14),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1395 <main+0x1f5>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x14(%rsp)
lea    0xe26(%rip),%rsi
call   1030 <strcpy@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
je     139e <main+0x1fe>
movb   $0x41,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     1376 <main+0x1d6>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     137f <main+0x1df>
movsbl 0xf(%rsp),%esi
lea    0xde3(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movb   $0x42,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     1376 <main+0x1d6>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     137f <main+0x1df>
movsbl 0xf(%rsp),%esi
lea    0xd97(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movb   $0x43,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     1376 <main+0x1d6>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     137f <main+0x1df>
movsbl 0xf(%rsp),%esi
lea    0xd4b(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movb   $0x44,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     1376 <main+0x1d6>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     137f <main+0x1df>
movsbl 0xf(%rsp),%esi
lea    0xd07(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movb   $0x45,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     1376 <main+0x1d6>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0xffffffff,%eax
je     137f <main+0x1df>
movsbl 0xf(%rsp),%esi
lea    0xcc3(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xc99(%rip),%rdi
jmp    1386 <main+0x1e6>
lea    0xc96(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xc6b(%rip),%rdi
jmp    1386 <main+0x1e6>
lea    0xc97(%rip),%rdi
call   1040 <puts@plt>
lea    0xc5d(%rip),%rdi
jmp    1386 <main+0x1e6>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [14 x i8] c"server_socket\00"
@.str.2 = [25 x i8] c"ensure the server is up\0A\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [27 x i8] c"receive from server is %c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8
%9 = alloca i8
store i32 0, i32* %1
%10 = load i32, i32* @AF_UNIX
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %0
%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 = %0
%19 = load i32, i32* @AF_UNIX
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 1
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %4, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @strcpy(i32 %22, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 8, i32* %3
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%26 = load i32, i32* %3
%27 = call i32 @connect(i32 %24, %struct.sockaddr* %25, i32 %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %18
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %18
store i32 0, i32* %6
store i8 65, i8* %9
br label %35
35: ; preds = %56, %34
%36 = load i32, i32* %6
%37 = icmp slt i32 %36, 5
br i1 %37, label %38, label %61
38: ; preds = %35
%39 = load i32, i32* %2
%40 = call i32 @write(i32 %39, i8* %9, i32 1)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %38
%46 = load i32, i32* %2
%47 = call i32 @read(i32 %46, i8* %8, i32 1)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %45
%53 = load i8, i8* %8
%54 = sext i8 %53 to i32
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %52
%57 = load i32, i32* %6
%58 = add nsw i32 %57, 1
store i32 %58, i32* %6
%59 = load i8, i8* %9
%60 = add i8 %59, 1
store i8 %60, i8* %9
br label %35
61: ; preds = %35
%62 = load i32, i32* %2
%63 = call i32 @close(i32 %62)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [14 x i8] c"server_socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [6 x i8] c"write\00"
@.str.5 = [5 x i8] c"read\00"
@.str.6 = [27 x i8] c"receive from server is %c\0A\00"
@str = [24 x i8] c"ensure the server is up\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca i8
%3 = alloca i8
%4 = bitcast %struct.sockaddr_un* %1 to i8*
%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 = load i32, i32* @AF_UNIX
%14 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %13, i32* %14
%15 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%16 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%17 = call i32 @connect(i32 %7, %struct.sockaddr* %16, i32 8)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %19
19: ; preds = %12
store i8 65, i8* %3
%20 = call i32 @write(i32 %7, i8* %3, i32 1)
%21 = icmp eq i32 %20, -1
br i1 %21, label %26, label %29
22: ; preds = %12
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %62, %53, %44, %35, %19
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %19
%30 = call i32 @read(i32 %7, i8* %2, i32 1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %68, %59, %50, %41, %29
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
%36 = load i8, i8* %2
%37 = sext i8 %36 to i32
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %37)
store i8 66, i8* %3
%39 = call i32 @write(i32 %7, i8* %3, i32 1)
%40 = icmp eq i32 %39, -1
br i1 %40, label %26, label %41
41: ; preds = %35
%42 = call i32 @read(i32 %7, i8* %2, i32 1)
%43 = icmp eq i32 %42, -1
br i1 %43, label %32, label %44
44: ; preds = %41
%45 = load i8, i8* %2
%46 = sext i8 %45 to i32
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %46)
store i8 67, i8* %3
%48 = call i32 @write(i32 %7, i8* %3, i32 1)
%49 = icmp eq i32 %48, -1
br i1 %49, label %26, label %50
50: ; preds = %44
%51 = call i32 @read(i32 %7, i8* %2, i32 1)
%52 = icmp eq i32 %51, -1
br i1 %52, label %32, label %53
53: ; preds = %50
%54 = load i8, i8* %2
%55 = sext i8 %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %55)
store i8 68, i8* %3
%57 = call i32 @write(i32 %7, i8* %3, i32 1)
%58 = icmp eq i32 %57, -1
br i1 %58, label %26, label %59
59: ; preds = %53
%60 = call i32 @read(i32 %7, i8* %2, i32 1)
%61 = icmp eq i32 %60, -1
br i1 %61, label %32, label %62
62: ; preds = %59
%63 = load i8, i8* %2
%64 = sext i8 %63 to i32
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %64)
store i8 69, i8* %3
%66 = call i32 @write(i32 %7, i8* %3, i32 1)
%67 = icmp eq i32 %66, -1
br i1 %67, label %26, label %68
68: ; preds = %62
%69 = call i32 @read(i32 %7, i8* %2, i32 1)
%70 = icmp eq i32 %69, -1
br i1 %70, label %32, label %71
71: ; preds = %68
%72 = load i8, i8* %2
%73 = sext i8 %72 to i32
%74 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0), i32 %73)
%75 = call i32 @close(i32 %7)
ret i32 0
}