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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int send_statsd_gauge(const char *metricName) {
  struct sockaddr_in si_other;
  int s, slen = sizeof(si_other);
  char buf[128];
  if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    return -1;
  }
  memset((char *)&si_other, 0, sizeof(si_other));
  si_other.sin_family = AF_INET;
  si_other.sin_port = htons(8125);
  if (inet_aton("127.0.0.1", &si_other.sin_addr) == 0) {
    fprintf(stderr, "inet_aton() failed for address '%s'\n", "127.0.0.1");
    return -1;
  }
  snprintf(buf, sizeof(buf), "%s:1|g", metricName);
  if (sendto(s, buf, strlen(buf), 0, (struct sockaddr *)&si_other, slen) ==
      -1) {
    return -1;
  }
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<send_statsd_gauge>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xc,-0x28(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    11de <send_statsd_gauge+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <send_statsd_gauge+0x13a>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1fbd,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rsi
lea    0xdee(%rip),%rdi
call   10a0 <inet_aton@plt>
cmp    $0x0,%rax
jne    1249 <send_statsd_gauge+0xb9>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
lea    0xdc8(%rip),%rdx
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <send_statsd_gauge+0x13a>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x80,%esi
lea    0xdcf(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0xbc(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0xb8(%rbp)
lea    -0xb0(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0xbc(%rbp),%edi
mov    -0xb8(%rbp),%rsi
mov    %eax,%edx
lea    -0x20(%rbp),%r8
mov    -0x28(%rbp),%r9d
xor    %ecx,%ecx
call   1070 <sendto@plt>
cmp    $0xffffffff,%eax
jne    12bb <send_statsd_gauge+0x12b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <send_statsd_gauge+0x13a>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_statsd_gauge>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    %rdi,%r14
mov    0x2e3f(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     1275 <send_statsd_gauge+0xe5>
mov    %eax,%ebp
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0x1fbd,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe00(%rip),%rdi
mov    %r12,%rsi
call   10a0 <inet_aton@plt>
test   %rax,%rax
je     1259 <send_statsd_gauge+0xc9>
lea    0xe1b(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rsp,%r8
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
cmp    $0xffffffff,%eax
je     1275 <send_statsd_gauge+0xe5>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %r15d,%r15d
jmp    1275 <send_statsd_gauge+0xe5>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
lea    0xd90(%rip),%rdx
call   1090 <fprintf@plt>
mov    %r15d,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@IPPROTO_UDP = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@stderr = global i32 0
@.str.1 = [37 x i8] c"inet_aton() failed for address '%s'\0A\00"
@.str.2 = [7 x i8] c"%s:1|g\00"
define i32 @send_statsd_gauge(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca [128 x i8]
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = load i32, i32* @IPPROTO_UDP
%9 = tail call i32 @socket(i32 %6, i32 %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %31, label %11
11: ; preds = %1
%12 = call i32 @memset(i8* %4, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 8125)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%18 = call i64 @inet_aton(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %31
23: ; preds = %11
%24 = call i32 @snprintf(i8* %5, i32 128, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%25 = call i32 @strlen(i8* %5)
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i32 @sendto(i32 %9, i8* %5, i32 %25, i32 0, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i32 %27, -1
br i1 %28, label %31, label %29
29: ; preds = %23
%30 = call i32 @close(i32 %9)
br label %31
31: ; preds = %23, %1, %29, %20
%32 = phi i32 [ -1, %20 ], [ 0, %29 ], [ -1, %1 ], [ -1, %23 ]
ret i32 %32
}