#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 {
  void *sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
struct hostent *gethostbyname(char *);
void *htons(int);
int /*<<< orphan*/ inet_aton(char const *, int /*<<< orphan*/ *);
int sendto(int, char const *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr const *, unsigned int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strlen(char const *);
int sendToLog(const char *buf, const char *ip, int port) {
  int sockfd, n;
  unsigned int length;
  struct sockaddr_in server, from;
  struct hostent *hp;
  server.sin_family = AF_INET;
  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd < 0)
    error("ERROR opening UDP socket");
  if (0 == strcmp(ip, "default")) {
    hp = gethostbyname("localhost");
    if (hp == 0)
      error("Unknown host");
    bcopy((char *)hp->h_addr, (char *)&server.sin_addr, hp->h_length);
  } else {
    inet_aton(ip, &server.sin_addr.s_addr);
  }
  if (port == -1)
    server.sin_port = htons(port);
  else
    server.sin_port = htons(9999);
  length = sizeof(struct sockaddr_in);
  n = sendto(sockfd, buf, strlen(buf), 0, (const struct sockaddr *)&server,
             length);
  if (n < 0)
    error("Sendto");
  close(sockfd);
  return 1;
}


x86 assembly (clang, -O0):
<sendToLog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f1 <sendToLog+0x51>
lea    0xe14(%rip),%rdi
call   10b0 <error@plt>
mov    -0x10(%rbp),%rdi
lea    0xe1d(%rip),%rsi
call   1070 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1256 <sendToLog+0xb6>
lea    0xe0b(%rip),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1236 <sendToLog+0x96>
lea    0xdfa(%rip),%rdi
call   10b0 <error@plt>
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
add    $0x8,%rsi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   10a0 <bcopy@plt>
jmp    1267 <sendToLog+0xc7>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
add    $0x8,%rsi
call   1090 <inet_aton@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    1282 <sendToLog+0xe2>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %rax,-0x30(%rbp)
jmp    1290 <sendToLog+0xf0>
mov    $0x270f,%edi
call   1040 <htons@plt>
mov    %rax,-0x30(%rbp)
movl   $0x10,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
lea    -0x30(%rbp),%r8
mov    -0x20(%rbp),%r9d
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12df <sendToLog+0x13f>
lea    0xd5e(%rip),%rdi
call   10b0 <error@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    $0x1,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendToLog>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%rbp
mov    %rdi,%r14
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x14(%rsp)
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e2 <sendToLog+0x42>
lea    0xe23(%rip),%rdi
call   10b0 <error@plt>
lea    0xe30(%rip),%rsi
mov    %rbp,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1205 <sendToLog+0x65>
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
call   1090 <inet_aton@plt>
jmp    1236 <sendToLog+0x96>
lea    0xe15(%rip),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1225 <sendToLog+0x85>
lea    0xe0b(%rip),%rdi
call   10b0 <error@plt>
mov    0x8(%rbp),%rsi
lea    0x10(%rsp),%rdi
mov    0x0(%rbp),%edx
call   10a0 <memmove@plt>
cmp    $0xffffffff,%r15d
mov    $0x270f,%edi
cmove  %r15d,%edi
call   1040 <htons@plt>
mov    %rax,0x8(%rsp)
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0x10,%r9d
call   1050 <sendto@plt>
test   %eax,%eax
jns    127e <sendToLog+0xde>
lea    0xdbf(%rip),%rdi
call   10b0 <error@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [25 x i8] c"ERROR opening UDP socket\00"
@.str.1 = [8 x i8] c"default\00"
@.str.2 = [10 x i8] c"localhost\00"
@.str.3 = [13 x i8] c"Unknown host\00"
@.str.4 = [7 x i8] c"Sendto\00"
define i32 @sendToLog(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.hostent*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %3
%21 = call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %3
%23 = load i8*, i8** %5
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 0, %24
br i1 %25, label %26, label %43
26: ; preds = %22
%27 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store %struct.hostent* %27, %struct.hostent** %12
%28 = load %struct.hostent*, %struct.hostent** %12
%29 = icmp eq %struct.hostent* %28, null
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %26
%33 = load %struct.hostent*, %struct.hostent** %12
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to i8*
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%38 = bitcast %struct.TYPE_2__* %37 to i8*
%39 = load %struct.hostent*, %struct.hostent** %12
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @bcopy(i8* %36, i8* %38, i32 %41)
br label %48
43: ; preds = %22
%44 = load i8*, i8** %5
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%46 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %45, i32 0, i32 0
%47 = call i32 @inet_aton(i8* %44, i32* %46)
br label %48
48: ; preds = %43, %32
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i32, i32* %6
%53 = call i8* @htons(i32 %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i8* %53, i8** %54
br label %58
55: ; preds = %48
%56 = call i8* @htons(i32 9999)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i8* %56, i8** %57
br label %58
58: ; preds = %55, %51
store i32 16, i32* %9
%59 = load i32, i32* %7
%60 = load i8*, i8** %4
%61 = load i8*, i8** %4
%62 = call i32 @strlen(i8* %61)
%63 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%64 = load i32, i32* %9
%65 = call i32 @sendto(i32 %59, i8* %60, i32 %62, i32 0, %struct.sockaddr* %63, i32 %64)
store i32 %65, i32* %8
%66 = load i32, i32* %8
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %58
%69 = call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %70
70: ; preds = %68, %58
%71 = load i32, i32* %7
%72 = call i32 @close(i32 %71)
ret i32 1
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i8*, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [25 x i8] c"ERROR opening UDP socket\00"
@.str.1 = [8 x i8] c"default\00"
@.str.2 = [10 x i8] c"localhost\00"
@.str.3 = [13 x i8] c"Unknown host\00"
@.str.4 = [7 x i8] c"Sendto\00"
define i32 @sendToLog(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %6, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %3
%14 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq %struct.hostent* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i64 0, i32 1
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to i8*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%26 = bitcast %struct.TYPE_2__* %25 to i8*
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i64 0, i32 0
%28 = load i32, i32* %27
br label %32
29: ; preds = %13
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%31 = call i32 @inet_aton(i8* %1, i32* %30)
br label %32
32: ; preds = %29, %21
%33 = icmp eq i32 %2, -1
%34 = select i1 %33, i32 -1, i32 9999
%35 = call i8* @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i8* %35, i8** %36
%37 = call i32 @strlen(i8* %0)
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = call i32 @sendto(i32 %9, i8* %0, i32 %37, i32 0, %struct.sockaddr* %38, i32 16)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = call i32 @error(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %32
%44 = call i32 @close(i32 %9)
ret i32 1
}