#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *const);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *const);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write_text(int, char const *const);
int main(int argc, char *const argv[]) {
  struct hostent *hostinfo;
  struct sockaddr_in name;
  int socket_fd;
  socket_fd = socket(PF_INET, SOCK_STREAM, 0);
  hostinfo = gethostbyname(argv[1]);
  name.sin_addr = *((struct in_addr *)hostinfo->h_addr);
  name.sin_port = htons(atoi(argv[2]));
  const char *const message = argv[3];
  name.sin_family = AF_INET;
  if (connect(socket_fd, (struct sockaddr *)&name,
              sizeof(struct sockaddr_in)) == -1) {
    perror("connect");
    return 1;
  }
  write_text(socket_fd, message);
  close(socket_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1230 <main+0xb0>
lea    0xde1(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    124b <main+0xcb>
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1060 <write_text@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
call   1050 <gethostbyname@plt>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x10(%rbx),%rdi
call   1080 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x18(%rbx),%rbx
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     1210 <main+0x90>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <write_text@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%r14d
jmp    1204 <main+0x84>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32, i32, %struct.in_addr }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str = [8 x i8] c"connect\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %8
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call %struct.hostent* @gethostbyname(i8* %15)
store %struct.hostent* %16, %struct.hostent** %6
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%18 = load %struct.hostent*, %struct.hostent** %6
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = inttoptr i64 %20 to %struct.in_addr*
%22 = bitcast %struct.in_addr* %17 to i8*
%23 = bitcast %struct.in_addr* %21 to i8*
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @atoi(i8* %26)
%28 = call i32 @htons(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %28, i32* %29
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
store i8* %32, i8** %9
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %8
%36 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%37 = call i32 @connect(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %2
%40 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %47
41: ; preds = %2
%42 = load i32, i32* %8
%43 = load i8*, i8** %9
%44 = call i32 @write_text(i32 %42, i8* %43)
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 0, i32* %3
br label %47
47: ; preds = %41, %39
%48 = load i32, i32* %3
ret i32 %48
}

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