#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*/ c_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, int *, int);
int /*<<< orphan*/ scanf(char *, int *, char *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ write(int, ...);
int main(void) {
  int c_socket;
  struct sockaddr_in c_addr;
  int result = 0;
  int a = 0;
  int b = 0;
  char op;
  c_socket = socket(PF_INET, SOCK_STREAM, 0);
  memset(&c_addr, 0, sizeof(c_addr));
  c_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  c_addr.sin_family = AF_INET;
  c_addr.sin_port = htons(2000);
  if (connect(c_socket, (struct sockaddr *)&c_addr, sizeof(c_addr)) == -1) {
    printf("can not connect\n");
    close(c_socket);
    return -1;
  }
  while (1) {
    printf("-> ");
    fflush(stdin);
    scanf("%d %c %d", &a, &op, &b);
    write(c_socket, &a, sizeof(int));
    write(c_socket, &op, sizeof(char));
    write(c_socket, &b, sizeof(int));
    read(c_socket, &result, sizeof(int));
    printf("%d %c %d = %d\n", a, op, b, result);
  }
  close(c_socket);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
lea    0xdf9(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x7d0,%edi
call   1040 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1263 <main+0xb3>
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbp
ret
jmp    1268 <main+0xb8>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
lea    0xd94(%rip),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x25(%rbp),%rdx
lea    -0x24(%rbp),%rcx
call   10b0 <scanf@plt>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    -0x20(%rbp),%esi
movsbl -0x25(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1268 <main+0xb8>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movl   $0x0,0x14(%rsp)
movl   $0x0,0x10(%rsp)
movl   $0x0,0xc(%rsp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebx
lea    0x18(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
lea    0xde4(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x20(%rsp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    $0x7d0,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
je     1314 <main+0x154>
lea    0x10(%rsp),%r12
lea    0xb(%rsp),%r13
lea    0xc(%rsp),%rbp
lea    0x14(%rsp),%r14
lea    0xda4(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xd83(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
call   10b0 <fflush@plt>
lea    0xd6b(%rip),%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
call   10c0 <scanf@plt>
mov    $0x4,%edx
mov    %ebx,%edi
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <write@plt>
mov    $0x1,%edx
mov    %ebx,%edi
mov    %r13,%rsi
xor    %eax,%eax
call   1040 <write@plt>
mov    $0x4,%edx
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1090 <read@plt>
mov    0x10(%rsp),%esi
movsbl 0xb(%rsp),%edx
mov    0xc(%rsp),%ecx
mov    0x14(%rsp),%r8d
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1280 <main+0xc0>
lea    0xd0b(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0xffffffff,%eax
add    $0x28,%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
@.str = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
@.str.1 = [17 x i8] c"can not connect\0A\00"
@.str.2 = [4 x i8] c"-> \00"
@stdin = global i32 0
@.str.3 = [9 x i8] c"%d %c %d\00"
@.str.4 = [15 x i8] c"%d %c %d = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
store i32 0, i32* %1
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%12 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @htons(i32 2000)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %2
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i32 @connect(i32 %19, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %27
23: ; preds = %0
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
ret i32 -1
27: ; preds = %0
br label %28
28: ; preds = %27, %28
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @stdin
%31 = call i32 @fflush(i32 %30)
%32 = call i32 @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %5, i8* %7, i32* %6)
%33 = load i32, i32* %2
%34 = call i32 (i32, ...) @write(i32 %33, i32* %5, i64 4)
%35 = load i32, i32* %2
%36 = call i32 (i32, ...) @write(i32 %35, i8* %7, i64 1)
%37 = load i32, i32* %2
%38 = call i32 (i32, ...) @write(i32 %37, i32* %6, i64 4)
%39 = load i32, i32* %2
%40 = call i32 @read(i32 %39, i32* %4, i32 4)
%41 = load i32, i32* %5
%42 = load i8, i8* %7
%43 = sext i8 %42 to i32
%44 = load i32, i32* %6
%45 = load i32, i32* %4
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %41, i32 %43, i32 %44, i32 %45)
br label %28
}

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
@.str = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
@.str.2 = [4 x i8] c"-> \00"
@stdin = global i32 0
@.str.3 = [9 x i8] c"%d %c %d\00"
@.str.4 = [15 x i8] c"%d %c %d = %d\0A\00"
@str = [16 x i8] c"can not connect\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%8 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%9 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%14 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = call i32 @htons(i32 2000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %18, i32* %19
%20 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%21 = call i32 @connect(i32 %12, %struct.sockaddr* %20, i32 12)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %0
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%25 = call i32 @close(i32 %12)
ret i32 -1
26: ; preds = %0, %26
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @stdin
%29 = call i32 @fflush(i32 %28)
%30 = call i32 @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32* %3, i8* %5, i32* %4)
%31 = call i32 (i32, ...) @write(i32 %12, i32* %3, i64 4)
%32 = call i32 (i32, ...) @write(i32 %12, i8* %5, i64 1)
%33 = call i32 (i32, ...) @write(i32 %12, i32* %4, i64 4)
%34 = call i32 @read(i32 %12, i32* %2, i32 4)
%35 = load i32, i32* %3
%36 = load i8, i8* %5
%37 = sext i8 %36 to i32
%38 = load i32, i32* %4
%39 = load i32, i32* %2
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %35, i32 %37, i32 %38, i32 %39)
br label %26
}