#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*/ name;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
scalar_t__ gethostbyname(char *);
int /*<<< orphan*/ gethostname(char *, int);
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;
  }
  char name[80];
  gethostname(name, sizeof(name));
  host = (struct hostent *)gethostbyname(name);
  printf("hostname is: %s\n", name);
  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!");
  while (1) {
    memset(buffer, '\0', sizeof(buffer));
    puts("please input msg:");
    fgets(buffer, sizeof(buffer), stdin);
    write(sockfd, buffer, sizeof(buffer));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%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    135b <main+0x18b>
lea    -0xe0(%rbp),%rdi
mov    $0x50,%esi
call   10d0 <gethostname@plt>
lea    -0xe0(%rbp),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x88(%rbp)
lea    -0xe0(%rbp),%rsi
lea    0xdd0(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    0x2d77(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1286 <main+0xb6>
lea    0xdb0(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    135b <main+0x18b>
lea    0xdac(%rip),%rdi
call   1030 <puts@plt>
lea    -0x80(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
lea    0xd9a(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x7c(%rbp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,-0x80(%rbp)
lea    0xd8c(%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    1306 <main+0x136>
lea    0xd7c(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    135b <main+0x18b>
lea    0xd7c(%rip),%rdi
call   1030 <puts@plt>
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1070 <memset@plt>
lea    0xd75(%rip),%rdi
call   1030 <puts@plt>
lea    -0x70(%rbp),%rdi
mov    0x2ca7(%rip),%rax
mov    (%rax),%edx
mov    $0x50,%esi
call   1080 <fgets@plt>
mov    -0x14(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1040 <write@plt>
jmp    1312 <main+0x142>
mov    -0x4(%rbp),%eax
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xb8,%rsp
cmp    $0x2,%edi
jne    12f9 <main+0x129>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x50,%esi
call   10d0 <gethostname@plt>
mov    %rbx,%rdi
call   10a0 <gethostbyname@plt>
lea    0xe0d(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    0x2dd2(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     131d <main+0x14d>
mov    %eax,%r14d
lea    0xdfd(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
lea    0xdee(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x22b8,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
lea    0xddf(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
je     1330 <main+0x160>
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
lea    0x60(%rsp),%rbx
lea    0xdec(%rip),%r15
mov    0x2d27(%rip),%rbp
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x50,%edx
call   1070 <memset@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
mov    $0x50,%esi
call   1080 <fgets@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x50,%edx
call   1040 <write@plt>
jmp    12c0 <main+0xf0>
mov    (%rsi),%rsi
lea    0xcfd(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0xb8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ebx
lea    0xcfc(%rip),%rdi
call   10b0 <perror@plt>
jmp    130d <main+0x13d>
mov    $0x3,%ebx
lea    0xd35(%rip),%rdi
call   10b0 <perror@plt>
jmp    130d <main+0x13d>

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 = [17 x i8] c"hostname is: %s\0A\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*
%10 = alloca [80 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
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 ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %61
18: ; preds = %2
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%20 = call i32 @gethostname(i8* %19, i32 80)
%21 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%22 = call i64 @gethostbyname(i8* %21)
%23 = inttoptr i64 %22 to %struct.hostent*
store %struct.hostent* %23, %struct.hostent** %9
%24 = getelementptr inbounds [80 x i8], [80 x i8]* %10, i64 0, i64 0
%25 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @AF_INET
%27 = load i32, i32* @SOCK_STREAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
store i32 %28, i32* %6
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %18
%31 = 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 %61
32: ; preds = %18
%33 = call i32 @puts(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%34 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %35, i32* %36
%37 = call i32 @inet_addr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = call i32 @htons(i32 8888)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %40, i32* %41
%42 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%43 = load i32, i32* %6
%44 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%45 = call i32 @connect(i32 %43, %struct.sockaddr* %44, i32 4)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %32
%48 = 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 %61
49: ; preds = %32
%50 = call i32 @puts(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
br label %51
51: ; preds = %49, %51
%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)
br label %51
61: ; preds = %47, %30, %13
%62 = load i32, i32* %3
ret i32 %62
}

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 = [17 x i8] c"hostname is: %s\0A\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 = alloca [80 x i8]
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = icmp eq i32 %0, 2
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %46
12: ; preds = %2
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%14 = call i32 @gethostname(i8* %13, i32 80)
%15 = call i64 @gethostbyname(i8* %13)
%16 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %42, label %21
21: ; preds = %12
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %24, i32* %25
%26 = call i32 @inet_addr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %26, i32* %27
%28 = call i32 @htons(i32 8888)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %28, i32* %29
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%32 = call i32 @connect(i32 %19, %struct.sockaddr* %31, i32 4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %42, label %34
34: ; preds = %21
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
br label %36
36: ; preds = %36, %34
%37 = call i32 @memset(i8* %6, i8 signext 0, i32 80)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%39 = load i32, i32* @stdin
%40 = call i32 @fgets(i8* %6, i32 80, i32 %39)
%41 = call i32 @write(i32 %19, i8* %6, i32 80)
br label %36
42: ; preds = %21, %12
%43 = phi i8* [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %12 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), %21 ]
%44 = phi i32 [ 1, %12 ], [ 3, %21 ]
%45 = call i32 @perror(i8* %43)
br label %46
46: ; preds = %42, %9
%47 = phi i32 [ -1, %9 ], [ %44, %42 ]
ret i32 %47
}