#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_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serverAddr;
typedef int /*<<< orphan*/ clientAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(unsigned short);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void consumer() {
  struct sockaddr_in clientAddr;
  struct sockaddr_in serverAddr;
  int len;
  int size;
  char buf[200];
  unsigned short portnum = 2345;
  int sock, snew;
  int client_len;
  char c;
  printf("consumer connecting to producer\n");
  memset(&serverAddr, 0x0, sizeof(serverAddr));
  memset(&clientAddr, 0x0, sizeof(clientAddr));
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "socket failed, errno \n");
    exit(1);
  }
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(portnum);
  printf("\nBinding server port %d\n", portnum);
  if (bind(sock, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0) {
    fprintf(stderr, "bind failed, errno \n");
    close(sock);
    exit(1);
  }
  printf("Listening for connect...\n");
  if (listen(sock, 2) != 0) {
    printf("listen failed\n");
    close(sock);
    exit(1);
  }
  printf("Accepting client connection...\n");
  client_len = sizeof(clientAddr);
  snew = accept(sock, (struct sockaddr *)&clientAddr, &client_len);
  if (snew < 0) {
    printf("accept failed\n");
    close(sock);
    exit(1);
  }
  while (1) {
    if (recv(snew, &c, 1, 0) == 0) {
      break;
    }
    putchar(c);
  }
  shutdown(sock, 2);
  shutdown(snew, 2);
  close(sock);
  close(snew);
  printf("\n...goodbye\n");
  printf("consumer received message: %s\n", &buf[0]);
}


x86 assembly (clang, -O0):
<consumer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movw   $0x929,-0xe2(%rbp)
29 09
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0xe8(%rbp)
cmp    $0x0,%eax
jge    1259 <consumer+0x89>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
movzwl -0xe2(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x10(%rbp)
movzwl -0xe2(%rbp),%esi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0xe8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
je     12d1 <consumer+0x101>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xd9a(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0xe8(%rbp),%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0xe8(%rbp),%edi
mov    $0x2,%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
je     131c <consumer+0x14c>
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0xe8(%rbp),%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x8,-0xf0(%rbp)
mov    -0xe8(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0xf0(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0xec(%rbp)
cmpl   $0x0,-0xec(%rbp)
jge    1380 <consumer+0x1b0>
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0xe8(%rbp),%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    1385 <consumer+0x1b5>
mov    -0xec(%rbp),%edi
lea    -0xf1(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jne    13ad <consumer+0x1dd>
jmp    13be <consumer+0x1ee>
movsbl -0xf1(%rbp),%edi
call   1040 <putchar@plt>
jmp    1385 <consumer+0x1b5>
mov    -0xe8(%rbp),%edi
mov    $0x2,%esi
call   1050 <shutdown@plt>
mov    -0xec(%rbp),%edi
mov    $0x2,%esi
call   1050 <shutdown@plt>
mov    -0xe8(%rbp),%edi
call   1090 <close@plt>
mov    -0xec(%rbp),%edi
call   1090 <close@plt>
lea    0xcc3(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0xe0(%rbp),%rsi
lea    0xcbb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consumer>:
push   %rbp
push   %r14
push   %rbx
sub    $0xf0,%rsp
lea    0xe72(%rip),%rdi
call   1050 <puts@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1090 <memset@plt>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1090 <memset@plt>
mov    0x2dc8(%rip),%rbx
mov    (%rbx),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1363 <consumer+0x183>
mov    %eax,%r14d
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    $0x929,%edi
call   1070 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0xdbf(%rip),%rdi
mov    $0x929,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
call   10d0 <bind@plt>
test   %rax,%rax
jne    1382 <consumer+0x1a2>
lea    0xdfe(%rip),%rdi
call   1050 <puts@plt>
mov    %r14d,%edi
mov    $0x2,%esi
call   10c0 <listen@plt>
test   %rax,%rax
jne    1399 <consumer+0x1b9>
lea    0xdf5(%rip),%rdi
call   1050 <puts@plt>
movl   $0x8,0x14(%rsp)
lea    0x18(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %r14d,%edi
call   10e0 <accept@plt>
test   %eax,%eax
js     13a2 <consumer+0x1c2>
mov    %eax,%ebp
lea    0x7(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
je     1310 <consumer+0x130>
lea    0x7(%rsp),%rbx
xchg   %ax,%ax
movsbl 0x7(%rsp),%edi
call   1040 <putchar@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
jne    12f0 <consumer+0x110>
mov    %r14d,%edi
mov    $0x2,%esi
call   1060 <shutdown@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1060 <shutdown@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
lea    0xd7d(%rip),%rdi
call   1050 <puts@plt>
lea    0xcfa(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1080 <printf@plt>
add    $0xf0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2c6e(%rip),%rax
mov    (%rax),%edi
lea    0xc8d(%rip),%rsi
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    0x2c4f(%rip),%rax
mov    (%rax),%edi
lea    0xc9e(%rip),%rsi
call   10b0 <fprintf@plt>
jmp    13ae <consumer+0x1ce>
lea    0xd36(%rip),%rdi
jmp    13a9 <consumer+0x1c9>
lea    0xd1f(%rip),%rdi
call   1050 <puts@plt>
mov    %r14d,%edi
call   10a0 <close@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [33 x i8] c"consumer connecting to producer\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"socket failed, errno \0A\00"
@.str.2 = [25 x i8] c"\0ABinding server port %d\0A\00"
@.str.3 = [21 x i8] c"bind failed, errno \0A\00"
@.str.4 = [26 x i8] c"Listening for connect...\0A\00"
@.str.5 = [15 x i8] c"listen failed\0A\00"
@.str.6 = [32 x i8] c"Accepting client connection...\0A\00"
@.str.7 = [15 x i8] c"accept failed\0A\00"
@.str.8 = [13 x i8] c"\0A...goodbye\0A\00"
@.str.9 = [31 x i8] c"consumer received message: %s\0A\00"
define void @consumer() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca [200 x i8]
%6 = alloca i16
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i16 2345, i16* %6
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 8)
%13 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 8)
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %0
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %0
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %23, i32* %24
%25 = load i16, i16* %6
%26 = call i32 @htons(i16 zeroext %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i16, i16* %6
%29 = zext i16 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %7
%32 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%33 = call i64 @bind(i32 %31, %struct.sockaddr* %32, i32 8)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %22
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* %7
%39 = call i32 @close(i32 %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %22
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* %7
%44 = call i64 @listen(i32 %43, i32 2)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %51
46: ; preds = %41
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %41
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
store i32 8, i32* %9
%53 = load i32, i32* %7
%54 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%55 = call i32 @accept(i32 %53, %struct.sockaddr* %54, i32* %9)
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %63
58: ; preds = %51
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
%60 = load i32, i32* %7
%61 = call i32 @close(i32 %60)
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %51
br label %64
64: ; preds = %63, %69
%65 = load i32, i32* %8
%66 = call i64 @recv(i32 %65, i8* %10, i32 1, i32 0)
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %64
br label %72
69: ; preds = %64
%70 = load i8, i8* %10
%71 = call i32 @putchar(i8 signext %70)
br label %64
72: ; preds = %68
%73 = load i32, i32* %7
%74 = call i32 @shutdown(i32 %73, i32 2)
%75 = load i32, i32* %8
%76 = call i32 @shutdown(i32 %75, i32 2)
%77 = load i32, i32* %7
%78 = call i32 @close(i32 %77)
%79 = load i32, i32* %8
%80 = call i32 @close(i32 %79)
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
%82 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i8* %82)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [23 x i8] c"socket failed, errno \0A\00"
@.str.2 = [25 x i8] c"\0ABinding server port %d\0A\00"
@.str.3 = [21 x i8] c"bind failed, errno \0A\00"
@.str.9 = [31 x i8] c"consumer received message: %s\0A\00"
@str = [32 x i8] c"consumer connecting to producer\00"
@str.10 = [25 x i8] c"Listening for connect...\00"
@str.11 = [31 x i8] c"Accepting client connection...\00"
@str.12 = [12 x i8] c"\0A...goodbye\00"
@str.13 = [14 x i8] c"accept failed\00"
@str.14 = [14 x i8] c"listen failed\00"
define void @consumer() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca [200 x i8]
%4 = alloca i32
%5 = alloca i8
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%9 = bitcast i32* %4 to i8*
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%11 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 8)
%12 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 8)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %0
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %0
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %22, i32* %23
%24 = call i32 @htons(i16 zeroext 2345)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %24, i32* %25
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 2345)
%27 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%28 = call i64 @bind(i32 %15, %struct.sockaddr* %27, i32 8)
%29 = icmp eq i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @close(i32 %15)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %21
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.10, i64 0, i64 0))
%37 = call i64 @listen(i32 %15, i32 2)
%38 = icmp eq i64 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.14, i64 0, i64 0))
%41 = call i32 @close(i32 %15)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %35
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.11, i64 0, i64 0))
store i32 8, i32* %4
%45 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%46 = call i32 @accept(i32 %15, %struct.sockaddr* %45, i32* %4)
%47 = icmp slt i32 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %43
%49 = call i64 @recv(i32 %46, i8* %5, i32 1, i32 0)
%50 = icmp eq i64 %49, 0
br i1 %50, label %60, label %55
51: ; preds = %43
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.13, i64 0, i64 0))
%53 = call i32 @close(i32 %15)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %48, %55
%56 = load i8, i8* %5
%57 = call i32 @putchar(i8 signext %56)
%58 = call i64 @recv(i32 %46, i8* %5, i32 1, i32 0)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %55
60: ; preds = %55, %48
%61 = call i32 @shutdown(i32 %15, i32 2)
%62 = call i32 @shutdown(i32 %46, i32 2)
%63 = call i32 @close(i32 %15)
%64 = call i32 @close(i32 %46)
%65 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.12, i64 0, i64 0))
%66 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.9, i64 0, i64 0), i8* %8)
ret void
}