#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);
char getchar();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int sockfd;
  struct sockaddr_un address;
  while (1) {
    printf("Input a char: ");
    char ch;
    do {
      ch = getchar();
    } while (ch == '\n');
    sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
    if (sockfd == -1) {
      perror("socket failed");
      return -1;
    }
    address.sun_family = AF_UNIX;
    strcpy(address.sun_path, "server_socket");
    int res = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
    if (res == -1) {
      perror("Oops client1");
      return -1;
    }
    write(sockfd, &ch, 1);
    read(sockfd, &ch, 1);
    printf("char from server - %c\n", ch);
    close(sockfd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0xa,%eax
je     11ad <main+0x1d>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1202 <main+0x72>
lea    0xe1e(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <main+0x104>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe05(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1253 <main+0xc3>
lea    0xde9(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <main+0x104>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl -0x11(%rbp),%esi
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    119f <main+0xf>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e43(%rip),%r15
lea    0xe71(%rip),%r12
lea    0x10(%rsp),%r13
lea    0xf(%rsp),%rbx
lea    0xe7b(%rip),%rbp
nopl   (%rax)
lea    0xe39(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xchg   %ax,%ax
xor    %eax,%eax
call   1080 <getchar@plt>
cmp    $0xa,%al
je     11d0 <main+0x40>
mov    %al,0xf(%rsp)
mov    (%r15),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     125e <main+0xce>
mov    %eax,%r14d
mov    (%r15),%eax
mov    %eax,0x14(%rsp)
mov    0x10(%rsp),%edi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    $0x8,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     1267 <main+0xd7>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl 0xf(%rsp),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%edi
call   1060 <close@plt>
jmp    11c0 <main+0x30>
lea    0xdaa(%rip),%rdi
jmp    126e <main+0xde>
lea    0xdbd(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"Input a char: \00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [14 x i8] c"socket failed\00"
@.str.2 = [14 x i8] c"server_socket\00"
@.str.3 = [13 x i8] c"Oops client1\00"
@.str.4 = [23 x i8] c"char from server - %c\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca i8
%5 = alloca i32
store i32 0, i32* %1
br label %6
6: ; preds = %0, %35
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %10, %6
%9 = call signext i8 (...) @getchar()
store i8 %9, i8* %4
br label %10
10: ; preds = %8
%11 = load i8, i8* %4
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 10
br i1 %13, label %8, label %14
14: ; preds = %10
%15 = load i32, i32* @AF_UNIX
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %45
22: ; preds = %14
%23 = load i32, i32* @AF_UNIX
%24 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @strcpy(i32 %26, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %2
%29 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%30 = call i32 @connect(i32 %28, %struct.sockaddr* %29, i32 8)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %22
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %45
35: ; preds = %22
%36 = load i32, i32* %2
%37 = call i32 @write(i32 %36, i8* %4, i32 1)
%38 = load i32, i32* %2
%39 = call i32 @read(i32 %38, i8* %4, i32 1)
%40 = load i8, i8* %4
%41 = sext i8 %40 to i32
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %41)
%43 = load i32, i32* %2
%44 = call i32 @close(i32 %43)
br label %6
45: ; preds = %33, %20
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"Input a char: \00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [14 x i8] c"socket failed\00"
@.str.2 = [14 x i8] c"server_socket\00"
@.str.3 = [13 x i8] c"Oops client1\00"
@.str.4 = [23 x i8] c"char from server - %c\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca i8
%3 = bitcast %struct.sockaddr_un* %1 to i8*
%4 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
%5 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 0
%6 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
br label %7
7: ; preds = %23, %0
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %9, %7
%10 = call signext i8 (...) @getchar()
%11 = icmp eq i8 %10, 10
br i1 %11, label %9, label %12
12: ; preds = %9
store i8 %10, i8* %2
%13 = load i32, i32* @AF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %30, label %17
17: ; preds = %12
%18 = load i32, i32* @AF_UNIX
store i32 %18, i32* %4
%19 = load i32, i32* %5
%20 = call i32 @strcpy(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @connect(i32 %15, %struct.sockaddr* %6, i32 8)
%22 = icmp eq i32 %21, -1
br i1 %22, label %30, label %23
23: ; preds = %17
%24 = call i32 @write(i32 %15, i8* %2, i32 1)
%25 = call i32 @read(i32 %15, i8* %2, i32 1)
%26 = load i8, i8* %2
%27 = sext i8 %26 to i32
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = call i32 @close(i32 %15)
br label %7
30: ; preds = %17, %12
%31 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), %12 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), %17 ]
%32 = call i32 @perror(i8* %31)
ret i32 -1
}