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

/* Variables and functions */
scalar_t__ connect_socket(int, char *, int, int);
int create_socket();
int /*<<< orphan*/ destroy_socket(int);
int /*<<< orphan*/ disconnect_socket(int);
int /*<<< orphan*/ keyboard_read(char *, char *, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int recv_socket(int, char *, int);
int send_socket(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  int sockfd = create_socket();
  if (sockfd != -1) {
    if (connect_socket(sockfd, argv[1], 9090, 10)) {
      char buffer[256] = {0};
      int n;
      while (1) {
        keyboard_read("Please enter the message: ", buffer, 255);
        n = send_socket(sockfd, buffer, strlen(buffer));
        if (n >= 0) {
          memset((void *)buffer, 0, 256);
          n = recv_socket(sockfd, buffer, 256);
          if (n >= 0) {
            printf("%s\n", buffer);
          } else {
            perror("ERROR reading from socket");
          }
        } else {
          perror("ERROR writing to socket");
        }
      }
    } else {
      perror("ERROR connecting");
    }
    disconnect_socket(sockfd);
  } else {
    perror("ERROR opening socket");
  }
  destroy_socket(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <create_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     1303 <main+0x153>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    $0x2382,%edx
mov    $0xa,%ecx
call   1080 <connect_socket@plt>
cmp    $0x0,%rax
je     12ea <main+0x13a>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    -0x120(%rbp),%rsi
lea    0xdde(%rip),%rdi
mov    $0xff,%edx
call   10d0 <keyboard_read@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x134(%rbp)
lea    -0x120(%rbp),%rax
mov    %rax,-0x130(%rbp)
lea    -0x120(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x134(%rbp),%edi
mov    -0x130(%rbp),%rsi
mov    %eax,%edx
call   10b0 <send_socket@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jl     12d9 <main+0x129>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   10a0 <recv_socket@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jl     12c8 <main+0x118>
lea    -0x120(%rbp),%rsi
lea    0xd5d(%rip),%rdi
call   1040 <printf@plt>
jmp    12d4 <main+0x124>
lea    0xd50(%rip),%rdi
call   10c0 <perror@plt>
jmp    12e5 <main+0x135>
lea    0xd59(%rip),%rdi
call   10c0 <perror@plt>
jmp    1214 <main+0x64>
lea    0xd60(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <disconnect_socket@plt>
jmp    130f <main+0x15f>
lea    0xd58(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <destroy_socket@plt>
xor    %eax,%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%rbx
xor    %eax,%eax
call   1090 <create_socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12f1 <main+0x141>
mov    0x8(%rbx),%rsi
mov    %ebp,%edi
mov    $0x2382,%edx
mov    $0xa,%ecx
call   1080 <connect_socket@plt>
test   %rax,%rax
je     12ff <main+0x14f>
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
lea    0xd9b(%rip),%r14
mov    %rsp,%rbx
lea    0xdac(%rip),%r15
lea    0xda9(%rip),%r12
lea    0xdbc(%rip),%r13
jmp    1290 <main+0xe0>
mov    %r13,%rdi
call   10c0 <perror@plt>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0xff,%edx
call   10d0 <keyboard_read@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   10b0 <send_socket@plt>
test   %eax,%eax
js     127f <main+0xcf>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
call   10a0 <recv_socket@plt>
test   %eax,%eax
js     12e7 <main+0x137>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
jmp    1290 <main+0xe0>
mov    %r12,%rdi
call   10c0 <perror@plt>
jmp    1290 <main+0xe0>
lea    0xd6a(%rip),%rdi
call   10c0 <perror@plt>
jmp    1312 <main+0x162>
lea    0xd4b(%rip),%rdi
call   10c0 <perror@plt>
mov    %ebp,%edi
call   1050 <disconnect_socket@plt>
mov    %ebp,%edi
call   1060 <destroy_socket@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Please enter the message: \00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [26 x i8] c"ERROR reading from socket\00"
@.str.3 = [24 x i8] c"ERROR writing to socket\00"
@.str.4 = [17 x i8] c"ERROR connecting\00"
@.str.5 = [21 x i8] c"ERROR opening socket\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [256 x i8]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (...) @create_socket()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %53
12: ; preds = %2
%13 = load i32, i32* %6
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i64 @connect_socket(i32 %13, i8* %16, i32 9090, i32 10)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %48
19: ; preds = %12
%20 = bitcast [256 x i8]* %7 to i8*
br label %21
21: ; preds = %19, %47
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%23 = call i32 @keyboard_read(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %22, i32 255)
%24 = load i32, i32* %6
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @send_socket(i32 %24, i8* %25, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp sge i32 %29, 0
br i1 %30, label %31, label %45
31: ; preds = %21
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i32 0, i32 256)
%34 = load i32, i32* %6
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%36 = call i32 @recv_socket(i32 %34, i8* %35, i32 256)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp sge i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %31
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%41 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %40)
br label %44
42: ; preds = %31
%43 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
br label %47
45: ; preds = %21
%46 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %44
br label %21
48: ; preds = %12
%49 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48
%51 = load i32, i32* %6
%52 = call i32 @disconnect_socket(i32 %51)
br label %55
53: ; preds = %2
%54 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
%56 = load i32, i32* %6
%57 = call i32 @destroy_socket(i32 %56)
ret i32 0
}

LLVM IR (-O2):
@.str = [27 x i8] c"Please enter the message: \00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [26 x i8] c"ERROR reading from socket\00"
@.str.3 = [24 x i8] c"ERROR writing to socket\00"
@.str.4 = [17 x i8] c"ERROR connecting\00"
@.str.5 = [21 x i8] c"ERROR opening socket\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = tail call i32 (...) @create_socket()
%5 = icmp eq i32 %4, -1
br i1 %5, label %32, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i64 @connect_socket(i32 %4, i8* %8, i32 9090, i32 10)
%10 = icmp eq i64 %9, 0
br i1 %10, label %29, label %11
11: ; preds = %6
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
br label %13
13: ; preds = %24, %11
%14 = call i32 @keyboard_read(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %12, i32 255)
%15 = call i32 @strlen(i8* %12)
%16 = call i32 @send_socket(i32 %4, i8* %12, i32 %15)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %27
18: ; preds = %13
%19 = call i32 @memset(i8* %12, i32 0, i32 256)
%20 = call i32 @recv_socket(i32 %4, i8* %12, i32 256)
%21 = icmp sgt i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %24
24: ; preds = %22, %25, %27
br label %13
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %24
27: ; preds = %13
%28 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %24
29: ; preds = %6
%30 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @disconnect_socket(i32 %4)
br label %34
32: ; preds = %2
%33 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = tail call i32 @destroy_socket(i32 %4)
ret i32 0
}