#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ get_file(char *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc < 4) {
    printf("Usage: ./memClient address port file [file...]\n");
    return 1;
  }
  struct sockaddr_in sck_addr;
  int sck;
  int i;
  memset(&sck_addr, 0, sizeof sck_addr);
  sck_addr.sin_family = AF_INET;
  inet_aton(argv[1], &sck_addr.sin_addr);
  sck_addr.sin_port = htons(atoi(argv[2]));
  if ((sck = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    perror("Cannot create socket");
    return 2;
  }
  if (connect(sck, (struct sockaddr *)&sck_addr, sizeof sck_addr) < 0) {
    perror("Cannot connect to socket");
    return 3;
  }
  for (i = 3; i < argc; ++i) {
    get_file(argv[i], sck);
  }
  close(sck);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11d8 <main+0x38>
lea    0xe39(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12db <main+0x13b>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
add    $0x4,%rsi
call   1080 <inet_aton@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1264 <main+0xc4>
lea    0xddd(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12db <main+0x13b>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    1297 <main+0xf7>
lea    0xdbf(%rip),%rdi
call   1090 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    12db <main+0x13b>
movl   $0x3,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12cc <main+0x12c>
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x24(%rbp),%esi
call   1070 <get_file@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    129e <main+0xfe>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jg     11c7 <main+0x27>
lea    0xe76(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    126f <main+0xcf>
mov    %rsi,%r15
mov    %edi,%r14d
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%r15),%rdi
lea    0x4(%rsp),%rsi
call   1080 <inet_aton@plt>
mov    0x10(%r15),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     127c <main+0xdc>
mov    %eax,%r12d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     128f <main+0xef>
mov    %r14d,%ebx
mov    $0x3,%ebp
nopl   (%rax)
mov    (%r15,%rbp,8),%rdi
mov    %r12d,%esi
call   1070 <get_file@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1250 <main+0xb0>
mov    %r12d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7d(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%eax
jmp    126f <main+0xcf>
lea    0xd7f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%eax
jmp    126f <main+0xcf>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [48 x i8] c"Usage: ./memClient address port file [file...]\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [21 x i8] c"Cannot create socket\00"
@.str.2 = [25 x i8] c"Cannot connect to socket\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 4
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %61
13: ; preds = %2
%14 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%21 = call i32 @inet_aton(i8* %19, i32* %20)
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* @PF_INET
%29 = load i32, i32* @SOCK_STREAM
%30 = load i32, i32* @IPPROTO_TCP
%31 = call i32 @socket(i32 %28, i32 %29, i32 %30)
store i32 %31, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %13
%34 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %61
35: ; preds = %13
%36 = load i32, i32* %7
%37 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%38 = call i64 @connect(i32 %36, %struct.sockaddr* %37, i32 12)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %3
br label %61
42: ; preds = %35
store i32 3, i32* %8
br label %43
43: ; preds = %55, %42
%44 = load i32, i32* %8
%45 = load i32, i32* %4
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %58
47: ; preds = %43
%48 = load i8**, i8*** %5
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %48, i64 %50
%52 = load i8*, i8** %51
%53 = load i32, i32* %7
%54 = call i32 @get_file(i8* %52, i32 %53)
br label %55
55: ; preds = %47
%56 = load i32, i32* %8
%57 = add nsw i32 %56, 1
store i32 %57, i32* %8
br label %43
58: ; preds = %43
%59 = load i32, i32* %7
%60 = call i32 @close(i32 %59)
store i32 0, i32* %3
br label %61
61: ; preds = %58, %40, %33, %11
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [21 x i8] c"Cannot create socket\00"
@.str.2 = [25 x i8] c"Cannot connect to socket\00"
@str = [47 x i8] c"Usage: ./memClient address port file [file...]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = icmp slt i32 %0, 4
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
br label %47
7: ; preds = %2
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %10, i32* %11
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%15 = call i32 @inet_aton(i8* %13, i32* %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = load i32, i32* @IPPROTO_TCP
%24 = call i32 @socket(i32 %21, i32 %22, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %7
%27 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %45
28: ; preds = %7
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i64 @connect(i32 %24, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %28
%33 = zext i32 %0 to i64
br label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %45
36: ; preds = %32, %36
%37 = phi i64 [ 3, %32 ], [ %41, %36 ]
%38 = getelementptr inbounds i8*, i8** %1, i64 %37
%39 = load i8*, i8** %38
%40 = call i32 @get_file(i8* %39, i32 %24)
%41 = add nuw nsw i64 %37, 1
%42 = icmp eq i64 %41, %33
br i1 %42, label %43, label %36
43: ; preds = %36
%44 = call i32 @close(i32 %24)
br label %45
45: ; preds = %43, %34, %26
%46 = phi i32 [ 2, %26 ], [ 3, %34 ], [ 0, %43 ]
br label %47
47: ; preds = %45, %5
%48 = phi i32 [ 1, %5 ], [ %46, %45 ]
ret i32 %48
}