#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_addr;
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 connect(int, struct sockaddr *, int);
int /*<<< orphan*/ error_handling(char *);
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_addr;
  if (argc != 3) {
    printf("Usage : %s <IP> <port>\n", argv[0]);
    exit(1);
  }
  fp = fopen("received.dat", "wb");
  sd = socket(PF_INET, SOCK_STREAM, 0);
  if (sd == -1) {
    error_handling("socket() error");
  }
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
  serv_addr.sin_port = htons(atoi(argv[2]));
  if (connect(sd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
    error_handling("connect() error!");
  }
  while ((read_cnt = read(sd, buf, 30)) != 0) {
    fwrite((void *)buf, 1, read_cnt, fp);
  }
  write(sd, "Thank you", 10);
  puts("Received file dat");
  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     123d <main+0x3d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdd2(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0xdd4(%rip),%rdi
lea    0xdda(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1286 <main+0x86>
lea    0xda7(%rip),%rdi
call   10c0 <error_handling@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d4b(%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   10e0 <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   1100 <connect@plt>
cmp    $0xffffffff,%eax
jne    12ef <main+0xef>
lea    0xd4d(%rip),%rdi
call   10c0 <error_handling@plt>
jmp    12f4 <main+0xf4>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1e,%edx
call   10a0 <read@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     132b <main+0x12b>
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1110 <fwrite@plt>
jmp    12f4 <main+0xf4>
mov    -0x14(%rbp),%edi
lea    0xd13(%rip),%rsi
mov    $0xa,%edx
call   1040 <write@plt>
lea    0xd0c(%rip),%rdi
call   1030 <puts@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    1342 <main+0x142>
lea    0xdf9(%rip),%rdi
lea    0xdff(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%r12
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    125a <main+0x5a>
lea    0xdd3(%rip),%rdi
call   10c0 <error_handling@plt>
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d75(%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   10e0 <atoi@plt>
mov    %eax,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
jne    12b9 <main+0xb9>
lea    0xd83(%rip),%rdi
call   10c0 <error_handling@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1e,%edx
call   10a0 <read@plt>
test   %eax,%eax
je     1305 <main+0x105>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r12,%rcx
call   1110 <fwrite@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1e,%edx
call   10a0 <read@plt>
test   %eax,%eax
jne    12e0 <main+0xe0>
lea    0xd3c(%rip),%rsi
mov    %ebp,%edi
mov    $0xa,%edx
call   1040 <write@plt>
lea    0xd33(%rip),%rdi
call   1030 <puts@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    0xcb4(%rip),%rdi
call   1070 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

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