#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*/ member_1;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EOF;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ 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 sd;
  struct sockaddr_in server_addr = {AF_INET, htons(7777)};
  char buf[1024];
  struct hostent *hp;
  if (argc != 2) {
    printf("Usage: %s hostname\n", argv[0]);
    exit(1);
  }
  if ((hp = gethostbyname(argv[1])) == NULL) {
    printf("%s: %s unknown host\n", argv[0], argv[1]);
    exit(1);
  }
  bcopy(hp->h_addr_list[0], (char *)&server_addr.sin_addr, hp->h_length);
  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("client: socket failed");
    exit(1);
  }
  if (connect(sd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
    perror("client: connect FAILED:");
    exit(1);
  }
  printf("connect() successful!\n");
  printf("Input get file command\n");
  while (scanf("%s", buf) != EOF) {
    write(sd, buf, sizeof(buf));
    read(sd, buf, sizeof(buf));
    printf("Download Successful");
  }
  close(sd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x1e61,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1222 <main+0x62>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
jne    1267 <main+0xa7>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x428(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
mov    -0x428(%rbp),%rax
mov    (%rax),%edx
call   1090 <bcopy@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12c1 <main+0x101>
lea    0xd77(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12f1 <main+0x131>
lea    0xd5d(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd68(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x420(%rbp),%rsi
lea    0xd6b(%rip),%rdi
call   10a0 <scanf@plt>
mov    0x2cb9(%rip),%rcx
cmp    (%rcx),%rax
je     136b <main+0x1ab>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    0xd2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    130d <main+0x14d>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2dfd(%rip),%r14
mov    (%r14),%eax
mov    %eax,(%rsp)
mov    $0x1e61,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
movl   $0x0,0x8(%rsp)
cmp    $0x2,%ebp
jne    1312 <main+0x142>
mov    0x8(%rbx),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     132d <main+0x15d>
mov    0x8(%rax),%rcx
mov    (%rcx),%edi
mov    (%rax),%edx
mov    %rsp,%rsi
call   10a0 <bcopy@plt>
mov    (%r14),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
cmp    $0xffffffff,%eax
je     134c <main+0x17c>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0xffffffff,%eax
je     1355 <main+0x185>
lea    0xdff(%rip),%rdi
call   1030 <puts@plt>
lea    0xe09(%rip),%rdi
call   1030 <puts@plt>
lea    0xdd0(%rip),%rdi
lea    0x10(%rsp),%rsi
call   10b0 <scanf@plt>
mov    0x2d48(%rip),%r12
cmp    (%r12),%rax
je     12f9 <main+0x129>
lea    0x10(%rsp),%rbx
lea    0xdb0(%rip),%r14
lea    0xda6(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10b0 <scanf@plt>
cmp    (%r12),%rax
jne    12c0 <main+0xf0>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xce4(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    (%rbx),%rsi
mov    0x8(%rbx),%rdx
lea    0xcd9(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xcd6(%rip),%rdi
jmp    135c <main+0x18c>
lea    0xce3(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [20 x i8] c"Usage: %s hostname\0A\00"
@.str.1 = [21 x i8] c"%s: %s unknown host\0A\00"
@SOCK_STREAM = global i32 0
@.str.2 = [22 x i8] c"client: socket failed\00"
@.str.3 = [24 x i8] c"client: connect FAILED:\00"
@.str.4 = [23 x i8] c"connect() successful!\0A\00"
@.str.5 = [24 x i8] c"Input get file command\0A\00"
@.str.6 = [3 x i8] c"%s\00"
@EOF = global i64 0
@.str.7 = [20 x i8] c"Download Successful\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca [1024 x i8]
%9 = alloca %struct.hostent*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%11 = load i32, i32* @AF_INET
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%13 = call i32 @htons(i32 7777)
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 0, i32* %14
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call %struct.hostent* @gethostbyname(i8* %26)
store %struct.hostent* %27, %struct.hostent** %9
%28 = icmp eq %struct.hostent* %27, null
br i1 %28, label %29, label %38
29: ; preds = %23
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 0
%32 = load i8*, i8** %31
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %32, i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %23
%39 = load %struct.hostent*, %struct.hostent** %9
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %39, i32 0, i32 1
%41 = load i32*, i32** %40
%42 = getelementptr inbounds i32, i32* %41, i64 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%45 = bitcast i32* %44 to i8*
%46 = load %struct.hostent*, %struct.hostent** %9
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @bcopy(i32 %43, i8* %45, i32 %48)
%50 = load i32, i32* @AF_INET
%51 = load i32, i32* @SOCK_STREAM
%52 = call i32 @socket(i32 %50, i32 %51, i32 0)
store i32 %52, i32* %6
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %38
%55 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %38
%58 = load i32, i32* %6
%59 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%60 = call i32 @connect(i32 %58, %struct.sockaddr* %59, i32 12)
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %57
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %68
68: ; preds = %73, %65
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%70 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %69)
%71 = load i64, i64* @EOF
%72 = icmp ne i64 %70, %71
br i1 %72, label %73, label %81
73: ; preds = %68
%74 = load i32, i32* %6
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%76 = call i32 @write(i32 %74, i8* %75, i32 1024)
%77 = load i32, i32* %6
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%79 = call i32 @read(i32 %77, i8* %78, i32 1024)
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
br label %68
81: ; preds = %68
%82 = load i32, i32* %6
%83 = call i32 @close(i32 %82)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [20 x i8] c"Usage: %s hostname\0A\00"
@.str.1 = [21 x i8] c"%s: %s unknown host\0A\00"
@SOCK_STREAM = global i32 0
@.str.2 = [22 x i8] c"client: socket failed\00"
@.str.3 = [24 x i8] c"client: connect FAILED:\00"
@.str.6 = [3 x i8] c"%s\00"
@EOF = global i64 0
@.str.7 = [20 x i8] c"Download Successful\00"
@str = [22 x i8] c"connect() successful!\00"
@str.8 = [23 x i8] c"Input get file command\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%7 = load i32, i32* @AF_INET
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%9 = tail call i32 @htons(i32 7777)
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 0, i32* %10
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = icmp eq i32 %0, 2
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = load i8*, i8** %1
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = tail call %struct.hostent* @gethostbyname(i8* %19)
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %27
22: ; preds = %17
%23 = load i8*, i8** %1
%24 = load i8*, i8** %18
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %24)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %17
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 1
%29 = load i32*, i32** %28
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @bcopy(i32 %30, i8* %5, i32 %32)
%34 = load i32, i32* @AF_INET
%35 = load i32, i32* @SOCK_STREAM
%36 = call i32 @socket(i32 %34, i32 %35, i32 0)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %27
%39 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %27
%42 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%43 = call i32 @connect(i32 %36, %struct.sockaddr* %42, i32 12)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %41
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.8, i64 0, i64 0))
%51 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %11)
%52 = load i64, i64* @EOF
%53 = icmp eq i64 %51, %52
br i1 %53, label %61, label %54
54: ; preds = %48, %54
%55 = call i32 @write(i32 %36, i8* %11, i32 1024)
%56 = call i32 @read(i32 %36, i8* %11, i32 1024)
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0))
%58 = call i64 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %11)
%59 = load i64, i64* @EOF
%60 = icmp eq i64 %58, %59
br i1 %60, label %61, label %54
61: ; preds = %54, %48
%62 = call i32 @close(i32 %36)
ret i32 0
}