#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*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, ...);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int send_message(char *address, char *port, char *msg) {
  int n;
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in server_addr;
  memset(&server_addr, '0', sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(atoi(port));
  inet_pton(AF_INET, address, &server_addr.sin_addr);
  if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr))) {
    printf("DEBUG: send_message(): Connection to %s:%s failed\n", address,
           port);
    return -1;
  }
  printf("Sending message: %s to %s:%s\n", msg, address, port);
  if ((n = send(sock, msg, strlen(msg), 0)) <= 0) {
    printf("DEBUG: send_message(): Message %s cannot be sent\n", msg);
    close(sock);
    return -1;
  }
  close(sock);
  return n;
}


x86 assembly (clang, -O0):
<send_message>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x28(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1090 <inet_pton@plt>
mov    -0x28(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
je     1255 <send_message+0xb5>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d6 <send_message+0x136>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jg     12c8 <send_message+0x128>
mov    -0x20(%rbp),%rsi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d6 <send_message+0x136>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_message>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r13
mov    0x2e2a(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r15d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    %r12,%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%rbp),%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1090 <inet_pton@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %rax,%rax
je     1243 <send_message+0xa3>
lea    0xdd1(%rip),%rdi
mov    %r13,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%ebx
jmp    127f <send_message+0xdf>
lea    0xde9(%rip),%rdi
mov    %r14,%rsi
mov    %r13,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
test   %eax,%eax
jle    1290 <send_message+0xf0>
mov    %eax,%ebx
mov    %r15d,%edi
call   1080 <close@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdba(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15d,%edi
call   1080 <close@plt>
jmp    123c <send_message+0x9c>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [51 x i8] c"DEBUG: send_message(): Connection to %s:%s failed\0A\00"
@.str.1 = [30 x i8] c"Sending message: %s to %s:%s\0A\00"
@.str.2 = [50 x i8] c"DEBUG: send_message(): Message %s cannot be sent\0A\00"
define i32 @send_message(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %9
%14 = call i32 @memset(%struct.sockaddr_in* %10, i32 48, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i8*, i8** %6
%18 = call i32 @atoi(i8* %17)
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = load i8*, i8** %5
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%24 = call i32 @inet_pton(i32 %21, i8* %22, i32* %23)
%25 = load i32, i32* %9
%26 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%27 = call i64 @connect(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %3
%30 = load i8*, i8** %5
%31 = load i8*, i8** %6
%32 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %30, i8* %31)
store i32 -1, i32* %4
br label %53
33: ; preds = %3
%34 = load i8*, i8** %7
%35 = load i8*, i8** %5
%36 = load i8*, i8** %6
%37 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %35, i8* %36)
%38 = load i32, i32* %9
%39 = load i8*, i8** %7
%40 = load i8*, i8** %7
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @send(i32 %38, i8* %39, i32 %41, i32 0)
store i32 %42, i32* %8
%43 = icmp sle i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %33
%45 = load i8*, i8** %7
%46 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = load i32, i32* %9
%48 = call i32 @close(i32 %47)
store i32 -1, i32* %4
br label %53
49: ; preds = %33
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* %8
store i32 %52, i32* %4
br label %53
53: ; preds = %49, %44, %29
%54 = load i32, i32* %4
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [51 x i8] c"DEBUG: send_message(): Connection to %s:%s failed\0A\00"
@.str.1 = [30 x i8] c"Sending message: %s to %s:%s\0A\00"
@.str.2 = [50 x i8] c"DEBUG: send_message(): Message %s cannot be sent\0A\00"
define i32 @send_message(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = call i32 @memset(%struct.sockaddr_in* %4, i32 48, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %10, i32* %11
%12 = call i32 @atoi(i8* %1)
%13 = call i32 @htons(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%17 = call i32 @inet_pton(i32 %15, i8* %0, i32* %16)
%18 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%19 = call i64 @connect(i32 %7, %struct.sockaddr* %18, i32 12)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %3
%22 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %33
23: ; preds = %3
%24 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %2, i8* %0, i8* %1)
%25 = call i32 @strlen(i8* %2)
%26 = call i32 @send(i32 %7, i8* %2, i32 %25, i32 0)
%27 = icmp slt i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%30 = call i32 @close(i32 %7)
br label %33
31: ; preds = %23
%32 = call i32 @close(i32 %7)
br label %33
33: ; preds = %31, %28, %21
%34 = phi i32 [ -1, %21 ], [ -1, %28 ], [ %26, %31 ]
ret i32 %34
}