#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 {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ s_addr;
typedef int /*<<< orphan*/ c_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ read(int, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
int main(void) {
  int c_socket, s_socket;
  int len;
  struct sockaddr_in c_addr, s_addr;
  int result;
  int a;
  int b;
  char op;
  s_socket = socket(PF_INET, SOCK_STREAM, 0);
  memset(&s_addr, 0, sizeof(s_addr));
  s_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  s_addr.sin_family = AF_INET;
  s_addr.sin_port = htons(2000);
  if (bind(s_socket, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1) {
    printf("can not bind\n");
    return -1;
  }
  if (listen(s_socket, 5) == -1) {
    printf("listen fail\n");
    return -1;
  }
  len = sizeof(c_addr);
  c_socket = accept(s_socket, (struct sockaddr *)&c_addr, &len);
  while (1) {
    read(c_socket, &a, sizeof(int));
    read(c_socket, &op, 1);
    read(c_socket, &b, sizeof(int));
    switch (op) {
    case '+':
      result = a + b;
      break;
    case '-':
      result = a - b;
      break;
    case '*':
      result = a * b;
      break;
    case '/':
      result = a / b;
      break;
    }
    write(c_socket, &result, sizeof(int));
  }
  close(c_socket);
  close(s_socket);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xc(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x7d0,%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1237 <main+0x97>
lea    0xdf2(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <main+0x195>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1265 <main+0xc5>
lea    0xdd2(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1335 <main+0x195>
movl   $0xc,-0x10(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %al,-0x49(%rbp)
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
mov    %rdx,-0x58(%rbp)
call   1080 <read@plt>
mov    -0x49(%rbp),%al
mov    -0x8(%rbp),%edi
lea    -0x3d(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
mov    -0x58(%rbp),%rdx
mov    -0x49(%rbp),%al
mov    -0x8(%rbp),%edi
lea    -0x3c(%rbp),%rsi
call   1080 <read@plt>
movsbl -0x3d(%rbp),%eax
add    $0xffffffd6,%eax
mov    %eax,%ecx
mov    %rcx,-0x48(%rbp)
sub    $0x5,%eax
ja     131f <main+0x17f>
mov    -0x48(%rbp),%rax
lea    0xd1f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x38(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    131f <main+0x17f>
mov    -0x38(%rbp),%eax
sub    -0x3c(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    131f <main+0x17f>
mov    -0x38(%rbp),%eax
imul   -0x3c(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    131f <main+0x17f>
mov    -0x38(%rbp),%eax
cltd
idivl  -0x3c(%rbp)
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
jmp    127f <main+0xdf>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x20(%rsp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    $0x7d0,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
je     130d <main+0x16d>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
je     1316 <main+0x176>
movl   $0xc,0x14(%rsp)
lea    0x28(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %ebx,%edi
call   10b0 <accept@plt>
mov    %eax,%ebx
lea    0xc(%rsp),%r14
lea    0x7(%rsp),%r15
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xd93(%rip),%rbp
jmp    128c <main+0xec>
nop
mov    0xc(%rsp),%eax
cltd
idivl  0x8(%rsp)
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    $0x4,%edx
mov    %ebx,%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <read@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1080 <read@plt>
mov    $0x4,%edx
mov    %ebx,%edi
mov    %r12,%rsi
xor    %eax,%eax
call   1080 <read@plt>
movsbl 0x7(%rsp),%eax
add    $0xffffffd6,%eax
cmp    $0x5,%eax
ja     127d <main+0xdd>
movslq 0x0(%rbp,%rax,4),%rax
add    %rbp,%rax
jmp    *%rax
mov    0x8(%rsp),%eax
imul   0xc(%rsp),%eax
jmp    1279 <main+0xd9>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%eax
add    0xc(%rsp),%eax
jmp    1279 <main+0xd9>
nopl   (%rax)
mov    0xc(%rsp),%eax
sub    0x8(%rsp),%eax
jmp    1279 <main+0xd9>
lea    0xd10(%rip),%rdi
jmp    131d <main+0x17d>
lea    0xcfb(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@.str = [14 x i8] c"can not bind\0A\00"
@.str.1 = [13 x i8] c"listen fail\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i32 0, i32* %1
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %3
%14 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%15 = load i32, i32* @INADDR_ANY
%16 = call i32 @htonl(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%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 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %19, i32* %20
%21 = call i32 @htons(i32 2000)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* %3
%24 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%25 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %0
%28 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %67
29: ; preds = %0
%30 = load i32, i32* %3
%31 = call i32 @listen(i32 %30, i32 5)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %67
35: ; preds = %29
store i32 12, i32* %4
%36 = load i32, i32* %3
%37 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%38 = call i32 @accept(i32 %36, %struct.sockaddr* %37, i32* %4)
store i32 %38, i32* %2
br label %39
39: ; preds = %35, %64
%40 = load i32, i32* %2
%41 = call i32 (i32, ...) @read(i32 %40, i32* %8, i64 4)
%42 = load i32, i32* %2
%43 = call i32 (i32, ...) @read(i32 %42, i8* %10, i32 1)
%44 = load i32, i32* %2
%45 = call i32 (i32, ...) @read(i32 %44, i32* %9, i64 4)
%46 = load i8, i8* %10
%47 = sext i8 %46 to i32
switch i32 %47, label %64 [
i32 43, label %48
i32 45, label %52
i32 42, label %56
i32 47, label %60
]
48: ; preds = %39
%49 = load i32, i32* %8
%50 = load i32, i32* %9
%51 = add nsw i32 %49, %50
store i32 %51, i32* %7
br label %64
52: ; preds = %39
%53 = load i32, i32* %8
%54 = load i32, i32* %9
%55 = sub nsw i32 %53, %54
store i32 %55, i32* %7
br label %64
56: ; preds = %39
%57 = load i32, i32* %8
%58 = load i32, i32* %9
%59 = mul nsw i32 %57, %58
store i32 %59, i32* %7
br label %64
60: ; preds = %39
%61 = load i32, i32* %8
%62 = load i32, i32* %9
%63 = sdiv i32 %61, %62
store i32 %63, i32* %7
br label %64
64: ; preds = %39, %60, %56, %52, %48
%65 = load i32, i32* %2
%66 = call i32 @write(i32 %65, i32* %7, i32 4)
br label %39
67: ; preds = %33, %27
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@str = [12 x i8] c"listen fail\00"
@str.2 = [13 x i8] c"can not bind\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = bitcast i32* %1 to i8*
%9 = bitcast %struct.sockaddr_in* %2 to i8*
%10 = bitcast %struct.sockaddr_in* %3 to i8*
%11 = bitcast i32* %4 to i8*
%12 = bitcast i32* %5 to i8*
%13 = bitcast i32* %6 to i8*
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %21, i32* %22
%23 = call i32 @htons(i32 2000)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @bind(i32 %16, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %58, label %28
28: ; preds = %0
%29 = call i32 @listen(i32 %16, i32 5)
%30 = icmp eq i32 %29, -1
br i1 %30, label %58, label %31
31: ; preds = %28
store i32 12, i32* %1
%32 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%33 = call i32 @accept(i32 %16, %struct.sockaddr* %32, i32* %1)
br label %34
34: ; preds = %56, %31
%35 = call i32 (i32, ...) @read(i32 %33, i32* %5, i64 4)
%36 = call i32 (i32, ...) @read(i32 %33, i8* %7, i32 1)
%37 = call i32 (i32, ...) @read(i32 %33, i32* %6, i64 4)
%38 = load i8, i8* %7
%39 = sext i8 %38 to i32
switch i32 %39, label %56 [
i32 43, label %40
i32 45, label %44
i32 42, label %48
i32 47, label %52
]
40: ; preds = %34
%41 = load i32, i32* %5
%42 = load i32, i32* %6
%43 = add nsw i32 %42, %41
store i32 %43, i32* %4
br label %56
44: ; preds = %34
%45 = load i32, i32* %5
%46 = load i32, i32* %6
%47 = sub nsw i32 %45, %46
store i32 %47, i32* %4
br label %56
48: ; preds = %34
%49 = load i32, i32* %5
%50 = load i32, i32* %6
%51 = mul nsw i32 %50, %49
store i32 %51, i32* %4
br label %56
52: ; preds = %34
%53 = load i32, i32* %5
%54 = load i32, i32* %6
%55 = sdiv i32 %53, %54
store i32 %55, i32* %4
br label %56
56: ; preds = %34, %52, %48, %44, %40
%57 = call i32 @write(i32 %33, i32* %4, i32 4)
br label %34
58: ; preds = %28, %0
%59 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @str.2, i64 0, i64 0), %0 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0), %28 ]
%60 = call i32 @puts(i8* dereferenceable(1) %59)
ret i32 -1
}