#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;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int getpid();
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main() {
  int fd;
  int r;
  char buf[300];
  printf("pid : %d,fd : %d\n", getpid(), fd);
  fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  if (fd == -1) {
    printf("socket create error:%m\n");
    exit(-1);
  }
  printf("socket create success....\n");
  struct sockaddr_un addr = {0};
  addr.sun_family = AF_UNIX;
  memcpy(addr.sun_path, "my.sock", strlen("my.sock"));
  r = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (r == -1) {
    printf("socket bind error:%m\n");
    exit(-1);
  }
  printf("socket bind success....\n");
  while (1) {
    bzero(buf, sizeof(buf));
    r = read(fd, buf, sizeof(buf));
    buf[r] = 0;
    printf("get data : %s\n", buf);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    120a <main+0x6a>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x150(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14c(%rbp)
mov    -0x150(%rbp),%eax
mov    %eax,-0x154(%rbp)
lea    0xdf8(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x154(%rbp),%edi
mov    %eax,%edx
lea    0xde4(%rip),%rsi
call   1080 <memcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x150(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    129f <main+0xff>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x140(%rbp),%rdi
mov    $0x12c,%esi
call   10a0 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x12c,%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
movb   $0x0,-0x140(%rbp,%rax,1)
lea    -0x140(%rbp),%rsi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12ad <main+0x10d>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x140,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe47(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e1f(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1282 <main+0xe2>
mov    %eax,%ebp
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
movq   $0x0,(%rsp)
movl   $0x0,0x8(%rsp)
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
lea    0xe1b(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     128b <main+0xeb>
lea    0xe33(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbx
lea    0xdf9(%rip),%r14
nop
mov    %rbx,%rdi
mov    $0x12c,%esi
call   10a0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x12c,%edx
call   1070 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1250 <main+0xb0>
lea    0xd89(%rip),%rdi
jmp    1292 <main+0xf2>
lea    0xda0(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"pid : %d,fd : %d\0A\00"
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [24 x i8] c"socket create error:%m\0A\00"
@.str.2 = [27 x i8] c"socket create success....\0A\00"
@.str.3 = [8 x i8] c"my.sock\00"
@.str.4 = [22 x i8] c"socket bind error:%m\0A\00"
@.str.5 = [25 x i8] c"socket bind success....\0A\00"
@.str.6 = [15 x i8] c"get data : %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [300 x i8]
%5 = alloca %struct.sockaddr_un
store i32 0, i32* %1
%6 = call i32 (...) @getpid()
%7 = load i32, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%19 = bitcast %struct.sockaddr_un* %5 to i8*
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %5, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%25 = call i32 @memcpy(i32 %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %2
%27 = bitcast %struct.sockaddr_un* %5 to %struct.sockaddr*
%28 = call i32 @bind(i32 %26, %struct.sockaddr* %27, i32 12)
store i32 %28, i32* %3
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %17
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %17
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %36
36: ; preds = %34, %36
%37 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%38 = call i32 @bzero(i8* %37, i32 300)
%39 = load i32, i32* %2
%40 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 300)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 %43
store i8 0, i8* %44
%45 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* %45)
br label %36
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"pid : %d,fd : %d\0A\00"
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [24 x i8] c"socket create error:%m\0A\00"
@.str.3 = [8 x i8] c"my.sock\00"
@.str.4 = [22 x i8] c"socket bind error:%m\0A\00"
@.str.6 = [15 x i8] c"get data : %s\0A\00"
@str = [26 x i8] c"socket create success....\00"
@str.7 = [24 x i8] c"socket bind success....\00"
define i32 @main() {
%1 = alloca [300 x i8]
%2 = alloca %struct.sockaddr_un
%3 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 0
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4, i32 undef)
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%15 = bitcast %struct.sockaddr_un* %2 to i8*
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i64 0, i32 1
store i32 %16, i32* %17
%18 = tail call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @memcpy(i32 0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%21 = call i32 @bind(i32 %8, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %13
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0))
br label %28
28: ; preds = %28, %26
%29 = call i32 @bzero(i8* %3, i32 300)
%30 = call i32 @read(i32 %8, i8* %3, i32 300)
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 %31
store i8 0, i8* %32
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* %3)
br label %28
}