#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, "127.0.0.1", 8080, 10)) {
      char buffer[256] = {0};
      int n;
      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     1302 <main+0x152>
mov    -0x14(%rbp),%edi
lea    0xe19(%rip),%rsi
mov    $0x1f90,%edx
mov    $0xa,%ecx
call   1080 <connect_socket@plt>
cmp    $0x0,%rax
je     12e9 <main+0x139>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    -0x120(%rbp),%rsi
lea    0xde9(%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     12d8 <main+0x128>
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     12c7 <main+0x117>
lea    -0x120(%rbp),%rsi
lea    0xd68(%rip),%rdi
call   1040 <printf@plt>
jmp    12d3 <main+0x123>
lea    0xd5b(%rip),%rdi
call   10c0 <perror@plt>
jmp    12e4 <main+0x134>
lea    0xd64(%rip),%rdi
call   10c0 <perror@plt>
jmp    12f5 <main+0x145>
lea    0xd6b(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <disconnect_socket@plt>
jmp    130e <main+0x15e>
lea    0xd63(%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   %rbx
sub    $0x108,%rsp
xor    %eax,%eax
call   1090 <create_socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     12d3 <main+0x123>
lea    0xe2e(%rip),%rsi
mov    %ebp,%edi
mov    $0x1f90,%edx
mov    $0xa,%ecx
call   1080 <connect_socket@plt>
test   %rax,%rax
je     12e1 <main+0x131>
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    0xdad(%rip),%rdi
mov    %rsp,%rbx
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     12ea <main+0x13a>
mov    %rsp,%rbx
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     12f3 <main+0x143>
lea    0xd74(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@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    %rbp
ret
lea    0xd92(%rip),%rdi
call   10c0 <perror@plt>
jmp    12c0 <main+0x110>
lea    0xd73(%rip),%rdi
jmp    12fa <main+0x14a>
lea    0xd52(%rip),%rdi
jmp    12fa <main+0x14a>
lea    0xd2f(%rip),%rdi
call   10c0 <perror@plt>
jmp    12b9 <main+0x109>

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

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