#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;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ write(int, ...);
int main(int argc, char **argv) {
  int port;
  int sock = -1;
  struct sockaddr_in address;
  struct hostent *host;
  int len;
  if (argc != 4) {
    printf("Usage: %s hostname port text\n", argv[0]);
    return -1;
  }
  if (sscanf(argv[2], "%d", &port) <= 0) {
    fprintf(stderr, "%s: error: wrong parameter: port\n", argv[0]);
    return -2;
  }
  sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock <= 0) {
    fprintf(stderr, "%s: error: cannot create socket\n", argv[0]);
    return -3;
  }
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  host = gethostbyname(argv[1]);
  if (!host) {
    fprintf(stderr, "%s: error: unknown host/server %s\n", argv[0], argv[1]);
    return -4;
  }
  memcpy(&address.sin_addr, host->h_addr_list[0], host->h_length);
  if (connect(sock, (struct sockaddr *)&address, sizeof(address))) {
    fprintf(stderr, "%s: error: cannot connect to host %s\n", argv[0], argv[1]);
    return -5;
  }
  len = strlen(argv[3]);
  write(sock, &len, sizeof(int));
  write(sock, argv[3], len);
  close(sock);
  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)
movl   $0xffffffff,-0x18(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11f6 <main+0x46>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1b(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <main+0x1fb>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe19(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   10b0 <sscanf@plt>
cmp    $0x0,%rax
jg     1242 <main+0x92>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    13ab <main+0x1fb>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    0x2d85(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1299 <main+0xe9>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    13ab <main+0x1fb>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12fe <main+0x14e>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xd79(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    13ab <main+0x1fb>
lea    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   10a0 <memcpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
je     1364 <main+0x1b4>
mov    0x2c97(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
lea    0xd36(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    13ab <main+0x1fb>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
mov    -0x34(%rbp),%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1070 <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    $0x20,%rsp
mov    %rsi,%rbx
cmp    $0x4,%edi
jne    128a <main+0xda>
mov    0x10(%rbx),%rdi
lea    0xe4f(%rip),%rsi
lea    0xc(%rsp),%rdx
call   10b0 <sscanf@plt>
test   %rax,%rax
jle    12a3 <main+0xf3>
mov    0x2dff(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
test   %eax,%eax
jle    12c7 <main+0x117>
mov    %eax,%r14d
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0xc(%rsp),%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%rbx),%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     12e8 <main+0x138>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   10a0 <memcpy@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
je     130d <main+0x15d>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
mov    0x8(%rbx),%rcx
lea    0xe0e(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xfffffffb,%ebp
jmp    134a <main+0x19a>
mov    (%rbx),%rsi
lea    0xd6c(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
jmp    134a <main+0x19a>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd6b(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xfffffffe,%ebp
jmp    134a <main+0x19a>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd69(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xfffffffd,%ebp
jmp    134a <main+0x19a>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
mov    0x8(%rbx),%rcx
lea    0xd65(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0xfffffffc,%ebp
jmp    134a <main+0x19a>
mov    0x18(%rbx),%rdi
call   1040 <strlen@plt>
mov    %eax,0x8(%rsp)
xor    %ebp,%ebp
lea    0x8(%rsp),%rsi
mov    $0x4,%edx
mov    %r14d,%edi
xor    %eax,%eax
call   1030 <write@plt>
mov    0x18(%rbx),%rsi
mov    0x8(%rsp),%edx
mov    %r14d,%edi
xor    %eax,%eax
call   1030 <write@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@.str = [30 x i8] c"Usage: %s hostname port text\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"%s: error: wrong parameter: port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.3 = [33 x i8] c"%s: error: cannot create socket\0A\00"
@.str.4 = [35 x i8] c"%s: error: unknown host/server %s\0A\00"
@.str.5 = [38 x i8] c"%s: error: cannot connect to host %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %7
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 4
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %103
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i64 @sscanf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %6)
%23 = icmp sle i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %18
%25 = load i32, i32* @stderr
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %28)
store i32 -2, i32* %3
br label %103
30: ; preds = %18
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = load i32, i32* @IPPROTO_TCP
%34 = call i32 @socket(i32 %31, i32 %32, i32 %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp sle i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %30
%38 = load i32, i32* @stderr
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 0
%41 = load i8*, i8** %40
%42 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %41)
store i32 -3, i32* %3
br label %103
43: ; preds = %30
%44 = load i32, i32* @AF_INET
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %44, i32* %45
%46 = load i32, i32* %6
%47 = call i32 @htons(i32 %46)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %47, i32* %48
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = call %struct.hostent* @gethostbyname(i8* %51)
store %struct.hostent* %52, %struct.hostent** %9
%53 = load %struct.hostent*, %struct.hostent** %9
%54 = icmp ne %struct.hostent* %53, null
br i1 %54, label %64, label %55
55: ; preds = %43
%56 = load i32, i32* @stderr
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 0
%59 = load i8*, i8** %58
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 1
%62 = load i8*, i8** %61
%63 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %59, i8* %62)
store i32 -4, i32* %3
br label %103
64: ; preds = %43
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%66 = load %struct.hostent*, %struct.hostent** %9
%67 = getelementptr inbounds %struct.hostent, %struct.hostent* %66, i32 0, i32 1
%68 = load i32*, i32** %67
%69 = getelementptr inbounds i32, i32* %68, i64 0
%70 = load i32, i32* %69
%71 = load %struct.hostent*, %struct.hostent** %9
%72 = getelementptr inbounds %struct.hostent, %struct.hostent* %71, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i32 @memcpy(i32* %65, i32 %70, i32 %73)
%75 = load i32, i32* %7
%76 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%77 = call i64 @connect(i32 %75, %struct.sockaddr* %76, i32 12)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %88
79: ; preds = %64
%80 = load i32, i32* @stderr
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 0
%83 = load i8*, i8** %82
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 1
%86 = load i8*, i8** %85
%87 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %83, i8* %86)
store i32 -5, i32* %3
br label %103
88: ; preds = %64
%89 = load i8**, i8*** %5
%90 = getelementptr inbounds i8*, i8** %89, i64 3
%91 = load i8*, i8** %90
%92 = call i32 @strlen(i8* %91)
store i32 %92, i32* %10
%93 = load i32, i32* %7
%94 = call i32 (i32, ...) @write(i32 %93, i32* %10, i64 4)
%95 = load i32, i32* %7
%96 = load i8**, i8*** %5
%97 = getelementptr inbounds i8*, i8** %96, i64 3
%98 = load i8*, i8** %97
%99 = load i32, i32* %10
%100 = call i32 (i32, ...) @write(i32 %95, i8* %98, i32 %99)
%101 = load i32, i32* %7
%102 = call i32 @close(i32 %101)
store i32 0, i32* %3
br label %103
103: ; preds = %88, %79, %55, %37, %24, %13
%104 = load i32, i32* %3
ret i32 %104
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@.str = [30 x i8] c"Usage: %s hostname port text\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"%s: error: wrong parameter: port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.3 = [33 x i8] c"%s: error: cannot create socket\0A\00"
@.str.4 = [35 x i8] c"%s: error: unknown host/server %s\0A\00"
@.str.5 = [38 x i8] c"%s: error: cannot connect to host %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = icmp eq i32 %0, 4
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %72
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = call i64 @sscanf(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%17 = icmp slt i64 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %1
%21 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %72
22: ; preds = %13
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = load i32, i32* @IPPROTO_TCP
%26 = call i32 @socket(i32 %23, i32 %24, i32 %25)
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %1
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %72
32: ; preds = %22
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %33, i32* %34
%35 = load i32, i32* %3
%36 = call i32 @htons(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %36, i32* %37
%38 = getelementptr inbounds i8*, i8** %1, i64 1
%39 = load i8*, i8** %38
%40 = call %struct.hostent* @gethostbyname(i8* %39)
%41 = icmp eq %struct.hostent* %40, null
br i1 %41, label %42, label %47
42: ; preds = %32
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %1
%45 = load i8*, i8** %38
%46 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %44, i8* %45)
br label %72
47: ; preds = %32
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%49 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i64 0, i32 1
%50 = load i32*, i32** %49
%51 = load i32, i32* %50
%52 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i64 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @memcpy(i32* %48, i32 %51, i32 %53)
%55 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%56 = call i64 @connect(i32 %26, %struct.sockaddr* %55, i32 12)
%57 = icmp eq i64 %56, 0
br i1 %57, label %63, label %58
58: ; preds = %47
%59 = load i32, i32* @stderr
%60 = load i8*, i8** %1
%61 = load i8*, i8** %38
%62 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %60, i8* %61)
br label %72
63: ; preds = %47
%64 = getelementptr inbounds i8*, i8** %1, i64 3
%65 = load i8*, i8** %64
%66 = call i32 @strlen(i8* %65)
store i32 %66, i32* %5
%67 = call i32 (i32, ...) @write(i32 %26, i32* %5, i64 4)
%68 = load i8*, i8** %64
%69 = load i32, i32* %5
%70 = call i32 (i32, ...) @write(i32 %26, i8* %68, i32 %69)
%71 = call i32 @close(i32 %26)
br label %72
72: ; preds = %63, %58, %42, %28, %18, %10
%73 = phi i32 [ -1, %10 ], [ -2, %18 ], [ -3, %28 ], [ -5, %58 ], [ 0, %63 ], [ -4, %42 ]
ret i32 %73
}