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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_pton(int /*<<< orphan*/, char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int sendto(int, char const *, int, int /*<<< orphan*/, struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char const *);
int connect_server(const char *caddr, uint16_t port, const char *msg) {
  int sockfd;
  int len;
  struct sockaddr_in addr;
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
    fprintf(stderr, "Socket creation failed: %s\n", strerror(errno));
    exit(1);
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  if (inet_pton(AF_INET, caddr, &addr.sin_addr) == 0) {
    fprintf(stderr, "Conversion of IP address %s failed.\n", caddr);
    exit(1);
  }
  printf("Message length: %lu\n", strlen(msg) + 1);
  len = sendto(sockfd, msg, strlen(msg) + 1, 0, (struct sockaddr *)&addr,
               sizeof(addr));
  printf("Sent %d bytes\n", len);
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    120e <connect_server+0x6e>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xe01(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1090 <inet_pton@plt>
cmp    $0x0,%rax
jne    1278 <connect_server+0xd8>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdb3(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%esi
add    $0x1,%esi
lea    0xdb4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
lea    -0x30(%rbp),%r8
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xd83(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e2a(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1271 <connect_server+0xd1>
mov    %eax,%ebp
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%r13),%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1090 <inet_pton@plt>
test   %rax,%rax
je     1296 <connect_server+0xf6>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%esi
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%edx
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    $0xc,%r9d
call   1070 <sendto@plt>
lea    0xdfd(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d58(%rip),%rax
mov    (%rax),%ebx
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd71(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
jmp    12a9 <connect_server+0x109>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
mov    %r14,%rdx
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Socket creation failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [37 x i8] c"Conversion of IP address %s failed.\0A\00"
@.str.2 = [21 x i8] c"Message length: %lu\0A\00"
@.str.3 = [15 x i8] c"Sent %d bytes\0A\00"
define i32 @connect_server(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_DGRAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %7
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %3
%21 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* %5
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %25, i32* %26
%27 = load i32, i32* @AF_INET
%28 = load i8*, i8** %4
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%30 = call i64 @inet_pton(i32 %27, i8* %28, i32* %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %20
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %4
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %20
%38 = load i8*, i8** %6
%39 = call i32 @strlen(i8* %38)
%40 = add nsw i32 %39, 1
%41 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %7
%43 = load i8*, i8** %6
%44 = load i8*, i8** %6
%45 = call i32 @strlen(i8* %44)
%46 = add nsw i32 %45, 1
%47 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%48 = call i32 @sendto(i32 %42, i8* %43, i32 %46, i32 0, %struct.sockaddr* %47, i32 12)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %7
ret i32 %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [28 x i8] c"Socket creation failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [37 x i8] c"Conversion of IP address %s failed.\0A\00"
@.str.2 = [21 x i8] c"Message length: %lu\0A\00"
@.str.3 = [15 x i8] c"Sent %d bytes\0A\00"
define i32 @connect_server(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %16
10: ; preds = %3
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %18, i32* %19
%20 = call i32 @htons(i32 %1)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%24 = call i64 @inet_pton(i32 %22, i8* %0, i32* %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %16
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %16
%31 = call i32 @strlen(i8* %2)
%32 = add nsw i32 %31, 1
%33 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = call i32 @strlen(i8* %2)
%35 = add nsw i32 %34, 1
%36 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%37 = call i32 @sendto(i32 %8, i8* %2, i32 %35, i32 0, %struct.sockaddr* %36, i32 12)
%38 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %37)
ret i32 %8
}