#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*/ str2;
typedef int /*<<< orphan*/ str;
typedef int socklen_t;
typedef int /*<<< orphan*/ seraddr;
typedef int /*<<< orphan*/ cliinfo;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strcmp(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int a, sersock, newsock, n;
  char str[25], str2[25];
  struct sockaddr_in seraddr;
  struct sockaddr_in cliinfo;
  socklen_t csize = sizeof(cliinfo);
  seraddr.sin_family = AF_INET;
  seraddr.sin_port = htons(1200);
  seraddr.sin_addr.s_addr = htonl(INADDR_ANY);
  if ((sersock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    error("\n socket");
    exit(0);
  }
  if (bind(sersock, (struct sockaddr *)&seraddr, sizeof(seraddr)) < 0) {
    error("\nBIND");
    exit(0);
  }
  if (listen(sersock, 1) < 0) {
    error("\n LISTEN");
  }
  if ((newsock = accept(sersock, (struct sockaddr *)&cliinfo, &csize)) < 0) {
    error("\n ACCEPT");
    exit(0);
  } else
    printf("\n now connected to %s\n", inet_ntoa(cliinfo.sin_addr));
  read(newsock, str, sizeof(str));
  do {
    printf("\n client msg:%s", str);
    printf("\n server msg:");
    scanf("%s", str2);
    write(newsock, str2, sizeof(str2));
    listen(newsock, 1);
    read(newsock, str, sizeof(str));
    n = strcmp(str, "BYE");
    a = strcmp(str2, "BYE");
  } while (n != 0 || a != 0);
  close(newsock);
  close(sersock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xc,-0x74(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    $0x4b0,%edi
call   1050 <htons@plt>
mov    %eax,-0x5c(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x60(%rbp)
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    126b <main+0x7b>
lea    0xda1(%rip),%rdi
call   10e0 <error@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
jge    1299 <main+0xa9>
lea    0xd7c(%rip),%rdi
call   10e0 <error@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
jge    12bc <main+0xcc>
lea    0xd58(%rip),%rdi
call   10e0 <error@plt>
mov    -0xc(%rbp),%edi
lea    -0x70(%rbp),%rsi
lea    -0x74(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    12eb <main+0xfb>
lea    0xd39(%rip),%rdi
call   10e0 <error@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    -0x70(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd24(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x19,%edx
call   1090 <read@plt>
lea    -0x30(%rbp),%rsi
lea    0xd18(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd1a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x50(%rbp),%rsi
lea    0xd05(%rip),%rdi
call   10c0 <scanf@plt>
mov    -0x10(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x19,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   10b0 <listen@plt>
mov    -0x10(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x19,%edx
call   1090 <read@plt>
lea    -0x30(%rbp),%rdi
lea    0xcd7(%rip),%rsi
call   10a0 <strcmp@plt>
mov    %eax,-0x14(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xcc4(%rip),%rsi
call   10a0 <strcmp@plt>
mov    %eax,-0x8(%rbp)
mov    $0x1,%al
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x75(%rbp)
jne    13b3 <main+0x1c3>
cmpl   $0x0,-0x8(%rbp)
setne  %al
mov    %al,-0x75(%rbp)
mov    -0x75(%rbp),%al
test   $0x1,%al
jne    1315 <main+0x125>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
mov    -0xc(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
movl   $0xc,0xc(%rsp)
mov    0x2deb(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x4b0,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
test   %eax,%eax
js     1361 <main+0x181>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
js     136a <main+0x18a>
mov    %ebx,%edi
mov    $0x1,%esi
call   10a0 <listen@plt>
test   %rax,%rax
jns    1276 <main+0x96>
lea    0xd9e(%rip),%rdi
call   10d0 <error@plt>
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,0x8(%rsp)
mov    %ebx,%edi
call   10e0 <accept@plt>
test   %eax,%eax
js     1373 <main+0x193>
mov    %eax,%ebp
mov    0x20(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xd7c(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x50(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x19,%edx
call   1090 <read@plt>
lea    0xd6e(%rip),%r15
lea    0xd77(%rip),%r12
lea    0xd6d(%rip),%r13
lea    0x30(%rsp),%r14
nopl   (%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   10b0 <scanf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x19,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   10a0 <listen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x19,%edx
call   1090 <read@plt>
cmpl   $0x455942,0x50(%rsp)
jne    12e0 <main+0x100>
cmpl   $0x455942,0x30(%rsp)
jne    12e0 <main+0x100>
mov    %ebp,%edi
call   1080 <close@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc98(%rip),%rdi
jmp    137a <main+0x19a>
lea    0xc98(%rip),%rdi
jmp    137a <main+0x19a>
lea    0xc9e(%rip),%rdi
call   10d0 <error@plt>
xor    %edi,%edi
call   10f0 <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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"\0A socket\00"
@.str.1 = [6 x i8] c"\0ABIND\00"
@.str.2 = [9 x i8] c"\0A LISTEN\00"
@.str.3 = [9 x i8] c"\0A ACCEPT\00"
@.str.4 = [23 x i8] c"\0A now connected to %s\0A\00"
@.str.5 = [16 x i8] c"\0A client msg:%s\00"
@.str.6 = [14 x i8] c"\0A server msg:\00"
@.str.7 = [3 x i8] c"%s\00"
@.str.8 = [4 x i8] c"BYE\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [25 x i8]
%7 = alloca [25 x i8]
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
store i32 0, i32* %1
store i32 12, i32* %10
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %11, i32* %12
%13 = call i32 @htons(i32 1200)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%18 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %17, i32 0, i32 0
store i32 %16, i32* %18
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %0
%24 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %0
%27 = load i32, i32* %3
%28 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%29 = call i64 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %26
%35 = load i32, i32* %3
%36 = call i64 @listen(i32 %35, i32 1)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = load i32, i32* %3
%42 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%43 = call i32 @accept(i32 %41, %struct.sockaddr* %42, i32* %10)
store i32 %43, i32* %4
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %40
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i8* @inet_ntoa(i32 %51)
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %52)
br label %54
54: ; preds = %48
%55 = load i32, i32* %4
%56 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 25)
br label %58
58: ; preds = %82, %54
%59 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%62 = getelementptr inbounds [25 x i8], [25 x i8]* %7, i64 0, i64 0
%63 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %62)
%64 = load i32, i32* %4
%65 = getelementptr inbounds [25 x i8], [25 x i8]* %7, i64 0, i64 0
%66 = call i32 @write(i32 %64, i8* %65, i32 25)
%67 = load i32, i32* %4
%68 = call i64 @listen(i32 %67, i32 1)
%69 = load i32, i32* %4
%70 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%71 = call i32 @read(i32 %69, i8* %70, i32 25)
%72 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%73 = call i32 @strcmp(i8* %72, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
store i32 %73, i32* %5
%74 = getelementptr inbounds [25 x i8], [25 x i8]* %7, i64 0, i64 0
%75 = call i32 @strcmp(i8* %74, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.8, i64 0, i64 0))
store i32 %75, i32* %2
br label %76
76: ; preds = %58
%77 = load i32, i32* %5
%78 = icmp ne i32 %77, 0
br i1 %78, label %82, label %79
79: ; preds = %76
%80 = load i32, i32* %2
%81 = icmp ne i32 %80, 0
br label %82
82: ; preds = %79, %76
%83 = phi i1 [ true, %76 ], [ %81, %79 ]
br i1 %83, label %58, label %84
84: ; preds = %82
%85 = load i32, i32* %4
%86 = call i32 @close(i32 %85)
%87 = load i32, i32* %3
%88 = call i32 @close(i32 %87)
ret i32 0
}

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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"\0A socket\00"
@.str.1 = [6 x i8] c"\0ABIND\00"
@.str.2 = [9 x i8] c"\0A LISTEN\00"
@.str.3 = [9 x i8] c"\0A ACCEPT\00"
@.str.4 = [23 x i8] c"\0A now connected to %s\0A\00"
@.str.5 = [16 x i8] c"\0A client msg:%s\00"
@.str.6 = [14 x i8] c"\0A server msg:\00"
@.str.7 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca [25 x i8]
%2 = alloca [25 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = getelementptr inbounds [25 x i8], [25 x i8]* %1, i64 0, i64 0
%7 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = tail call i32 @htons(i32 1200)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @INADDR_ANY
%16 = tail call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %0
%23 = tail call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%24 = tail call i32 @exit(i32 0)
unreachable
25: ; preds = %0
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i64 @bind(i32 %20, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %25
%33 = call i64 @listen(i32 %20, i32 1)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = call i32 @accept(i32 %20, %struct.sockaddr* %38, i32* %5)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %37
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i8* @inet_ntoa(i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %47)
%49 = call i32 @read(i32 %39, i8* %6, i32 25)
%50 = bitcast [25 x i8]* %1 to i32*
%51 = bitcast [25 x i8]* %2 to i32*
br label %52
52: ; preds = %52, %44
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %7)
%56 = call i32 @write(i32 %39, i8* %7, i32 25)
%57 = call i64 @listen(i32 %39, i32 1)
%58 = call i32 @read(i32 %39, i8* %6, i32 25)
%59 = load i32, i32* %50
%60 = icmp ne i32 %59, 4544834
%61 = load i32, i32* %51
%62 = icmp ne i32 %61, 4544834
%63 = select i1 %60, i1 true, i1 %62
br i1 %63, label %52, label %64
64: ; preds = %52
%65 = call i32 @close(i32 %39)
%66 = call i32 @close(i32 %20)
ret i32 0
}