#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, size_t *);
scalar_t__ bind(int, struct sockaddr *, size_t);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ copyData(int, int);
int /*<<< orphan*/ die(char *);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int main(void) {
  struct sockaddr_un address;
  int sock, conn;
  size_t addrLength;
  if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
    die("socket");
  unlink("./sample-socket");
  address.sun_family = AF_UNIX;
  strcpy(address.sun_path, "./sample-socket");
  addrLength = sizeof(address.sun_family) + strlen(address.sun_path);
  if (bind(sock, (struct sockaddr *)&address, addrLength))
    die("bind");
  if (listen(sock, 5))
    die("listen");
  while ((conn = accept(sock, (struct sockaddr *)&address, &addrLength)) >= 0) {
    printf("---- getting data\n");
    copyData(conn, 1);
    printf("---- done\n");
    close(conn);
  }
  if (conn < 0)
    die("accept");
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11f0 <main+0x40>
lea    0xe1e(%rip),%rdi
call   10a0 <die@plt>
lea    0xe09(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xdee(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%edi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     124f <main+0x9f>
lea    0xdc6(%rip),%rdi
call   10a0 <die@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
je     1272 <main+0xc2>
lea    0xda8(%rip),%rdi
call   10a0 <die@plt>
jmp    1277 <main+0xc7>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jl     12c5 <main+0x115>
lea    0xd82(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   10c0 <copyData@plt>
lea    0xd7c(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    1277 <main+0xc7>
cmpl   $0x0,-0x18(%rbp)
jge    12db <main+0x12b>
lea    0xd64(%rip),%rdi
call   10a0 <die@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%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    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    11ea <main+0x3a>
lea    0xe24(%rip),%rdi
call   10a0 <die@plt>
lea    0xe0f(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %rbx,%rsi
call   1040 <strcpy@plt>
call   1060 <strlen@plt>
movslq %eax,%rdx
add    $0x4,%rdx
mov    %rdx,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
call   1090 <bind@plt>
test   %rax,%rax
je     123d <main+0x8d>
lea    0xdd8(%rip),%rdi
call   10a0 <die@plt>
mov    %r12d,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %rax,%rax
je     125b <main+0xab>
lea    0xdbf(%rip),%rdi
call   10a0 <die@plt>
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %r12d,%edi
call   10b0 <accept@plt>
test   %eax,%eax
js     12c7 <main+0x117>
mov    %eax,%ebp
lea    0xda9(%rip),%r14
lea    0xdb4(%rip),%r15
lea    0x8(%rsp),%r13
lea    0x10(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <puts@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   10c0 <copyData@plt>
mov    %r15,%rdi
call   1050 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1290 <main+0xe0>
lea    0xd4e(%rip),%rdi
call   10a0 <die@plt>
mov    %r12d,%edi
call   1070 <close@plt>
xor    %eax,%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 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [16 x i8] c"./sample-socket\00"
@AF_UNIX = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [19 x i8] c"---- getting data\0A\00"
@.str.5 = [11 x i8] c"---- done\0A\00"
@.str.6 = [7 x i8] c"accept\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i32 0, i32* %1
%6 = load i32, i32* @PF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = call i32 @unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @strcpy(i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %2, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @strlen(i32 %20)
%22 = sext i32 %21 to i64
%23 = add i64 4, %22
store i64 %23, i64* %5
%24 = load i32, i32* %3
%25 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%26 = load i64, i64* %5
%27 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i64 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %12
%30 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %12
%32 = load i32, i32* %3
%33 = call i64 @listen(i32 %32, i32 5)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
br label %38
38: ; preds = %43, %37
%39 = load i32, i32* %3
%40 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%41 = call i32 @accept(i32 %39, %struct.sockaddr* %40, i64* %5)
store i32 %41, i32* %4
%42 = icmp sge i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %38
%44 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%45 = load i32, i32* %4
%46 = call i32 @copyData(i32 %45, i32 1)
%47 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%48 = load i32, i32* %4
%49 = call i32 @close(i32 %48)
br label %38
50: ; preds = %38
%51 = load i32, i32* %4
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
%56 = load i32, i32* %3
%57 = call i32 @close(i32 %56)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [16 x i8] c"./sample-socket\00"
@AF_UNIX = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.6 = [7 x i8] c"accept\00"
@str = [18 x i8] c"---- getting data\00"
@str.7 = [10 x i8] c"---- done\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_un
%2 = alloca i64
%3 = bitcast %struct.sockaddr_un* %1 to i8*
%4 = bitcast i64* %2 to i8*
%5 = load i32, i32* @PF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = tail call i32 @unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 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 ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @strlen(i32 undef)
%17 = sext i32 %16 to i64
%18 = add nsw i64 %17, 4
store i64 %18, i64* %2
%19 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%20 = call i64 @bind(i32 %7, %struct.sockaddr* %19, i64 %18)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %11
%23 = call i32 @die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %11
%25 = call i64 @listen(i32 %7, i32 5)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = call i32 @accept(i32 %7, %struct.sockaddr* %19, i64* %2)
%31 = icmp sgt i32 %30, -1
br i1 %31, label %32, label %40
32: ; preds = %29, %32
%33 = phi i32 [ %38, %32 ], [ %30, %29 ]
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%35 = call i32 @copyData(i32 %33, i32 1)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.7, i64 0, i64 0))
%37 = call i32 @close(i32 %33)
%38 = call i32 @accept(i32 %7, %struct.sockaddr* %19, i64* %2)
%39 = icmp sgt i32 %38, -1
br i1 %39, label %32, label %40
40: ; preds = %32, %29
%41 = call i32 @die(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%42 = call i32 @close(i32 %7)
ret i32 0
}