#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 socklen_t;
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int recv_data(const char *ipaddr, int port, int size) {
  printf("recv_data\n");
  int fd;
  int n;
  char data[1024] = {0};
  struct sockaddr_in server;
  socklen_t server_len;
  printf("Start %s\n", ipaddr);
  fd = socket(AF_INET, SOCK_STREAM, 0);
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = inet_addr(ipaddr);
  server.sin_port = htons(port);
  server_len = sizeof(server);
  connect(fd, (struct sockaddr *)&server, server_len);
  int i = 0;
  for (; i < size; ++i) {
    n = recvfrom(fd, data, 1024, 0, (struct sockaddr *)&server, &server_len);
    if (n < 0) {
      perror("Error receiving data");
      exit(-1);
    }
  }
  printf("Done %s\n", ipaddr);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<recv_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%rsi
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x42c(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x430(%rbp)
movl   $0xc,-0x434(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x434(%rbp),%edx
call   10b0 <connect@plt>
movl   $0x0,-0x438(%rbp)
mov    -0x438(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    12c8 <recv_data+0x128>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x430(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x434(%rbp),%r9
call   1080 <recvfrom@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12af <recv_data+0x10f>
lea    0xd75(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    12b4 <recv_data+0x114>
mov    -0x438(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x438(%rbp)
jmp    1259 <recv_data+0xb9>
mov    -0x8(%rbp),%rsi
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %edx,%ebp
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xe58(%rip),%rdi
call   1030 <puts@plt>
lea    0x20(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xe13(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dea(%rip),%r12
mov    (%r12),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %r14,%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %ebp,%ebp
jle    1280 <recv_data+0xd0>
lea    0x20(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    %r13,%r9
call   1090 <recvfrom@plt>
test   %eax,%eax
js     12ac <recv_data+0xfc>
add    $0xffffffff,%ebp
jne    1260 <recv_data+0xb0>
lea    0xd98(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd57(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"recv_data\0A\00"
@.str.1 = [10 x i8] c"Start %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"Error receiving data\00"
@.str.3 = [9 x i8] c"Done %s\0A\00"
define i32 @recv_data(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%14 = bitcast [1024 x i8]* %9 to i8*
%15 = load i8*, i8** %4
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %7
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i8*, i8** %4
%23 = call i32 @inet_addr(i8* %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = load i32, i32* %5
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %27, i32* %28
store i32 12, i32* %11
%29 = load i32, i32* %7
%30 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%31 = load i32, i32* %11
%32 = call i32 @connect(i32 %29, %struct.sockaddr* %30, i32 %31)
store i32 0, i32* %12
br label %33
33: ; preds = %48, %3
%34 = load i32, i32* %12
%35 = load i32, i32* %6
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %51
37: ; preds = %33
%38 = load i32, i32* %7
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%40 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%41 = call i32 @recvfrom(i32 %38, i8* %39, i32 1024, i32 0, %struct.sockaddr* %40, i32* %11)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %37
br label %48
48: ; preds = %47
%49 = load i32, i32* %12
%50 = add nsw i32 %49, 1
store i32 %50, i32* %12
br label %33
51: ; preds = %33
%52 = load i8*, i8** %4
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %52)
%54 = load i32, i32* %7
%55 = call i32 @close(i32 %54)
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.1 = [10 x i8] c"Start %s\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"Error receiving data\00"
@.str.3 = [9 x i8] c"Done %s\0A\00"
@str = [10 x i8] c"recv_data\00"
define i32 @recv_data(i8* %0, i32 %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @inet_addr(i8* %0)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 %1)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %19, i32* %20
store i32 12, i32* %6
%21 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%22 = call i32 @connect(i32 %14, %struct.sockaddr* %21, i32 12)
%23 = icmp sgt i32 %2, 0
br i1 %23, label %26, label %34
24: ; preds = %26
%25 = icmp eq i32 %30, %2
br i1 %25, label %34, label %26
26: ; preds = %3, %24
%27 = phi i32 [ %30, %24 ], [ 0, %3 ]
%28 = call i32 @recvfrom(i32 %14, i8* %8, i32 1024, i32 0, %struct.sockaddr* %21, i32* %6)
%29 = icmp slt i32 %28, 0
%30 = add nuw nsw i32 %27, 1
br i1 %29, label %31, label %24
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %24, %3
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%36 = call i32 @close(i32 %14)
ret i32 0
}