#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 socklen_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int nargs, char *argv[]) {
  if (nargs != 2) {
    printf("Give the sun path number\n");
    return -1;
  }
  int sockfd;
  if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
    error("creating socket");
  }
  struct sockaddr_un serv_addr;
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sun_family = AF_UNIX;
  strcpy(serv_addr.sun_path, argv[1]);
  int servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, servlen) < 0) {
    error("binding socket");
  }
  listen(sockfd, 5);
  struct sockaddr_un cli_addr;
  socklen_t clilen = sizeof(cli_addr);
  int newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
  if (newsockfd < 0) {
    error("accepting");
  }
  char buf[80];
  int n = read(newsockfd, buf, 80);
  printf("A connection has been established\n");
  write(1, buf, n);
  write(newsockfd, "I got your message\n", 19);
  close(newsockfd);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11fb <main+0x3b>
lea    0xe16(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1331 <main+0x171>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    122c <main+0x6c>
lea    0xdf3(%rip),%rdi
call   10b0 <error@plt>
lea    -0x20(%rbp),%rdi
mov    $0x8,%esi
call   10c0 <bzero@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%edi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    128c <main+0xcc>
lea    0xda3(%rip),%rdi
call   10b0 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0x8,-0x34(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12c9 <main+0x109>
lea    0xd75(%rip),%rdi
call   10b0 <error@plt>
mov    -0x38(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x50,%edx
call   1080 <read@plt>
mov    %eax,-0x94(%rbp)
lea    0xd59(%rip),%rdi
call   1060 <printf@plt>
lea    -0x90(%rbp),%rsi
mov    -0x94(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    -0x38(%rbp),%edi
lea    0xd56(%rip),%rsi
mov    $0x13,%edx
call   1040 <write@plt>
mov    -0x38(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
cmp    $0x2,%edi
jne    12df <main+0x11f>
mov    %rsi,%rbp
mov    0x2e0b(%rip),%rbx
mov    (%rbx),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    1202 <main+0x42>
lea    0xe03(%rip),%rdi
call   10b0 <error@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x8,%esi
call   10c0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x14(%rsp)
mov    0x10(%rsp),%ebx
mov    0x8(%rbp),%rsi
mov    %ebx,%edi
call   1030 <strcpy@plt>
mov    %ebx,%edi
call   1060 <strlen@plt>
lea    0x4(%rax),%edx
mov    %r15d,%edi
mov    %r14,%rsi
call   10a0 <bind@plt>
test   %rax,%rax
jns    124f <main+0x8f>
lea    0xdc6(%rip),%rdi
call   10b0 <error@plt>
mov    %r15d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0x8,0xc(%rsp)
lea    0x18(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r15d,%edi
call   10d0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1288 <main+0xc8>
lea    0xd9c(%rip),%rdi
call   10b0 <error@plt>
lea    0x20(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x50,%edx
call   1080 <read@plt>
mov    %eax,%ebx
lea    0xd98(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <write@plt>
lea    0xd69(%rip),%rsi
mov    %ebp,%edi
mov    $0x13,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r15d,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    12f0 <main+0x130>
lea    0xd79(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"Give the sun path number\0A\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"creating socket\00"
@.str.2 = [15 x i8] c"binding socket\00"
@.str.3 = [10 x i8] c"accepting\00"
@.str.4 = [35 x i8] c"A connection has been established\0A\00"
@.str.5 = [20 x i8] c"I got your message\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_un
%8 = alloca i32
%9 = alloca %struct.sockaddr_un
%10 = alloca i32
%11 = alloca i32
%12 = alloca [80 x i8]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %73
18: ; preds = %2
%19 = load i32, i32* @AF_UNIX
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = bitcast %struct.sockaddr_un* %7 to i8*
%27 = call i32 @bzero(i8* %26, i32 8)
%28 = load i32, i32* @AF_UNIX
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @strcpy(i32 %31, i8* %34)
%36 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @strlen(i32 %37)
%39 = sext i32 %38 to i64
%40 = add i64 %39, 4
%41 = trunc i64 %40 to i32
store i32 %41, i32* %8
%42 = load i32, i32* %6
%43 = bitcast %struct.sockaddr_un* %7 to %struct.sockaddr*
%44 = load i32, i32* %8
%45 = call i64 @bind(i32 %42, %struct.sockaddr* %43, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %25
%48 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %47, %25
%50 = load i32, i32* %6
%51 = call i32 @listen(i32 %50, i32 5)
store i32 8, i32* %10
%52 = load i32, i32* %6
%53 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%54 = call i32 @accept(i32 %52, %struct.sockaddr* %53, i32* %10)
store i32 %54, i32* %11
%55 = load i32, i32* %11
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %49
%58 = call i32 @error(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %57, %49
%60 = load i32, i32* %11
%61 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 0
%62 = call i32 @read(i32 %60, i8* %61, i32 80)
store i32 %62, i32* %13
%63 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0))
%64 = getelementptr inbounds [80 x i8], [80 x i8]* %12, i64 0, i64 0
%65 = load i32, i32* %13
%66 = call i32 @write(i32 1, i8* %64, i32 %65)
%67 = load i32, i32* %11
%68 = call i32 @write(i32 %67, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 19)
%69 = load i32, i32* %11
%70 = call i32 @close(i32 %69)
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
store i32 0, i32* %3
br label %73
73: ; preds = %59, %16
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"creating socket\00"
@.str.2 = [15 x i8] c"binding socket\00"
@.str.3 = [10 x i8] c"accepting\00"
@.str.5 = [20 x i8] c"I got your message\0A\00"
@str = [34 x i8] c"A connection has been established\00"
@str.6 = [25 x i8] c"Give the sun path number\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_un
%4 = alloca %struct.sockaddr_un
%5 = alloca i32
%6 = alloca [80 x i8]
%7 = icmp eq i32 %0, 2
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
br label %51
10: ; preds = %2
%11 = load i32, i32* @AF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = bitcast %struct.sockaddr_un* %3 to i8*
%19 = call i32 @bzero(i8* %18, i32 8)
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = tail call i32 @strcpy(i32 %23, i8* %25)
%27 = tail call i32 @strlen(i32 %23)
%28 = add i32 %27, 4
%29 = bitcast %struct.sockaddr_un* %3 to %struct.sockaddr*
%30 = call i64 @bind(i32 %13, %struct.sockaddr* %29, i32 %28)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = call i32 @error(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %17
%35 = call i32 @listen(i32 %13, i32 5)
%36 = bitcast %struct.sockaddr_un* %4 to i8*
%37 = bitcast i32* %5 to i8*
store i32 8, i32* %5
%38 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%39 = call i32 @accept(i32 %13, %struct.sockaddr* %38, i32* %5)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 @error(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %34
%44 = getelementptr inbounds [80 x i8], [80 x i8]* %6, i64 0, i64 0
%45 = call i32 @read(i32 %39, i8* %44, i32 80)
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%47 = call i32 @write(i32 1, i8* %44, i32 %45)
%48 = call i32 @write(i32 %39, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 19)
%49 = call i32 @close(i32 %39)
%50 = call i32 @close(i32 %13)
br label %51
51: ; preds = %43, %8
%52 = phi i32 [ -1, %8 ], [ 0, %43 ]
ret i32 %52
}