#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;
};
struct hostent {
  int dummy;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd;
  char buffer[80];
  struct sockaddr_in server_addr;
  struct hostent *host;
  if (argc != 2) {
    printf("Usage: %s hostname\n", argv[0]);
    return -1;
  }
  if ((host = (struct hostent *)gethostbyname(argv[1])) == NULL) {
    perror("get hostname error");
    return -2;
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("create socket error");
    return 1;
  }
  puts("create socket done!");
  bzero(&server_addr, sizeof(struct sockaddr_in));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = inet_addr("192.168.2.110");
  server_addr.sin_port = htons(8888);
  puts("connect to server ...");
  if (connect(sockfd, (struct sockaddr *)&server_addr,
              sizeof(struct sockaddr)) == -1) {
    perror("connect to server error");
    return 3;
  }
  puts("connect server done!");
  memset(buffer, '\0', sizeof(buffer));
  puts("please input msg:");
  fgets(buffer, sizeof(buffer), stdin);
  write(sockfd, buffer, sizeof(buffer));
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1212 <main+0x42>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdff(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1364 <main+0x194>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <gethostbyname@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    1248 <main+0x78>
lea    0xddd(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1364 <main+0x194>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1285 <main+0xb5>
lea    0xdb3(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1364 <main+0x194>
lea    0xdaf(%rip),%rdi
call   1030 <puts@plt>
lea    -0x80(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
lea    0xd9d(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x7c(%rbp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,-0x80(%rbp)
lea    0xd8f(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x4,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1305 <main+0x135>
lea    0xd7f(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1364 <main+0x194>
lea    0xd7f(%rip),%rdi
call   1030 <puts@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1070 <memset@plt>
lea    0xd78(%rip),%rdi
call   1030 <puts@plt>
lea    -0x70(%rbp),%rdi
mov    0x2ca8(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1090 <fgets@plt>
mov    -0x14(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x60,%rsp
cmp    $0x2,%edi
jne    12d8 <main+0x108>
mov    0x8(%rsi),%rdi
call   10b0 <gethostbyname@plt>
test   %rax,%rax
je     12f7 <main+0x127>
mov    0x2dee(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     130a <main+0x13a>
mov    %eax,%ebx
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
lea    0xe0d(%rip),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
lea    0xdfe(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
je     131d <main+0x14d>
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%r14
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1070 <memset@plt>
lea    0xdfb(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0x50,%esi
call   1090 <fgets@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x50,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    12ec <main+0x11c>
mov    (%rsi),%rsi
lea    0xd1e(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd16(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xfffffffe,%ebp
jmp    12ec <main+0x11c>
lea    0xd16(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%ebp
jmp    12ec <main+0x11c>
lea    0xd4f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x3,%ebp
jmp    12ec <main+0x11c>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"Usage: %s hostname\0A\00"
@.str.1 = [19 x i8] c"get hostname error\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [20 x i8] c"create socket error\00"
@.str.3 = [20 x i8] c"create socket done!\00"
@.str.4 = [14 x i8] c"192.168.2.110\00"
@.str.5 = [22 x i8] c"connect to server ...\00"
@.str.6 = [24 x i8] c"connect to server error\00"
@.str.7 = [21 x i8] c"connect server done!\00"
@.str.8 = [18 x i8] c"please input msg:\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [80 x i8]
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %63
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @gethostbyname(i8* %20)
%22 = inttoptr i64 %21 to %struct.hostent*
store %struct.hostent* %22, %struct.hostent** %9
%23 = icmp eq %struct.hostent* %22, null
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %3
br label %63
26: ; preds = %17
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %63
33: ; preds = %26
%34 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %36, i32* %37
%38 = call i32 @inet_addr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = call i32 @htons(i32 8888)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %41, i32* %42
%43 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%44 = load i32, i32* %6
%45 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%46 = call i32 @connect(i32 %44, %struct.sockaddr* %45, i32 4)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %33
%49 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
store i32 3, i32* %3
br label %63
50: ; preds = %33
%51 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%52 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%53 = call i32 @memset(i8* %52, i8 signext 0, i32 80)
%54 = call i32 @puts(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%55 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%56 = load i32, i32* @stdin
%57 = call i32 @fgets(i8* %55, i32 80, i32 %56)
%58 = load i32, i32* %6
%59 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%60 = call i32 @write(i32 %58, i8* %59, i32 80)
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
store i32 0, i32* %3
br label %63
63: ; preds = %50, %48, %31, %24, %12
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [20 x i8] c"Usage: %s hostname\0A\00"
@.str.1 = [19 x i8] c"get hostname error\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [20 x i8] c"create socket error\00"
@.str.3 = [20 x i8] c"create socket done!\00"
@.str.4 = [14 x i8] c"192.168.2.110\00"
@.str.5 = [22 x i8] c"connect to server ...\00"
@.str.6 = [24 x i8] c"connect to server error\00"
@.str.7 = [21 x i8] c"connect server done!\00"
@.str.8 = [18 x i8] c"please input msg:\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %48
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i64 @gethostbyname(i8* %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %48
18: ; preds = %11
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = tail call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %48
25: ; preds = %18
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %28, i32* %29
%30 = tail call i32 @inet_addr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %30, i32* %31
%32 = tail call i32 @htons(i32 8888)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %32, i32* %33
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = call i32 @connect(i32 %21, %struct.sockaddr* %35, i32 4)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %25
%39 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %48
40: ; preds = %25
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%42 = call i32 @memset(i8* %5, i8 signext 0, i32 80)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%44 = load i32, i32* @stdin
%45 = call i32 @fgets(i8* %5, i32 80, i32 %44)
%46 = call i32 @write(i32 %21, i8* %5, i32 80)
%47 = call i32 @close(i32 %21)
br label %48
48: ; preds = %40, %38, %23, %16, %8
%49 = phi i32 [ -1, %8 ], [ -2, %16 ], [ 1, %23 ], [ 3, %38 ], [ 0, %40 ]
ret i32 %49
}