#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 /*<<< orphan*/ socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(char *);
int /*<<< orphan*/ unlink(char *);
void producer() {
  struct sockaddr_un my_addr;
  struct sockaddr_un peer_addr;
  int status;
  int connsock;
  int len;
  socklen_t peeraddrlen;
  char *msg = "this is a message from producer to consumer";
  int sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
    printf("producer: error opening socket\n");
    exit(1);
  }
  my_addr.sun_family = AF_UNIX;
  strcpy(my_addr.sun_path, "/Users/bikwong/Documents/tmp/lab10");
  len =
      strlen("/Users/bikwong/Documents/tmp/lab10") + sizeof(my_addr.sun_family);
  unlink("/Users/bikwong/Documents/tmp/lab10");
  status = bind(sock, (struct sockaddr *)&my_addr, len);
  if (status < 0) {
    printf("producer failed to bind\n");
    exit(1);
  }
  if (listen(sock, 5) < 0) {
    printf("producer failed to listen\n");
    exit(1);
  }
  connsock = accept(sock, (struct sockaddr *)&peer_addr, &peeraddrlen);
  if (connsock < 0) {
    printf("producer failed to accept\n");
    exit(1);
  }
  status = send(connsock, &msg[0], strlen(msg) + 1, 0);
  if (status < 0) {
    printf("producer failed to send\n");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<producer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xe51(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11ef <producer+0x4f>
lea    0xe4c(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe47(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xe3b(%rip),%rdi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0x1c(%rbp)
lea    0xe26(%rip),%rdi
call   1030 <unlink@plt>
mov    -0x2c(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1090 <bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    125d <producer+0xbd>
lea    0xe21(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x2c(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    128a <producer+0xea>
lea    0xe0d(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x2c(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12bd <producer+0x11d>
lea    0xdf5(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    130a <producer+0x16a>
lea    0xdc3(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<producer>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e39(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     125a <producer+0xba>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
lea    0xe52(%rip),%r14
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    0x4(%rax),%ebp
mov    %r14,%rdi
call   1030 <unlink@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
call   1090 <bind@plt>
test   %eax,%eax
js     1263 <producer+0xc3>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     126c <producer+0xcc>
lea    0x18(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <accept@plt>
test   %eax,%eax
js     1275 <producer+0xd5>
mov    %eax,%ebx
lea    0xdca(%rip),%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%edx
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %ecx,%ecx
call   1070 <send@plt>
test   %eax,%eax
js     127e <producer+0xde>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe52(%rip),%rdi
jmp    1285 <producer+0xe5>
lea    0xe31(%rip),%rdi
jmp    1285 <producer+0xe5>
lea    0xe0e(%rip),%rdi
jmp    1285 <producer+0xe5>
lea    0xdeb(%rip),%rdi
jmp    1285 <producer+0xe5>
lea    0xdca(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [44 x i8] c"this is a message from producer to consumer\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [32 x i8] c"producer: error opening socket\0A\00"
@.str.2 = [35 x i8] c"/Users/bikwong/Documents/tmp/lab10\00"
@.str.3 = [25 x i8] c"producer failed to bind\0A\00"
@.str.4 = [27 x i8] c"producer failed to listen\0A\00"
@.str.5 = [27 x i8] c"producer failed to accept\0A\00"
@.str.6 = [25 x i8] c"producer failed to send\0A\00"
define void @producer() {
%1 = alloca %struct.sockaddr_un
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8** %7
%9 = load i32, i32* @AF_UNIX
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @strcpy(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @strlen(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%24 = sext i32 %23 to i64
%25 = add i64 %24, 4
%26 = trunc i64 %25 to i32
store i32 %26, i32* %5
%27 = call i32 @unlink(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* %8
%29 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%30 = load i32, i32* %5
%31 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 %30)
store i32 %31, i32* %3
%32 = load i32, i32* %3
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %17
%35 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %17
%38 = load i32, i32* %8
%39 = call i64 @listen(i32 %38, i32 5)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %37
%45 = load i32, i32* %8
%46 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%47 = call i32 @accept(i32 %45, %struct.sockaddr* %46, i32* %6)
store i32 %47, i32* %4
%48 = load i32, i32* %4
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %44
%54 = load i32, i32* %4
%55 = load i8*, i8** %7
%56 = getelementptr inbounds i8, i8* %55, i64 0
%57 = load i8*, i8** %7
%58 = call i32 @strlen(i8* %57)
%59 = add nsw i32 %58, 1
%60 = call i32 @send(i32 %54, i8* %56, i32 %59, i32 0)
store i32 %60, i32* %3
%61 = load i32, i32* %3
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %53
%64 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0))
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %53
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [44 x i8] c"this is a message from producer to consumer\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [35 x i8] c"/Users/bikwong/Documents/tmp/lab10\00"
@str = [24 x i8] c"producer failed to send\00"
@str.7 = [26 x i8] c"producer failed to accept\00"
@str.8 = [26 x i8] c"producer failed to listen\00"
@str.9 = [24 x i8] c"producer failed to bind\00"
@str.10 = [31 x i8] c"producer: error opening socket\00"
define void @producer() {
%1 = alloca %struct.sockaddr_un
%2 = alloca %struct.sockaddr_un
%3 = alloca i32
%4 = bitcast %struct.sockaddr_un* %1 to i8*
%5 = bitcast %struct.sockaddr_un* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @AF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @AF_UNIX
%16 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @strlen(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%19 = add i32 %18, 4
%20 = tail call i32 @unlink(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%21 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%22 = call i32 @bind(i32 %9, %struct.sockaddr* %21, i32 %19)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.9, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %14
%28 = call i64 @listen(i32 %9, i32 5)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = bitcast %struct.sockaddr_un* %2 to %struct.sockaddr*
%35 = call i32 @accept(i32 %9, %struct.sockaddr* %34, i32* %3)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
%41 = call i32 @strlen(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%42 = add nsw i32 %41, 1
%43 = call i32 @send(i32 %35, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i32 %42, i32 0)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %40
ret void
}