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

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
unsigned int atoi(char *);
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ herror(char *);
int /*<<< orphan*/ htons(unsigned int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int sockfd, numbytes;
  char buf[100];
  struct hostent *he;
  struct sockaddr_in their_addr;
  unsigned int myport;
  if (argv[2])
    myport = atoi(argv[2]);
  else
    myport = 7838;
  if (argc != 3) {
    fprintf(stderr, "usage: %s hostname port\n", argv[0]);
    exit(1);
  }
  if ((he = gethostbyname(argv[1])) == NULL) {
    herror("gethostbyname");
    exit(1);
  }
  if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  their_addr.sin_family = PF_INET;
  their_addr.sin_port = htons(myport);
  their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  bzero(&(their_addr.sin_zero), 0);
  if (connect(sockfd, (struct sockaddr *)&their_addr,
              sizeof(struct sockaddr)) == -1) {
    perror("connect");
    exit(1);
  }
  if ((numbytes = recv(sockfd, buf, 100, 0)) == -1) {
    perror("recv");
    exit(1);
  }
  buf[numbytes] = 0;
  printf("Received: %s\n", buf);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x10(%rax)
je     1210 <main+0x40>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x9c(%rbp)
jmp    121a <main+0x4a>
movl   $0x1e9e,-0x9c(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     124a <main+0x7a>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdc5(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
jne    127e <main+0xae>
lea    0xdaa(%rip),%rdi
call   1090 <herror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12b9 <main+0xe9>
lea    0xd7d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x9c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x90(%rbp)
mov    -0x88(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
lea    -0x98(%rbp),%rdi
xor    %esi,%esi
call   10b0 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0x98(%rbp),%rsi
mov    $0x4,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
jne    132c <main+0x15c>
lea    0xd11(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1361 <main+0x191>
lea    0xce4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rsi
lea    0xcc6(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x10(%rsi),%rdi
test   %rdi,%rdi
je     12c1 <main+0xf1>
call   10c0 <atoi@plt>
mov    %eax,%r14d
cmp    $0x3,%ebp
jne    12d0 <main+0x100>
mov    0x8(%rbx),%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     12f2 <main+0x122>
mov    %rax,%rbp
mov    0x2db7(%rip),%r15
mov    (%r15),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     1308 <main+0x138>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
call   10b0 <bzero@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
je     1311 <main+0x141>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     131a <main+0x14a>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xd98(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1e9e,%r14d
cmp    $0x3,%ebp
je     11fd <main+0x2d>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd1d(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd20(%rip),%rdi
call   1090 <herror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd18(%rip),%rdi
jmp    1321 <main+0x151>
lea    0xd16(%rip),%rdi
jmp    1321 <main+0x151>
lea    0xd15(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"usage: %s hostname port\0A\00"
@.str.1 = [14 x i8] c"gethostbyname\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [5 x i8] c"recv\00"
@.str.5 = [14 x i8] c"Received: %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 [100 x i8]
%9 = alloca %struct.hostent*
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 2
%14 = load i8*, i8** %13
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %11
br label %22
21: ; preds = %2
store i32 7838, i32* %11
br label %22
22: ; preds = %21, %16
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 3
br i1 %24, label %25, label %32
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 0
%29 = load i8*, i8** %28
%30 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %22
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call %struct.hostent* @gethostbyname(i8* %35)
store %struct.hostent* %36, %struct.hostent** %9
%37 = icmp eq %struct.hostent* %36, null
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %32
%42 = load i32, i32* @PF_INET
%43 = load i32, i32* @SOCK_STREAM
%44 = call i32 @socket(i32 %42, i32 %43, i32 0)
store i32 %44, i32* %6
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %41
%50 = load i32, i32* @PF_INET
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 3
store i32 %50, i32* %51
%52 = load i32, i32* %11
%53 = call i32 @htons(i32 %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %53, i32* %54
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%56 = load %struct.hostent*, %struct.hostent** %9
%57 = getelementptr inbounds %struct.hostent, %struct.hostent* %56, i32 0, i32 0
%58 = load i64, i64* %57
%59 = inttoptr i64 %58 to %struct.in_addr*
%60 = bitcast %struct.in_addr* %55 to i8*
%61 = bitcast %struct.in_addr* %59 to i8*
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%63 = call i32 @bzero(i32* %62, i32 0)
%64 = load i32, i32* %6
%65 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%66 = call i32 @connect(i32 %64, %struct.sockaddr* %65, i32 4)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %49
%69 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %49
%72 = load i32, i32* %6
%73 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%74 = call i32 @recv(i32 %72, i8* %73, i32 100, i32 0)
store i32 %74, i32* %7
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %79
76: ; preds = %71
%77 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %71
%80 = load i32, i32* %7
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 %81
store i8 0, i8* %82
%83 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%84 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %83)
%85 = load i32, i32* %6
%86 = call i32 @close(i32 %85)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [25 x i8] c"usage: %s hostname port\0A\00"
@.str.1 = [14 x i8] c"gethostbyname\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [5 x i8] c"recv\00"
@.str.5 = [14 x i8] c"Received: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = icmp eq i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 @atoi(i8* %8)
br label %12
12: ; preds = %2, %10
%13 = phi i32 [ %11, %10 ], [ 7838, %2 ]
%14 = icmp eq i32 %0, 3
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %1
%18 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %12
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call %struct.hostent* @gethostbyname(i8* %22)
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = tail call i32 @herror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = load i32, i32* @PF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = tail call i32 @socket(i32 %29, i32 %30, i32 0)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%35 = tail call i32 @exit(i32 1)
unreachable
36: ; preds = %28
%37 = load i32, i32* @PF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
store i32 %37, i32* %38
%39 = tail call i32 @htons(i32 %13)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i64 0, i32 0
%42 = load i64, i64* %41
%43 = inttoptr i64 %42 to i32*
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%45 = load i32, i32* %43
store i32 %45, i32* %44
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%47 = call i32 @bzero(i32* %46, i32 0)
%48 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%49 = call i32 @connect(i32 %31, %struct.sockaddr* %48, i32 4)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %36
%52 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %36
%55 = call i32 @recv(i32 %31, i8* %5, i32 100, i32 0)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %54
%61 = sext i32 %55 to i64
%62 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %61
store i8 0, i8* %62
%63 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* %5)
%64 = call i32 @close(i32 %31)
ret i32 0
}