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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int SOCK_DGRAM;
short atoi(char *);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
scalar_t__ sendto(int, char *, int /*<<< orphan*/, int /*<<< orphan*/,
                  struct sockaddr *, int);
int /*<<< orphan*/ strlen(char *);
int buf_2_server(int sockfd, char *serverIP, char *port, int mode,
                 char *snd_buf) {
  if (mode == SOCK_DGRAM) {
    struct sockaddr_in servaddr;
    short s_port = atoi(port);
    memset((char *)&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(s_port);
    servaddr.sin_addr.s_addr = inet_addr(serverIP);
    printf("UDP dest: %s , port: %d\n", serverIP, s_port);
    if (sendto(sockfd, snd_buf, strlen(snd_buf), 0,
               (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
      perror("sendto failed");
      return (-1);
    }
    printf("buf_2_server:: serverIP: %s:%s, buf: %s\n", serverIP, port,
           snd_buf);
    return (1);
  }
  return (-1);
}


x86 assembly (clang, -O0):
<buf_2_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2e3c(%rip),%rcx
cmp    (%rcx),%eax
jne    127c <buf_2_server+0xfc>
mov    -0x18(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %ax,-0x3a(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movswl -0x3a(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rsi
movswl -0x3a(%rbp),%edx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
cmp    $0x0,%rax
jge    1256 <buf_2_server+0xd6>
lea    0xdd4(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1283 <buf_2_server+0x103>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1283 <buf_2_server+0x103>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_2_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    0x2e48(%rip),%rdx
mov    $0xffffffff,%eax
cmp    %ecx,(%rdx)
jne    1247 <buf_2_server+0xc7>
mov    %r8,%r15
mov    %rsi,%rbx
mov    %edi,%r13d
mov    %r14,%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movswl %bp,%ebp
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x8(%rsp)
lea    0xe06(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %r13d,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    %r12,%r8
mov    $0xc,%r9d
call   1070 <sendto@plt>
test   %rax,%rax
js     1256 <buf_2_server+0xd6>
lea    0xdf5(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbc(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    1247 <buf_2_server+0xc7>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
@.str = [25 x i8] c"UDP dest: %s , port: %d\0A\00"
@.str.1 = [14 x i8] c"sendto failed\00"
@.str.2 = [41 x i8] c"buf_2_server:: serverIP: %s:%s, buf: %s\0A\00"
define i32 @buf_2_server(i32 %0, i8* %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.sockaddr_in
%13 = alloca i16
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%14 = load i32, i32* %10
%15 = load i32, i32* @SOCK_DGRAM
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %49
17: ; preds = %5
%18 = load i8*, i8** %9
%19 = call signext i16 @atoi(i8* %18)
store i16 %19, i16* %13
%20 = bitcast %struct.sockaddr_in* %12 to i8*
%21 = call i32 @memset(i8* %20, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i16, i16* %13
%25 = call i32 @htons(i16 signext %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %25, i32* %26
%27 = load i8*, i8** %8
%28 = call i32 @inet_addr(i8* %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i8*, i8** %8
%32 = load i16, i16* %13
%33 = sext i16 %32 to i32
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %31, i32 %33)
%35 = load i32, i32* %7
%36 = load i8*, i8** %11
%37 = load i8*, i8** %11
%38 = call i32 @strlen(i8* %37)
%39 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%40 = call i64 @sendto(i32 %35, i8* %36, i32 %38, i32 0, %struct.sockaddr* %39, i32 12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %17
%43 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %50
44: ; preds = %17
%45 = load i8*, i8** %8
%46 = load i8*, i8** %9
%47 = load i8*, i8** %11
%48 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %45, i8* %46, i8* %47)
store i32 1, i32* %6
br label %50
49: ; preds = %5
store i32 -1, i32* %6
br label %50
50: ; preds = %49, %44, %42
%51 = load i32, i32* %6
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@SOCK_DGRAM = global i32 0
@AF_INET = global i32 0
@.str = [25 x i8] c"UDP dest: %s , port: %d\0A\00"
@.str.1 = [14 x i8] c"sendto failed\00"
@.str.2 = [41 x i8] c"buf_2_server:: serverIP: %s:%s, buf: %s\0A\00"
define i32 @buf_2_server(i32 %0, i8* %1, i8* %2, i32 %3, i8* %4) {
%6 = alloca %struct.sockaddr_in
%7 = load i32, i32* @SOCK_DGRAM
%8 = icmp eq i32 %7, %3
br i1 %8, label %9, label %31
9: ; preds = %5
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = tail call signext i16 @atoi(i8* %2)
%12 = call i32 @memset(i8* %10, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i16 signext %11)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @inet_addr(i8* %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = sext i16 %11 to i32
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %19)
%21 = call i32 @strlen(i8* %4)
%22 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%23 = call i64 @sendto(i32 %0, i8* %4, i32 %21, i32 0, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %9
%26 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %29
27: ; preds = %9
%28 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %2, i8* %4)
br label %29
29: ; preds = %27, %25
%30 = phi i32 [ -1, %25 ], [ 1, %27 ]
br label %31
31: ; preds = %5, %29
%32 = phi i32 [ %30, %29 ], [ -1, %5 ]
ret i32 %32
}