#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*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(char *);
void consumer() {
  struct sockaddr_un peer_addr;
  int len;
  int size;
  char buf[200];
  int sock = socket(AF_UNIX, SOCK_STREAM, 0);
  if (sock < 0) {
    printf("consumer: error opening socket\n");
    exit(1);
  }
  printf("consumer connecting to producer\n");
  peer_addr.sun_family = AF_UNIX;
  strcpy(peer_addr.sun_path, "/Users/bikwong/Documents/tmp/lab10");
  len = strlen("/Users/bikwong/Documents/tmp/lab10") +
        sizeof(peer_addr.sun_family);
  if (connect(sock, (struct sockaddr *)&peer_addr, len) < 0) {
    printf("consumer: error in connect\n");
    exit(1);
  }
  printf("consumer connected to producer\n");
  size = recv(sock, &buf[0], 200, 0);
  if (size < 0) {
    printf("consumer: error in recv\n");
    exit(1);
  }
  printf("consumer received message: %s\n", &buf[0]);
}


x86 assembly (clang, -O0):
<consumer>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0xe4(%rbp)
cmpl   $0x0,-0xe4(%rbp)
jge    11bf <consumer+0x4f>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe5e(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xe52(%rip),%rdi
call   1050 <strlen@plt>
cltq
add    $0x4,%rax
mov    %eax,-0xc(%rbp)
mov    -0xe4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1231 <consumer+0xc1>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0xe4(%rbp),%edi
lea    -0xe0(%rbp),%rsi
mov    $0xc8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    127d <consumer+0x10d>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0xe0(%rbp),%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consumer>:
push   %r14
push   %rbx
sub    $0xd8,%rsp
mov    0x2e57(%rip),%r14
mov    (%r14),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     122d <consumer+0xad>
mov    %eax,%ebx
lea    0xe8d(%rip),%rdi
call   1050 <puts@plt>
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
lea    0xe38(%rip),%r14
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    0x4(%rax),%edx
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1090 <connect@plt>
test   %rax,%rax
js     1236 <consumer+0xb6>
lea    0xe6f(%rip),%rdi
call   1050 <puts@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc8,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
js     123f <consumer+0xbf>
lea    0xe0d(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0xd8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe80(%rip),%rdi
jmp    1246 <consumer+0xc6>
lea    0xe5c(%rip),%rdi
jmp    1246 <consumer+0xc6>
lea    0xe3b(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [32 x i8] c"consumer: error opening socket\0A\00"
@.str.1 = [33 x i8] c"consumer connecting to producer\0A\00"
@.str.2 = [35 x i8] c"/Users/bikwong/Documents/tmp/lab10\00"
@.str.3 = [28 x i8] c"consumer: error in connect\0A\00"
@.str.4 = [32 x i8] c"consumer connected to producer\0A\00"
@.str.5 = [25 x i8] c"consumer: error in recv\0A\00"
@.str.6 = [31 x i8] c"consumer received message: %s\0A\00"
define void @consumer() {
%1 = alloca %struct.sockaddr_un
%2 = alloca i32
%3 = alloca i32
%4 = alloca [200 x i8]
%5 = alloca i32
%6 = load i32, i32* @AF_UNIX
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @AF_UNIX
%17 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i32 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @strcpy(i32 %19, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @strlen(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%22 = sext i32 %21 to i64
%23 = add i64 %22, 4
%24 = trunc i64 %23 to i32
store i32 %24, i32* %2
%25 = load i32, i32* %5
%26 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%27 = load i32, i32* %2
%28 = call i64 @connect(i32 %25, %struct.sockaddr* %26, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %14
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %14
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%35 = load i32, i32* %5
%36 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%37 = call i32 @recv(i32 %35, i8* %36, i32 200, i32 0)
store i32 %37, i32* %3
%38 = load i32, i32* %3
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %33
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %33
%44 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %44)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [35 x i8] c"/Users/bikwong/Documents/tmp/lab10\00"
@.str.6 = [31 x i8] c"consumer received message: %s\0A\00"
@str = [32 x i8] c"consumer connecting to producer\00"
@str.7 = [31 x i8] c"consumer connected to producer\00"
@str.8 = [24 x i8] c"consumer: error in recv\00"
@str.9 = [27 x i8] c"consumer: error in connect\00"
@str.10 = [31 x i8] c"consumer: error opening socket\00"
define void @consumer() {
%1 = alloca %struct.sockaddr_un
%2 = alloca [200 x i8]
%3 = bitcast %struct.sockaddr_un* %1 to i8*
%4 = getelementptr inbounds [200 x i8], [200 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @AF_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 %12
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.10, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%14 = load i32, i32* @AF_UNIX
%15 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %1, i64 0, i32 1
store i32 %14, i32* %15
%16 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%17 = tail call i32 @strlen(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%18 = add i32 %17, 4
%19 = bitcast %struct.sockaddr_un* %1 to %struct.sockaddr*
%20 = call i64 @connect(i32 %7, %struct.sockaddr* %19, i32 %18)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.9, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %12
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.7, i64 0, i64 0))
%27 = call i32 @recv(i32 %7, i8* %4, i32 200, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.8, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %4)
ret void
}