#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ receiver(int, char *);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sender(int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  char message[1024];
  int sock;
  struct sockaddr_in addr;
  char num[2];
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    perror("socket");
    exit(1);
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(3426);
  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("connect");
    exit(2);
  }
  recv(sock, num, 1024, 0);
  printf("your turn is %s\n", num);
  while (1) {
    if (num[0] == '1') {
      sender(sock, message);
      receiver(sock, message);
    } else {
      receiver(sock, message);
      sender(sock, message);
    }
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    11f4 <main+0x54>
lea    0xe1b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    $0xd62,%edi
call   1040 <htons@plt>
mov    %eax,-0x41c(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x420(%rbp)
mov    -0x414(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    125e <main+0xbe>
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x414(%rbp),%edi
lea    -0x422(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    -0x422(%rbp),%rsi
lea    0xd8a(%rip),%rdi
call   1050 <printf@plt>
movsbl -0x422(%rbp),%eax
cmp    $0x31,%eax
jne    12c3 <main+0x123>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1090 <sender@plt>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1070 <receiver@plt>
jmp    12e7 <main+0x147>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1070 <receiver@plt>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
call   1090 <sender@plt>
jmp    128a <main+0xea>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x428,%rsp
mov    0x2e37(%rip),%r14
mov    (%r14),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1278 <main+0xd8>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0xd62,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     128e <main+0xee>
lea    0xe(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
lea    0xde3(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
lea    0x20(%rsp),%r14
cmpb   $0x31,0xe(%rsp)
je     125b <main+0xbb>
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <receiver@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1090 <sender@plt>
cmpb   $0x31,0xe(%rsp)
jne    1240 <main+0xa0>
mov    %ebx,%edi
mov    %r14,%rsi
call   1090 <sender@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1070 <receiver@plt>
cmpb   $0x31,0xe(%rsp)
je     125b <main+0xbb>
jmp    1240 <main+0xa0>
lea    0xd81(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd72(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_LOOPBACK = global i32 0
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [17 x i8] c"your turn is %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [2 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %15, i32* %16
%17 = call i32 @htons(i32 3426)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_LOOPBACK
%20 = call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%22 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %21, i32 0, i32 0
store i32 %20, i32* %22
%23 = load i32, i32* %3
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %14
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 2)
unreachable
30: ; preds = %14
%31 = load i32, i32* %3
%32 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%33 = call i32 @recv(i32 %31, i8* %32, i32 1024, i32 0)
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%35 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %30, %55
%37 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 49
br i1 %40, label %41, label %48
41: ; preds = %36
%42 = load i32, i32* %3
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%44 = call i32 @sender(i32 %42, i8* %43)
%45 = load i32, i32* %3
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%47 = call i32 @receiver(i32 %45, i8* %46)
br label %55
48: ; preds = %36
%49 = load i32, i32* %3
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%51 = call i32 @receiver(i32 %49, i8* %50)
%52 = load i32, i32* %3
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%54 = call i32 @sender(i32 %52, i8* %53)
br label %55
55: ; preds = %48, %41
br label %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@INADDR_LOOPBACK = global i32 0
@.str.1 = [8 x i8] c"connect\00"
@.str.2 = [17 x i8] c"your turn is %s\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca %struct.sockaddr_in
%3 = alloca [2 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%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 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 3426)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_LOOPBACK
%20 = tail call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i64 @connect(i32 %9, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 2)
unreachable
28: ; preds = %14
%29 = call i32 @recv(i32 %9, i8* %6, i32 1024, i32 0)
%30 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %6)
br label %31
31: ; preds = %40, %28
%32 = load i8, i8* %6
%33 = icmp eq i8 %32, 49
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i32 @sender(i32 %9, i8* %4)
%36 = call i32 @receiver(i32 %9, i8* %4)
br label %40
37: ; preds = %31
%38 = call i32 @receiver(i32 %9, i8* %4)
%39 = call i32 @sender(i32 %9, i8* %4)
br label %40
40: ; preds = %37, %34
br label %31
}