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

/* 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*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int ret = -1;
  int clientSock, wsize;
  struct sockaddr_in serverAddr;
  if ((clientSock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    goto leave;
  }
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  serverAddr.sin_port = htons(3000);
  if ((ret = connect(clientSock, (struct sockaddr *)&serverAddr,
                     sizeof(serverAddr)))) {
    perror("connect");
    goto error;
  }
  if ((wsize = send(clientSock, "Hello World!", sizeof("Hello World!"), 0)) <=
      0) {
    perror("send");
    ret = -1;
  } else {
    printf("Wrote '%s' (%d Bytes)\n", "Hello World!", wsize);
    ret = 0;
  }
error:
  close(clientSock);
leave:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11cc <main+0x4c>
lea    0xe3e(%rip),%rdi
call   1080 <perror@plt>
jmp    1286 <main+0x106>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe28(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    $0xbb8,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     1222 <main+0xa2>
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
jmp    127e <main+0xfe>
mov    -0xc(%rbp),%edi
lea    0xded(%rip),%rsi
mov    $0xd,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jg     125c <main+0xdc>
lea    0xddb(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    1279 <main+0xf9>
mov    -0x10(%rbp),%edx
lea    0xdc5(%rip),%rdi
lea    0xdac(%rip),%rsi
call   1050 <printf@plt>
movl   $0x0,-0x8(%rbp)
jmp    127e <main+0xfe>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     122a <main+0xaa>
mov    %eax,%ebx
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0xe49(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,0xc(%rsp)
mov    $0xbb8,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %eax,%eax
jne    123d <main+0xbd>
lea    0xe28(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    $0xd,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
test   %eax,%eax
jle    124d <main+0xcd>
lea    0xe1f(%rip),%rdi
lea    0xe06(%rip),%rsi
mov    %eax,%edx
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdcf(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1221 <main+0xa1>
mov    %eax,%ebp
lea    0xdcb(%rip),%rdi
call   1080 <perror@plt>
jmp    121a <main+0x9a>
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebp
jmp    121a <main+0x9a>

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

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
@.str = [7 x i8] c"socket\00"
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [13 x i8] c"Hello World!\00"
@.str.4 = [5 x i8] c"send\00"
@.str.5 = [23 x i8] c"Wrote '%s' (%d Bytes)\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %31
9: ; preds = %0
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %10, i32* %11
%12 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 3000)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %14, i32* %15
%16 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%17 = call i32 @connect(i32 %5, %struct.sockaddr* %16, i32 12)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %9
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %28
21: ; preds = %9
%22 = call i32 @send(i32 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 13, i32 0)
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %28
26: ; preds = %21
%27 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %28
28: ; preds = %24, %26, %19
%29 = phi i32 [ %17, %19 ], [ -1, %24 ], [ 0, %26 ]
%30 = call i32 @close(i32 %5)
br label %31
31: ; preds = %28, %7
%32 = phi i32 [ -1, %7 ], [ %29, %28 ]
ret i32 %32
}