#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 */
typedef int /*<<< orphan*/ uint8_t;
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ reuse;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ debug_print(char *);
int /*<<< orphan*/ get_aggregator_port();
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ inform_user(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int sendto(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/,
           struct sockaddr *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int send_msg_to_aggregator(uint8_t *msg, int msgLen, char *server_ip) {
  struct hostent *he;
  struct in_addr inAddr;
  int sock = -1;
  int reuse = 1;
  int res = 0;
  struct sockaddr_in sinAddr;
  memset(&sinAddr, 0, sizeof(struct sockaddr_in));
  he = gethostbyname(server_ip);
  if (!he) {
    if (inet_aton(server_ip, &inAddr) == 0) {
      debug_print("ERROR: Unknown server address!\n");
      return -1;
    }
  } else {
    inAddr = *((struct in_addr *)he->h_addr);
  }
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock < 0) {
    debug_print("ERROR: Failed to open socket!\n");
    return -1;
  }
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse));
  sinAddr.sin_family = AF_INET;
  sinAddr.sin_addr = inAddr;
  sinAddr.sin_port = htons(get_aggregator_port());
  res = sendto(sock, msg, msgLen, 0, (struct sockaddr *)&sinAddr,
               sizeof(struct sockaddr_in));
  if (res < 0) {
    inform_user("ERROR: Failed to send to aggregator!\n");
  }
  return res;
}


x86 assembly (clang, -O0):
<send_msg_to_aggregator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x20(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1224 <send_msg_to_aggregator+0x84>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <inet_aton@plt>
cmp    $0x0,%rax
jne    121f <send_msg_to_aggregator+0x7f>
lea    0xdf2(%rip),%rdi
call   10b0 <debug_print@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <send_msg_to_aggregator+0x14f>
jmp    1230 <send_msg_to_aggregator+0x90>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    126e <send_msg_to_aggregator+0xce>
lea    0xdc3(%rip),%rdi
call   10b0 <debug_print@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ef <send_msg_to_aggregator+0x14f>
mov    -0x34(%rbp),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   10a0 <get_aggregator_port@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    -0x48(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12e9 <send_msg_to_aggregator+0x149>
lea    0xd5b(%rip),%rdi
call   1090 <inform_user@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_msg_to_aggregator>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%r15
movl   $0x1,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     11e3 <send_msg_to_aggregator+0x43>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
jmp    11f7 <send_msg_to_aggregator+0x57>
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1080 <inet_aton@plt>
test   %rax,%rax
je     1290 <send_msg_to_aggregator+0xf0>
mov    0x2de2(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1287 <send_msg_to_aggregator+0xe7>
mov    %eax,%ebp
mov    0x2dcb(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    (%rsp),%eax
mov    %eax,0xc(%rsp)
xor    %eax,%eax
call   10a0 <get_aggregator_port@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    12a1 <send_msg_to_aggregator+0x101>
lea    0xdbf(%rip),%rdi
call   1090 <inform_user@plt>
jmp    12a1 <send_msg_to_aggregator+0x101>
lea    0xd92(%rip),%rdi
jmp    1297 <send_msg_to_aggregator+0xf7>
lea    0xd69(%rip),%rdi
call   10b0 <debug_print@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.sockaddr = type { i32 }
@.str = [32 x i8] c"ERROR: Unknown server address!\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [31 x i8] c"ERROR: Failed to open socket!\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [38 x i8] c"ERROR: Failed to send to aggregator!\0A\00"
define i32 @send_msg_to_aggregator(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.hostent*
%9 = alloca %struct.in_addr
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_in
store i32* %0, i32** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 -1, i32* %10
store i32 1, i32* %11
store i32 0, i32* %12
%14 = call i32 @memset(%struct.sockaddr_in* %13, i32 0, i32 12)
%15 = load i8*, i8** %7
%16 = call %struct.hostent* @gethostbyname(i8* %15)
store %struct.hostent* %16, %struct.hostent** %8
%17 = load %struct.hostent*, %struct.hostent** %8
%18 = icmp ne %struct.hostent* %17, null
br i1 %18, label %26, label %19
19: ; preds = %3
%20 = load i8*, i8** %7
%21 = call i64 @inet_aton(i8* %20, %struct.in_addr* %9)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @debug_print(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %66
25: ; preds = %19
br label %33
26: ; preds = %3
%27 = load %struct.hostent*, %struct.hostent** %8
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to %struct.in_addr*
%31 = bitcast %struct.in_addr* %9 to i8*
%32 = bitcast %struct.in_addr* %30 to i8*
br label %33
33: ; preds = %26, %25
%34 = load i32, i32* @AF_INET
%35 = load i32, i32* @SOCK_DGRAM
%36 = call i32 @socket(i32 %34, i32 %35, i32 0)
store i32 %36, i32* %10
%37 = load i32, i32* %10
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @debug_print(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %66
41: ; preds = %33
%42 = load i32, i32* %10
%43 = load i32, i32* @SOL_SOCKET
%44 = load i32, i32* @SO_REUSEADDR
%45 = bitcast i32* %11 to i8*
%46 = call i32 @setsockopt(i32 %42, i32 %43, i32 %44, i8* %45, i32 4)
%47 = load i32, i32* @AF_INET
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 2
store i32 %47, i32* %48
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 1
%50 = bitcast %struct.in_addr* %49 to i8*
%51 = bitcast %struct.in_addr* %9 to i8*
%52 = call i32 (...) @get_aggregator_port()
%53 = call i32 @htons(i32 %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
store i32 %53, i32* %54
%55 = load i32, i32* %10
%56 = load i32*, i32** %5
%57 = load i32, i32* %6
%58 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%59 = call i32 @sendto(i32 %55, i32* %56, i32 %57, i32 0, %struct.sockaddr* %58, i32 12)
store i32 %59, i32* %12
%60 = load i32, i32* %12
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %41
%63 = call i32 @inform_user(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %64
64: ; preds = %62, %41
%65 = load i32, i32* %12
store i32 %65, i32* %4
br label %66
66: ; preds = %64, %39, %23
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [32 x i8] c"ERROR: Unknown server address!\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [31 x i8] c"ERROR: Failed to open socket!\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [38 x i8] c"ERROR: Failed to send to aggregator!\0A\00"
define i32 @send_msg_to_aggregator(i32* %0, i32 %1, i8* %2) {
%4 = alloca %struct.in_addr
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = bitcast %struct.in_addr* %4 to i8*
%8 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%9 = bitcast %struct.sockaddr_in* %6 to i8*
%10 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%11 = call %struct.hostent* @gethostbyname(i8* %2)
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = call i64 @inet_aton(i8* %2, %struct.in_addr* %4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = call i32 @debug_print(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %48
18: ; preds = %3
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%20 = load i64, i64* %19
%21 = inttoptr i64 %20 to i32*
%22 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %4, i64 0, i32 0
%23 = load i32, i32* %21
store i32 %23, i32* %22
br label %24
24: ; preds = %13, %18
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_DGRAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @debug_print(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %48
31: ; preds = %24
%32 = load i32, i32* @SOL_SOCKET
%33 = load i32, i32* @SO_REUSEADDR
%34 = call i32 @setsockopt(i32 %27, i32 %32, i32 %33, i8* %8, i32 4)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %4, i64 0, i32 0
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
%39 = load i32, i32* %37
store i32 %39, i32* %38
%40 = call i32 (...) @get_aggregator_port()
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
store i32 %41, i32* %42
%43 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%44 = call i32 @sendto(i32 %27, i32* %0, i32 %1, i32 0, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %31
%47 = call i32 @inform_user(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %31, %46, %29, %16
%49 = phi i32 [ -1, %29 ], [ -1, %16 ], [ %44, %46 ], [ %44, %31 ]
ret i32 %49
}