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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strlen(char *);
int main() {
  int cd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (cd == -1) {
    perror("failure creating socket");
    exit(EXIT_FAILURE);
  }
  char *host = "127.0.0.1";
  struct sockaddr_in serverAddress;
  bzero((void *)&serverAddress, sizeof(serverAddress));
  serverAddress.sin_family = AF_INET;
  serverAddress.sin_addr.s_addr = inet_addr(host);
  serverAddress.sin_port = htons(4242);
  int co =
      connect(cd, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
  if (co == -1) {
    perror("failure to connect");
    close(cd);
    exit(EXIT_FAILURE);
  }
  char *msg = "hello";
  int length = strlen(msg);
  int n = send(cd, msg, length, 0);
  if (n < 0) {
    perror("error sending message");
    close(cd);
    exit(EXIT_FAILURE);
  }
  char rmsg[7];
  bzero(rmsg, sizeof(rmsg));
  int m = read(cd, rmsg, sizeof(rmsg));
  if (m < 0) {
    perror("error reading message");
    close(cd);
    exit(EXIT_FAILURE);
  }
  printf("message received: %s", rmsg);
  close(cd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    120f <main+0x4f>
lea    0xe04(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xe02(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1092,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    128d <main+0xcd>
lea    0xdb0(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xda1(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    12e4 <main+0x124>
lea    0xd72(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x3f(%rbp),%rdi
mov    $0x7,%esi
call   10b0 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x3f(%rbp),%rsi
mov    $0x7,%edx
call   1080 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1332 <main+0x172>
lea    0xd3a(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    -0x3f(%rbp),%rsi
lea    0xd2a(%rip),%rdi
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
mov    (%rax),%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     12ad <main+0xed>
mov    %eax,%ebp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0xdff(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    $0x1092,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
je     12bb <main+0xfb>
lea    0xdea(%rip),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %eax,%eax
js     12c4 <main+0x104>
lea    0x19(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x7,%esi
call   10b0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x7,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12cd <main+0x10d>
lea    0xdd6(%rip),%rdi
lea    0x19(%rsp),%rsi
call   1060 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd4c(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e0 <main+0x120>
lea    0xd60(%rip),%rdi
jmp    12d4 <main+0x114>
lea    0xd70(%rip),%rdi
jmp    12d4 <main+0x114>
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [24 x i8] c"failure creating socket\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
@.str.2 = [19 x i8] c"failure to connect\00"
@.str.3 = [6 x i8] c"hello\00"
@.str.4 = [22 x i8] c"error sending message\00"
@.str.5 = [22 x i8] c"error reading message\00"
@.str.6 = [21 x i8] c"message received: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [7 x i8]
%9 = alloca i32
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = load i32, i32* @IPPROTO_TCP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %1
%14 = load i32, i32* %1
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %0
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8** %2
%21 = bitcast %struct.sockaddr_in* %3 to i8*
%22 = call i32 @bzero(i8* %21, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i8*, i8** %2
%26 = call i32 @inet_addr(i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = call i32 @htons(i32 4242)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %1
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i32 @connect(i32 %31, %struct.sockaddr* %32, i32 12)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %42
36: ; preds = %20
%37 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %1
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %20
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i8** %5
%43 = load i8*, i8** %5
%44 = call i32 @strlen(i8* %43)
store i32 %44, i32* %6
%45 = load i32, i32* %1
%46 = load i8*, i8** %5
%47 = load i32, i32* %6
%48 = call i32 @send(i32 %45, i8* %46, i32 %47, i32 0)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %42
%52 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%53 = load i32, i32* %1
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %42
%58 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%59 = call i32 @bzero(i8* %58, i32 7)
%60 = load i32, i32* %1
%61 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%62 = call i32 @read(i32 %60, i8* %61, i32 7)
store i32 %62, i32* %9
%63 = load i32, i32* %9
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %71
65: ; preds = %57
%66 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%67 = load i32, i32* %1
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %57
%72 = getelementptr inbounds [7 x i8], [7 x i8]* %8, i64 0, i64 0
%73 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %72)
%74 = load i32, i32* %1
%75 = call i32 @close(i32 %74)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [24 x i8] c"failure creating socket\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@AF_INET = global i32 0
@.str.2 = [19 x i8] c"failure to connect\00"
@.str.3 = [6 x i8] c"hello\00"
@.str.4 = [22 x i8] c"error sending message\00"
@.str.5 = [22 x i8] c"error reading message\00"
@.str.6 = [21 x i8] c"message received: %s\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [7 x i8]
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @IPPROTO_TCP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = bitcast %struct.sockaddr_in* %1 to i8*
%14 = call i32 @bzero(i8* %13, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 4242)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i32 @connect(i32 %6, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %29
24: ; preds = %12
%25 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @close(i32 %6)
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %12
%30 = call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @send(i32 %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %30, i32 0)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @close(i32 %6)
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %29
%39 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
%40 = call i32 @bzero(i8* %39, i32 7)
%41 = call i32 @read(i32 %6, i8* %39, i32 7)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%45 = call i32 @close(i32 %6)
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %38
%49 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %39)
%50 = call i32 @close(i32 %6)
ret i32 0
}