#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 */
typedef int /*<<< orphan*/ su;
struct sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ perror(char *);
int read(int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int s = socket(AF_LOCAL, SOCK_DGRAM, 0);
  struct sockaddr_un su;
  su.sun_family = AF_LOCAL;
  strcpy(su.sun_path, "mysock");
  if (connect(s, (struct sockaddr *)&su, sizeof(su)) < 0) {
    perror("connect");
    return 1;
  }
  char buf[1024];
  for (;;) {
    write(1, "input:", 6);
    int n = read(0, buf, sizeof(buf));
    write(s, buf, n);
    if (!strncmp(buf, "bye\n", 4))
      break;
  }
  close(s);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe3c(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    11fc <main+0x7c>
lea    0xe1c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1278 <main+0xf8>
jmp    1201 <main+0x81>
mov    $0x1,%edi
lea    0xe02(%rip),%rsi
mov    $0x6,%edx
call   1050 <write@plt>
lea    -0x410(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x414(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
call   1050 <write@plt>
lea    -0x410(%rbp),%rdi
lea    0xdc3(%rip),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    126b <main+0xeb>
jmp    1270 <main+0xf0>
jmp    1201 <main+0x81>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    0x2e54(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0xe4b(%rip),%rsi
call   1040 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1248 <main+0xc8>
lea    0xe38(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xe33(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
mov    %r14,%rsi
mov    $0x6,%edx
call   1050 <write@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11f0 <main+0x70>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    123a <main+0xba>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"mysock\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [7 x i8] c"input:\00"
@.str.3 = [5 x i8] c"bye\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_un
%4 = alloca [1024 x i8]
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @AF_LOCAL
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = load i32, i32* @AF_LOCAL
%10 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @strcpy(i32 %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %2
%15 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%16 = call i64 @connect(i32 %14, %struct.sockaddr* %15, i32 8)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %37
20: ; preds = %0
br label %21
21: ; preds = %33, %20
%22 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%24 = call i32 @read(i32 0, i8* %23, i32 1024)
store i32 %24, i32* %5
%25 = load i32, i32* %2
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%27 = load i32, i32* %5
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%30 = call i32 @strncmp(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%31 = icmp ne i32 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %21
br label %34
33: ; preds = %21
br label %21
34: ; preds = %32
%35 = load i32, i32* %2
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %34, %18
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_LOCAL = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [7 x i8] c"mysock\00"
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [7 x i8] c"input:\00"
@.str.3 = [5 x i8] c"bye\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [1024 x i8]
%3 = load i32, i32* @AF_LOCAL
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = bitcast %struct.sockaddr_un* %1 to i8*
%7 = load i32, i32* @AF_LOCAL
%8 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %7, i32* %8
%9 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%11 = call i64 @connect(i32 %5, %struct.sockaddr* %10, i32 8)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %25
15: ; preds = %0
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %17
17: ; preds = %17, %15
%18 = call i32 @write(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6)
%19 = call i32 @read(i32 0, i8* %16, i32 1024)
%20 = call i32 @write(i32 %5, i8* %16, i32 %19)
%21 = call i32 @strncmp(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %17
23: ; preds = %17
%24 = call i32 @close(i32 %5)
br label %25
25: ; preds = %23, %13
%26 = phi i32 [ 1, %13 ], [ 0, %23 ]
ret i32 %26
}