#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int dummy;
};
typedef int /*<<< orphan*/ si_server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ send_filename(int, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  struct hostent *server;
  struct sockaddr_in si_server;
  char buf[10];
  int PORTNO = atoi(argv[4]);
  server = gethostbyname(argv[3]);
  if (server == NULL) {
    fprintf(stderr, "ERROR, no such host\n");
    exit(0);
  }
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  bzero(&si_server, sizeof(si_server));
  si_server.sin_family = AF_INET;
  si_server.sin_port = htons(PORTNO);
  inet_pton(AF_INET, argv[1], &si_server.sin_addr.s_addr);
  if (connect(sockfd, (struct sockaddr *)&si_server, sizeof(si_server)) < 0)
    error("ERROR connecting");
  if (!send_filename(sockfd, argv[2])) {
    printf("ERROR sending filename\n");
    error("SENDING FILENAME");
  }
  printf("Created filename, now sending file contents \n");
  int fp = open(argv[1], O_RDONLY);
  for (;;) {
    int nbytes = read(fp, buf, 10);
    buf[10] = '\0';
    if (!nbytes)
      break;
    write(sockfd, buf, nbytes);
  }
  printf("File transferred.\n");
  close(fp);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    125e <main+0x5e>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
call   10a0 <fprintf@plt>
xor    %edi,%edi
call   1100 <exit@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    1290 <main+0x90>
lea    0xd8a(%rip),%rdi
call   10c0 <error@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x38(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x28(%rbp),%rdx
call   10b0 <inet_pton@plt>
mov    -0x3c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
cmp    $0x0,%rax
jge    12f6 <main+0xf6>
lea    0xd39(%rip),%rdi
call   10c0 <error@plt>
mov    -0x3c(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1040 <send_filename@plt>
cmp    $0x0,%eax
jne    1327 <main+0x127>
lea    0xd25(%rip),%rdi
call   1060 <printf@plt>
lea    0xd31(%rip),%rdi
call   10c0 <error@plt>
lea    0xd36(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2c9e(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%edi
lea    -0x32(%rbp),%rsi
mov    $0xa,%edx
call   1080 <read@plt>
mov    %eax,-0x44(%rbp)
movb   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    1373 <main+0x173>
jmp    1387 <main+0x187>
mov    -0x3c(%rbp),%edi
lea    -0x32(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1030 <write@plt>
jmp    134c <main+0x14c>
lea    0xd04(%rip),%rdi
call   1060 <printf@plt>
mov    -0x40(%rbp),%edi
call   1070 <close@plt>
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    0x20(%rsi),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r12d
mov    0x18(%r14),%rdi
call   1090 <gethostbyname@plt>
test   %rax,%rax
je     136f <main+0x16f>
mov    0x2db4(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    125a <main+0x5a>
lea    0xdc0(%rip),%rdi
call   10c0 <error@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    %r12d,%edi
call   1060 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x0(%rbp),%edi
mov    0x8(%r14),%rsi
mov    %rbx,%rdx
call   10b0 <inet_pton@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1110 <connect@plt>
test   %rax,%rax
jns    12af <main+0xaf>
lea    0xd80(%rip),%rdi
call   10c0 <error@plt>
mov    0x10(%r14),%rsi
mov    %r15d,%edi
call   1050 <send_filename@plt>
test   %eax,%eax
jne    12d7 <main+0xd7>
lea    0xd86(%rip),%rdi
call   1030 <puts@plt>
lea    0xd69(%rip),%rdi
call   10c0 <error@plt>
lea    0xd85(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%r14),%rdi
mov    0x2cf2(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,%ebp
lea    0x6(%rsp),%rsi
mov    %eax,%edi
mov    $0xa,%edx
call   1080 <read@plt>
movb   $0x0,0x10(%rsp)
test   %eax,%eax
je     1345 <main+0x145>
lea    0x6(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1080 <read@plt>
movb   $0x0,0x10(%rsp)
test   %eax,%eax
jne    1320 <main+0x120>
lea    0xd44(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r15d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c5a(%rip),%rax
mov    (%rax),%edi
lea    0xc81(%rip),%rsi
call   10a0 <fprintf@plt>
xor    %edi,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"ERROR, no such host\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@.str.2 = [17 x i8] c"ERROR connecting\00"
@.str.3 = [24 x i8] c"ERROR sending filename\0A\00"
@.str.4 = [17 x i8] c"SENDING FILENAME\00"
@.str.5 = [46 x i8] c"Created filename, now sending file contents \0A\00"
@O_RDONLY = global i32 0
@.str.6 = [19 x i8] c"File transferred.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
%8 = alloca [10 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 4
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %9
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 3
%19 = load i8*, i8** %18
%20 = call %struct.hostent* @gethostbyname(i8* %19)
store %struct.hostent* %20, %struct.hostent** %6
%21 = load %struct.hostent*, %struct.hostent** %6
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %27
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %2
%28 = load i32, i32* @AF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = call i32 @socket(i32 %28, i32 %29, i32 0)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %27
%36 = call i32 @bzero(%struct.sockaddr_in* %7, i32 12)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %37, i32* %38
%39 = load i32, i32* %9
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %40, i32* %41
%42 = load i32, i32* @AF_INET
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%47 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 0
%48 = call i32 @inet_pton(i32 %42, i8* %45, i32* %47)
%49 = load i32, i32* %10
%50 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%51 = call i64 @connect(i32 %49, %struct.sockaddr* %50, i32 12)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %35
%54 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %55
55: ; preds = %53, %35
%56 = load i32, i32* %10
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 2
%59 = load i8*, i8** %58
%60 = call i32 @send_filename(i32 %56, i8* %59)
%61 = icmp ne i32 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %55
%63 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%64 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %62, %55
%66 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0))
%67 = load i8**, i8*** %5
%68 = getelementptr inbounds i8*, i8** %67, i64 1
%69 = load i8*, i8** %68
%70 = load i32, i32* @O_RDONLY
%71 = call i32 @open(i8* %69, i32 %70)
store i32 %71, i32* %11
br label %72
72: ; preds = %80, %65
%73 = load i32, i32* %11
%74 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%75 = call i32 @read(i32 %73, i8* %74, i32 10)
store i32 %75, i32* %12
%76 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 10
store i8 0, i8* %76
%77 = load i32, i32* %12
%78 = icmp ne i32 %77, 0
br i1 %78, label %80, label %79
79: ; preds = %72
br label %85
80: ; preds = %72
%81 = load i32, i32* %10
%82 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%83 = load i32, i32* %12
%84 = call i32 @write(i32 %81, i8* %82, i32 %83)
br label %72
85: ; preds = %79
%86 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%87 = load i32, i32* %11
%88 = call i32 @close(i32 %87)
%89 = load i32, i32* %10
%90 = call i32 @close(i32 %89)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"ERROR, no such host\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@.str.2 = [17 x i8] c"ERROR connecting\00"
@.str.4 = [17 x i8] c"SENDING FILENAME\00"
@O_RDONLY = global i32 0
@str = [23 x i8] c"ERROR sending filename\00"
@str.7 = [45 x i8] c"Created filename, now sending file contents \00"
@str.8 = [18 x i8] c"File transferred.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [10 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds i8*, i8** %1, i64 4
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = getelementptr inbounds i8*, i8** %1, i64 3
%11 = load i8*, i8** %10
%12 = tail call %struct.hostent* @gethostbyname(i8* %11)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %2
%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 slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = tail call i32 @htons(i32 %9)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds i8*, i8** %1, i64 1
%33 = load i8*, i8** %32
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%35 = call i32 @inet_pton(i32 %31, i8* %33, i32* %34)
%36 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%37 = call i64 @connect(i32 %21, %struct.sockaddr* %36, i32 12)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %25
%40 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %25
%42 = getelementptr inbounds i8*, i8** %1, i64 2
%43 = load i8*, i8** %42
%44 = call i32 @send_filename(i32 %21, i8* %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%48 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %49
49: ; preds = %46, %41
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.7, i64 0, i64 0))
%51 = load i8*, i8** %32
%52 = load i32, i32* @O_RDONLY
%53 = call i32 @open(i8* %51, i32 %52)
%54 = call i32 @read(i32 %53, i8* %6, i32 10)
%55 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 10
store i8 0, i8* %55
%56 = icmp eq i32 %54, 0
br i1 %56, label %62, label %57
57: ; preds = %49, %57
%58 = phi i32 [ %60, %57 ], [ %54, %49 ]
%59 = call i32 @write(i32 %21, i8* %6, i32 %58)
%60 = call i32 @read(i32 %53, i8* %6, i32 10)
store i8 0, i8* %55
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %57
62: ; preds = %57, %49
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
%64 = call i32 @close(i32 %53)
%65 = call i32 @close(i32 %21)
ret i32 0
}