#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;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int /*<<< orphan*/ unlink(char *);
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 (bind(s, (struct sockaddr *)&su, sizeof(su)) < 0) {
    perror("bind");
    return 1;
  }
  char buf[1024];
  for (;;) {
    int n = read(s, buf, sizeof(buf));
    if (n <= 0)
      break;
    printf("n=%d\n", n);
    write(1, buf, n);
    if (!strncmp(buf, "bye\n", 4))
      break;
  }
  close(s);
  unlink("mysock");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe1c(%rip),%rsi
call   1050 <strcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    121c <main+0x7c>
lea    0xdfc(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b5 <main+0x115>
jmp    1221 <main+0x81>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jg     124d <main+0xad>
jmp    12a1 <main+0x101>
mov    -0x414(%rbp),%esi
lea    0xdb2(%rip),%rdi
call   1070 <printf@plt>
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
mov    $0x1,%edi
call   1060 <write@plt>
lea    -0x410(%rbp),%rdi
lea    0xd8e(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    129c <main+0xfc>
jmp    12a1 <main+0x101>
jmp    1221 <main+0x81>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
lea    0xd50(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    0x2e32(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r12d
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0xe28(%rip),%rsi
call   1050 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
mov    $0x8,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     1279 <main+0xd9>
lea    0x10(%rsp),%rbx
lea    0xe08(%rip),%r14
lea    0xe07(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
jle    1253 <main+0xb3>
mov    %eax,%ebp
mov    %r14,%rdi
mov    %eax,%esi
call   1070 <printf@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1060 <write@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
jne    1210 <main+0x70>
mov    %r12d,%edi
call   1080 <close@plt>
lea    0xd9e(%rip),%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd87(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%eax
jmp    1269 <main+0xc9>

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 = [5 x i8] c"bind\00"
@.str.2 = [6 x i8] c"n=%d\0A\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 @bind(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 ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %43
20: ; preds = %0
br label %21
21: ; preds = %38, %20
%22 = load i32, i32* %2
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 1024)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
br label %39
28: ; preds = %21
%29 = load i32, i32* %5
%30 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%32 = load i32, i32* %5
%33 = call i32 @write(i32 1, i8* %31, i32 %32)
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%35 = call i32 @strncmp(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %28
br label %39
38: ; preds = %28
br label %21
39: ; preds = %37, %27
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
%42 = call i32 @unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %39, %18
%44 = load i32, i32* %1
ret i32 %44
}

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 = [5 x i8] c"bind\00"
@.str.2 = [6 x i8] c"n=%d\0A\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 @bind(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 ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
15: ; preds = %0
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %17
17: ; preds = %20, %15
%18 = call i32 @read(i32 %5, i8* %16, i32 1024)
%19 = icmp slt i32 %18, 1
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%22 = call i32 @write(i32 1, i8* %16, i32 %18)
%23 = call i32 @strncmp(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 4)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %17
25: ; preds = %20, %17
%26 = call i32 @close(i32 %5)
%27 = call i32 @unlink(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %25, %13
%29 = phi i32 [ 1, %13 ], [ 0, %25 ]
ret i32 %29
}