#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*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ puts(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  struct sockaddr_in serv_addr;
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  serv_addr.sin_port = htons(1234);
  connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  char buffer[100];
  int count;
  FILE *fp;
  fp = fopen("/tmp/file-server", "w");
  while ((count = recv(sock, buffer, 100, 0)) > 0) {
    fwrite(buffer, count, 1, fp);
  }
  puts("File transfer sucess!");
  fclose(fp);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xdf8(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
mov    $0x4d2,%edi
call   1060 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
lea    0xdd5(%rip),%rdi
lea    0xddf(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x90(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x84(%rbp)
cmp    $0x0,%eax
jle    128a <main+0xda>
lea    -0x80(%rbp),%rdi
mov    -0x84(%rbp),%esi
mov    -0x90(%rbp),%rcx
mov    $0x1,%edx
call   10c0 <fwrite@plt>
jmp    1248 <main+0x98>
lea    0xd8c(%rip),%rdi
call   1040 <puts@plt>
mov    -0x90(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    0x2e26(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
lea    0xe01(%rip),%rdi
call   1090 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x4d2,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
lea    0xdde(%rip),%rdi
lea    0xde8(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1288 <main+0xd8>
lea    0x10(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %eax,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10c0 <fwrite@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jg     1260 <main+0xb0>
lea    0xd8e(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r14d,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [17 x i8] c"/tmp/file-server\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [22 x i8] c"File transfer sucess!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i32*
store i32 0, i32* %1
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = load i32, i32* @IPPROTO_TCP
%10 = call i32 @socket(i32 %7, i32 %8, i32 %9)
store i32 %10, i32* %2
%11 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%12 = load i32, i32* @AF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %12, i32* %13
%14 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = call i32 @htons(i32 1234)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %17, i32* %18
%19 = load i32, i32* %2
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i32 @connect(i32 %19, %struct.sockaddr* %20, i32 12)
%22 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %6
br label %23
23: ; preds = %28, %0
%24 = load i32, i32* %2
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%26 = call i32 @recv(i32 %24, i8* %25, i32 100, i32 0)
store i32 %26, i32* %5
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%30 = load i32, i32* %5
%31 = load i32*, i32** %6
%32 = call i32 @fwrite(i8* %29, i32 %30, i32 1, i32* %31)
br label %23
33: ; preds = %23
%34 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %2
%38 = call i32 @close(i32 %37)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [17 x i8] c"/tmp/file-server\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [22 x i8] c"File transfer sucess!\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [100 x i8]
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @IPPROTO_TCP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = bitcast %struct.sockaddr_in* %1 to i8*
%8 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %9, i32* %10
%11 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %11, i32* %12
%13 = call i32 @htons(i32 1234)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %13, i32* %14
%15 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%16 = call i32 @connect(i32 %6, %struct.sockaddr* %15, i32 12)
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%18 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @recv(i32 %6, i8* %17, i32 100, i32 0)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %0, %21
%22 = phi i32 [ %24, %21 ], [ %19, %0 ]
%23 = call i32 @fwrite(i8* %17, i32 %22, i32 1, i32* %18)
%24 = call i32 @recv(i32 %6, i8* %17, i32 100, i32 0)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %21, label %26
26: ; preds = %21, %0
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @fclose(i32* %18)
%29 = call i32 @close(i32 %6)
ret i32 0
}