#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 in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EOF;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memset(void *, char, int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char *, int);
scalar_t__ scanf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int clientlen;
  int fd, newfd;
  char *hname = "localhost";
  struct hostent *hostptr;
  struct sockaddr_in serveraddr, clientaddr;
  if (argc > 1) {
    hname = argv[1];
  }
  fd = socket(AF_INET, SOCK_STREAM, 0);
  hostptr = gethostbyname(hname);
  memset((void *)&serveraddr, '\0', sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  serveraddr.sin_addr.s_addr =
      ((struct in_addr *)(hostptr->h_addr_list[0]))->s_addr;
  serveraddr.sin_port = htons((unsigned short)58000);
  connect(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
  for (;;) {
    char buf[100];
    if (scanf("%s", buf) == EOF) {
      break;
    }
    buf[99] = '\0';
    write(fd, buf, sizeof(buf) - 1);
    printf("Server reply:\n");
    while (read(fd, buf, sizeof(buf) - 1) > 0) {
      printf("%s", buf);
    }
    printf("\n");
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe40(%rip),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11da <main+0x3a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    $0xe290,%edi
call   1040 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
lea    -0xc0(%rbp),%rsi
lea    0xdb0(%rip),%rdi
call   10a0 <scanf@plt>
mov    0x2d7a(%rip),%rcx
cmp    (%rcx),%rax
jne    1274 <main+0xd4>
jmp    12e5 <main+0x145>
movb   $0x0,-0x5d(%rbp)
mov    -0x18(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    $0x63,%edx
call   1030 <write@plt>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    $0x63,%edx
call   1080 <read@plt>
cmp    $0x0,%rax
jle    12d2 <main+0x132>
lea    -0xc0(%rbp),%rsi
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    129a <main+0xfa>
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    124c <main+0xac>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x80,%rsp
cmp    $0x2,%edi
jl     11da <main+0x1a>
mov    0x8(%rsi),%rbx
jmp    11e1 <main+0x21>
lea    0xe1f(%rip),%rbx
mov    0x2e00(%rip),%r15
mov    (%r15),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   10b0 <gethostbyname@plt>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    (%rbx),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    $0xe290,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
lea    0xdba(%rip),%rdi
lea    0x10(%rsp),%rsi
call   10c0 <scanf@plt>
mov    0x2d7f(%rip),%r12
cmp    (%r12),%rax
jne    1280 <main+0xc0>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rbx
lea    0xd81(%rip),%r14
lea    0xd77(%rip),%r15
jmp    12bb <main+0xfb>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10c0 <scanf@plt>
cmp    (%r12),%rax
je     1267 <main+0xa7>
movb   $0x0,0x73(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x63,%edx
call   1050 <write@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x63,%edx
call   10a0 <read@plt>
test   %rax,%rax
jle    12a0 <main+0xe0>
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12e0 <main+0x120>

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@EOF = global i64 0
@.str.2 = [15 x i8] c"Server reply:\0A\00"
@.str.3 = [2 x i8] c"\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 i32
%9 = alloca i8*
%10 = alloca %struct.hostent*
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
%13 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %9
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %9
br label %20
20: ; preds = %16, %2
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %7
%24 = load i8*, i8** %9
%25 = call %struct.hostent* @gethostbyname(i8* %24)
store %struct.hostent* %25, %struct.hostent** %10
%26 = bitcast %struct.sockaddr_in* %11 to i8*
%27 = call i32 @memset(i8* %26, i8 signext 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %28, i32* %29
%30 = load %struct.hostent*, %struct.hostent** %10
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 0
%32 = load i64*, i64** %31
%33 = getelementptr inbounds i64, i64* %32, i64 0
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to %struct.in_addr*
%36 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, 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(i16 zeroext -7536)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %7
%43 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%44 = call i32 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
br label %45
45: ; preds = %65, %20
%46 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%47 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %46)
%48 = load i64, i64* @EOF
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %45
br label %67
51: ; preds = %45
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 99
store i8 0, i8* %52
%53 = load i32, i32* %7
%54 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%55 = call i32 @write(i32 %53, i8* %54, i32 99)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %57
57: ; preds = %62, %51
%58 = load i32, i32* %7
%59 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%60 = call i64 @read(i32 %58, i8* %59, i32 99)
%61 = icmp sgt i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %63)
br label %57
65: ; preds = %57
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %45
67: ; preds = %50
%68 = load i32, i32* %7
%69 = call i32 @close(i32 %68)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@EOF = global i64 0
@str = [14 x i8] c"Server reply:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [100 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
br label %10
10: ; preds = %7, %2
%11 = phi i8* [ %9, %7 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %2 ]
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = tail call %struct.hostent* @gethostbyname(i8* %11)
%16 = call i32 @memset(i8* %5, i8 signext 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%20 = load i64*, i64** %19
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to %struct.in_addr*
%23 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %22, i64 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %24, i32* %25
%26 = call i32 @htons(i16 zeroext -7536)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @connect(i32 %14, %struct.sockaddr* %28, i32 12)
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%31 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = load i64, i64* @EOF
%33 = icmp eq i64 %31, %32
br i1 %33, label %50, label %34
34: ; preds = %10
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 99
br label %36
36: ; preds = %34, %45
store i8 0, i8* %35
%37 = call i32 @write(i32 %14, i8* %30, i32 99)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%39 = call i64 @read(i32 %14, i8* %30, i32 99)
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %36, %41
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%43 = call i64 @read(i32 %14, i8* %30, i32 99)
%44 = icmp sgt i64 %43, 0
br i1 %44, label %41, label %45
45: ; preds = %41, %36
%46 = call i32 @putchar(i32 10)
%47 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%48 = load i64, i64* @EOF
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %36
50: ; preds = %45, %10
%51 = call i32 @close(i32 %14)
ret i32 0
}