#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;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int const, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int write(int const, char const **, int);
int main() {
  const int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in address = {0};
  address.sin_family = AF_INET;
  address.sin_port = htons(5001);
  if (inet_aton("127.0.0.1", &address.sin_addr) == 0) {
    perror("inet_aton");
    exit(1);
  }
  if (connect(socket_fd, (struct sockaddr *)&address, sizeof(address)) != 0) {
    perror("connect");
    exit(1);
  }
  while (1) {
    const char buf[10] = "hello  11 ";
    if (write(socket_fd, &buf, 10) != 10) {
      perror("write");
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x1389,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xe21(%rip),%rdi
call   1060 <inet_aton@plt>
cmp    $0x0,%rax
jne    1204 <main+0x84>
lea    0xe15(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     1235 <main+0xb5>
lea    0xdee(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    123a <main+0xba>
mov    0xde1(%rip),%rax
mov    %rax,-0x22(%rbp)
mov    0xdde(%rip),%ax
mov    %ax,-0x1a(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0xa,%edx
call   1030 <write@plt>
cmp    $0xa,%eax
je     1280 <main+0x100>
lea    0xdab(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    123a <main+0xba>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    0x2e69(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x1389,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0xe4a(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1050 <inet_aton@plt>
test   %rax,%rax
jne    11ce <main+0x5e>
lea    0xe3e(%rip),%rdi
jmp    123f <main+0xcf>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1080 <connect@plt>
test   %rax,%rax
jne    1238 <main+0xc8>
movabs $0x3120206f6c6c6568,%r14
20 20 31
mov    %r14,(%rsp)
movw   $0x2031,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xa,%edx
call   1030 <write@plt>
cmp    $0xa,%eax
jne    122f <main+0xbf>
mov    %rsp,%rbx
mov    %r14,(%rsp)
movw   $0x2031,0x8(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1030 <write@plt>
cmp    $0xa,%eax
je     1210 <main+0xa0>
lea    0xde6(%rip),%rdi
jmp    123f <main+0xcf>
lea    0xdd5(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [10 x i8] c"inet_aton\00"
@.str.2 = [8 x i8] c"connect\00"
@__const.main.buf = [10 x i8] c"hello 11 "
@.str.3 = [6 x i8] c"write\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [10 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %9, i32* %10
%11 = call i32 @htons(i32 5001)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%14 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %0
%20 = load i32, i32* %2
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i64 @connect(i32 %20, %struct.sockaddr* %21, i32 16)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
br label %28
28: ; preds = %27, %37
%29 = bitcast [10 x i8]* %4 to i8*
%30 = load i32, i32* %2
%31 = bitcast [10 x i8]* %4 to i8**
%32 = call i32 @write(i32 %30, i8** %31, i32 10)
%33 = icmp ne i32 %32, 10
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %28
br label %28
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [10 x i8] c"inet_aton\00"
@.str.2 = [8 x i8] c"connect\00"
@__const.main.buf = [10 x i8] c"hello 11 "
@.str.3 = [6 x i8] c"write\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [10 x i8]
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 5001)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%12 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%19 = call i64 @connect(i32 %5, %struct.sockaddr* %18, i32 16)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%23 = bitcast [10 x i8]* %2 to i8**
%24 = call i32 @write(i32 %5, i8** %23, i32 10)
%25 = icmp eq i32 %24, 10
br i1 %25, label %32, label %29
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %32, %21
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21, %32
%33 = call i32 @write(i32 %5, i8** %23, i32 10)
%34 = icmp eq i32 %33, 10
br i1 %34, label %32, label %29
}