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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
char *prepare_message(int *, char *, int);
int /*<<< orphan*/ printf(char *);
scalar_t__ sendto(int, char *, int, int /*<<< orphan*/, struct sockaddr const *,
                  int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int send_message(struct in_addr inaddr, int port, char *metric_name,
                 int metric_value) {
  struct sockaddr_in addr;
  char *buffer;
  int len = 0, s;
  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin_addr = inaddr;
  addr.sin_port = htons(port);
  addr.sin_family = AF_INET;
  s = socket(PF_INET, SOCK_DGRAM, 0);
  if (s == -1) {
    return 1;
  }
  buffer = prepare_message(&len, metric_name, metric_value);
  if (sendto(s, buffer, len, 0, (const struct sockaddr *)&addr,
             sizeof(struct sockaddr_in)) == 0) {
    printf("sendto failed\n");
    return 2;
  }
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<send_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x34(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    11eb <send_message+0x7b>
movl   $0x1,-0x4(%rbp)
jmp    124c <send_message+0xdc>
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
lea    -0x34(%rbp),%rdi
call   1080 <prepare_message@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
lea    -0x28(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
cmp    $0x0,%rax
jne    123c <send_message+0xcc>
lea    0xdd5(%rip),%rdi
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    124c <send_message+0xdc>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_message>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%ebp
mov    %edi,%ebx
movl   $0x0,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    %ebx,0x10(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1218 <send_message+0xa8>
mov    %eax,%ebp
lea    0x4(%rsp),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1080 <prepare_message@plt>
mov    %rax,%r14
mov    0x4(%rsp),%edx
xor    %ebx,%ebx
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    %rax,%rsi
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
test   %rax,%rax
je     121f <send_message+0xaf>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    1230 <send_message+0xc0>
mov    $0x1,%ebx
jmp    1230 <send_message+0xc0>
lea    0xdda(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, i32, %struct.in_addr }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [15 x i8] c"sendto failed\0A\00"
define i32 @send_message(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.in_addr
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %6, i32 0, i32 0
store i32 %0, i32* %14
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %12
%15 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
%17 = bitcast %struct.in_addr* %16 to i8*
%18 = bitcast %struct.in_addr* %6 to i8*
%19 = load i32, i32* %7
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* @PF_INET
%25 = load i32, i32* @SOCK_DGRAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %13
%27 = load i32, i32* %13
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %4
store i32 1, i32* %5
br label %45
30: ; preds = %4
%31 = load i8*, i8** %8
%32 = load i32, i32* %9
%33 = call i8* @prepare_message(i32* %12, i8* %31, i32 %32)
store i8* %33, i8** %11
%34 = load i32, i32* %13
%35 = load i8*, i8** %11
%36 = load i32, i32* %12
%37 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%38 = call i64 @sendto(i32 %34, i8* %35, i32 %36, i32 0, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %30
%41 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %5
br label %45
42: ; preds = %30
%43 = load i8*, i8** %11
%44 = call i32 @free(i8* %43)
store i32 0, i32* %5
br label %45
45: ; preds = %42, %40, %29
%46 = load i32, i32* %5
ret i32 %46
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.in_addr }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@str = [14 x i8] c"sendto failed\00"
define i32 @send_message(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%9 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2, i32 0
store i32 %0, i32* %10
%11 = call i32 @htons(i32 %1)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @PF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %29, label %19
19: ; preds = %4
%20 = call i8* @prepare_message(i32* %6, i8* %2, i32 %3)
%21 = load i32, i32* %6
%22 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%23 = call i64 @sendto(i32 %17, i8* %20, i32 %21, i32 0, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %19
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %29
27: ; preds = %19
%28 = call i32 @free(i8* %20)
br label %29
29: ; preds = %4, %27, %25
%30 = phi i32 [ 2, %25 ], [ 0, %27 ], [ 1, %4 ]
ret i32 %30
}