#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;
};
typedef int /*<<< orphan*/ serv_adr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(void *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int sd;
  FILE *fp;
  char buf[30];
  int read_cnt;
  struct sockaddr_in serv_adr;
  if (argc != 3) {
    printf("Usage: %s <IP> <port>\n", argv[0]);
    exit(1);
  }
  fp = fopen("receive.dat", "wb");
  sd = socket(PF_INET, SOCK_STREAM, 0);
  memset(&serv_adr, 0, sizeof(serv_adr));
  serv_adr.sin_family = AF_INET;
  serv_adr.sin_addr.s_addr = inet_addr(argv[1]);
  serv_adr.sin_port = htons(atoi(argv[2]));
  connect(sd, (struct sockaddr *)&serv_adr, sizeof(serv_adr));
  while ((read_cnt = read(sd, buf, 30)) != 0)
    fwrite((void *)buf, 1, read_cnt, fp);
  puts("Received file data");
  write(sd, "Thank you", 10);
  fclose(fp);
  close(sd);
  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)
cmpl   $0x3,-0x8(%rbp)
je     122d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xde2(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    0xde3(%rip),%rdi
lea    0xde8(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <inet_addr@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,-0x50(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1e,%edx
call   10a0 <read@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     12eb <main+0xfb>
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1100 <fwrite@plt>
jmp    12b4 <main+0xc4>
lea    0xd34(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%edi
lea    0xd38(%rip),%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x14(%rbp),%edi
call   1090 <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    $0x30,%rsp
mov    %rsi,%r14
cmp    $0x3,%edi
jne    1312 <main+0x122>
lea    0xe08(%rip),%rdi
lea    0xe0d(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%r12
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%r14),%rdi
call   10b0 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    0x10(%r14),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1e,%edx
call   10a0 <read@plt>
test   %eax,%eax
je     12d5 <main+0xe5>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r12,%rcx
call   1100 <fwrite@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1e,%edx
call   10a0 <read@plt>
test   %eax,%eax
jne    12b0 <main+0xc0>
lea    0xd4a(%rip),%rdi
call   1030 <puts@plt>
lea    0xd51(%rip),%rsi
mov    %ebp,%edi
mov    $0xa,%edx
call   1040 <write@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%rsi
lea    0xce4(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [23 x i8] c"Usage: %s <IP> <port>\0A\00"
@.str.1 = [12 x i8] c"receive.dat\00"
@.str.2 = [3 x i8] c"wb\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str.3 = [19 x i8] c"Received file data\00"
@.str.4 = [10 x i8] c"Thank you\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 [30 x i8]
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %20, i32** %7
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @inet_addr(i8* %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
%37 = call i32 @htons(i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* %6
%40 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%41 = call i32 @connect(i32 %39, %struct.sockaddr* %40, i32 12)
br label %42
42: ; preds = %47, %19
%43 = load i32, i32* %6
%44 = getelementptr inbounds [30 x i8], [30 x i8]* %8, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 30)
store i32 %45, i32* %9
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %42
%48 = getelementptr inbounds [30 x i8], [30 x i8]* %8, i64 0, i64 0
%49 = load i32, i32* %9
%50 = load i32*, i32** %7
%51 = call i32 @fwrite(i8* %48, i32 1, i32 %49, i32* %50)
br label %42
52: ; preds = %42
%53 = call i32 @puts(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%54 = load i32, i32* %6
%55 = call i32 @write(i32 %54, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 10)
%56 = load i32*, i32** %7
%57 = call i32 @fclose(i32* %56)
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [23 x i8] c"Usage: %s <IP> <port>\0A\00"
@.str.1 = [12 x i8] c"receive.dat\00"
@.str.2 = [3 x i8] c"wb\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@.str.3 = [19 x i8] c"Received file data\00"
@.str.4 = [10 x i8] c"Thank you\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [30 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %18, i32* %19
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @inet_addr(i8* %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds i8*, i8** %1, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i32 @connect(i32 %16, %struct.sockaddr* %29, i32 12)
%31 = call i32 @read(i32 %16, i8* %5, i32 30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %12, %33
%34 = phi i32 [ %36, %33 ], [ %31, %12 ]
%35 = call i32 @fwrite(i8* %5, i32 1, i32 %34, i32* %13)
%36 = call i32 @read(i32 %16, i8* %5, i32 30)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %33
38: ; preds = %33, %12
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @write(i32 %16, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 10)
%41 = call i32 @fclose(i32* %13)
%42 = call i32 @close(i32 %16)
ret i32 0
}