#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int c;
  struct sockaddr_in addr;
  char msg[1];
  ssize_t msglen;
  (void)argc;
  (void)argv;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(6666);
  inet_aton("127.0.0.2", &addr.sin_addr);
  printf("Connecting.\n");
  c = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (c < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
  }
  if (connect(c, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("connect");
    exit(EXIT_FAILURE);
  }
  printf("Sending a greeting.\n");
  msg[0] = '\n';
  msglen = send(c, msg, sizeof(msg), 0);
  if (msglen < 0) {
    perror("send");
    exit(EXIT_FAILURE);
  }
  printf("Waiting for the echo.\n");
  msglen = recv(c, msg, sizeof(msg), 0);
  if (msglen < 0) {
    perror("recv");
    exit(EXIT_FAILURE);
  }
  printf("Closing gracefully.\n");
  if (close(c) < 0) {
    perror("close");
    exit(EXIT_FAILURE);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1a0a,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rsi
lea    0xe26(%rip),%rdi
call   1080 <inet_aton@plt>
lea    0xe24(%rip),%rdi
call   1060 <printf@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1232 <main+0x92>
lea    0xdf8(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    1267 <main+0xc7>
lea    0xdca(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xdb8(%rip),%rdi
call   1060 <printf@plt>
movb   $0xa,-0x21(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    12b3 <main+0x113>
lea    0xd9b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd86(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    12fb <main+0x15b>
lea    0xd6f(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd5a(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
cmp    $0x0,%rax
jge    1333 <main+0x193>
lea    0xd51(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e3c(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x1a0a,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0xe39(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1080 <inet_aton@plt>
lea    0xe51(%rip),%rdi
call   1040 <puts@plt>
mov    (%rbx),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1283 <main+0xe3>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     128c <main+0xec>
lea    0xe18(%rip),%rdi
call   1040 <puts@plt>
movb   $0xa,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %rax,%rax
js     1295 <main+0xf5>
lea    0xe03(%rip),%rdi
call   1040 <puts@plt>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     129e <main+0xfe>
lea    0xdf5(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
call   1070 <close@plt>
test   %rax,%rax
js     12a7 <main+0x107>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xd80(%rip),%rdi
jmp    12ae <main+0x10e>
lea    0xd7e(%rip),%rdi
jmp    12ae <main+0x10e>
lea    0xd7d(%rip),%rdi
jmp    12ae <main+0x10e>
lea    0xd79(%rip),%rdi
jmp    12ae <main+0x10e>
lea    0xd75(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.2\00"
@.str.1 = [13 x i8] c"Connecting.\0A\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [21 x i8] c"Sending a greeting.\0A\00"
@.str.5 = [5 x i8] c"send\00"
@.str.6 = [23 x i8] c"Waiting for the echo.\0A\00"
@.str.7 = [5 x i8] c"recv\00"
@.str.8 = [21 x i8] c"Closing gracefully.\0A\00"
@.str.9 = [6 x i8] c"close\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca [1 x i8]
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = load i8**, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @htons(i32 6666)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%17 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = load i32, i32* @IPPROTO_TCP
%22 = call i32 @socket(i32 %19, i32 %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %2
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%32 = call i64 @connect(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%40 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
store i8 10, i8* %40
%41 = load i32, i32* %6
%42 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%43 = call i64 @send(i32 %41, i8* %42, i32 1, i32 0)
store i64 %43, i64* %9
%44 = load i64, i64* %9
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %38
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %38
%51 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
%52 = load i32, i32* %6
%53 = getelementptr inbounds [1 x i8], [1 x i8]* %8, i64 0, i64 0
%54 = call i64 @recv(i32 %52, i8* %53, i32 1, i32 0)
store i64 %54, i64* %9
%55 = load i64, i64* %9
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %50
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %50
%62 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0))
%63 = load i32, i32* %6
%64 = call i64 @close(i32 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %61
%67 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%68 = load i32, i32* @EXIT_FAILURE
%69 = call i32 @exit(i32 %68)
unreachable
70: ; preds = %61
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.2\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [8 x i8] c"connect\00"
@.str.5 = [5 x i8] c"send\00"
@.str.7 = [5 x i8] c"recv\00"
@.str.9 = [6 x i8] c"close\00"
@str = [12 x i8] c"Connecting.\00"
@str.10 = [20 x i8] c"Sending a greeting.\00"
@str.11 = [22 x i8] c"Waiting for the echo.\00"
@str.12 = [20 x i8] c"Closing gracefully.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [1 x i8], [1 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 6666)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%12 = call i32 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %11)
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = load i32, i32* @IPPROTO_TCP
%17 = call i32 @socket(i32 %14, i32 %15, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @connect(i32 %17, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %23
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.10, i64 0, i64 0))
store i8 10, i8* %6
%33 = call i64 @send(i32 %17, i8* %6, i32 1, i32 0)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %31
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.11, i64 0, i64 0))
%41 = call i64 @recv(i32 %17, i8* %6, i32 1, i32 0)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %39
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.12, i64 0, i64 0))
%49 = call i64 @close(i32 %17)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %47
ret i32 0
}