#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 scalar_t__ uint16_t;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct icmp {
  int icmp_type;
  int icmp_id;
  scalar_t__ icmp_cksum;
  scalar_t__ icmp_seq;
  scalar_t__ icmp_code;
};

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_TTL;
scalar_t__ htons(int);
scalar_t__ in_cksum(scalar_t__ *, int, int /*<<< orphan*/);
scalar_t__ sendto(int, struct icmp *, int, int /*<<< orphan*/,
                  struct sockaddr *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int icmp_send(int *sockfd, struct sockaddr_in *remote_address, int *ttl,
              int *_icmp_id, int *_icmp_seq) {
  struct icmp icmp_packet;
  icmp_packet.icmp_type = 8;
  icmp_packet.icmp_code = 0;
  icmp_packet.icmp_id = *_icmp_id;
  icmp_packet.icmp_seq = (uint16_t)htons(*_icmp_seq);
  icmp_packet.icmp_cksum = 0;
  icmp_packet.icmp_cksum = in_cksum((uint16_t *)&icmp_packet, 8, 0);
  if (setsockopt(*sockfd, IPPROTO_IP, IP_TTL, ttl, sizeof(int)))
    return -1;
  if (sendto(*sockfd, &icmp_packet, 8, 0, (struct sockaddr *)remote_address,
             sizeof(*remote_address)) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<icmp_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x8,-0x50(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1050 <htons@plt>
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
lea    -0x50(%rbp),%rdi
mov    $0x8,%esi
xor    %edx,%edx
call   1040 <in_cksum@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11dc <icmp_send+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <icmp_send+0xd9>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%r8
lea    -0x50(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
mov    $0x4,%r9d
call   1060 <sendto@plt>
cmp    $0x0,%rax
jge    1212 <icmp_send+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1219 <icmp_send+0xd9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<icmp_send>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
movl   $0x8,(%rsp)
movq   $0x0,0x18(%rsp)
mov    (%rcx),%eax
mov    %eax,0x4(%rsp)
mov    (%r8),%edi
call   1050 <htons@plt>
mov    %rax,0x10(%rsp)
movq   $0x0,0x8(%rsp)
mov    %rsp,%rdi
mov    $0x8,%esi
xor    %edx,%edx
call   1040 <in_cksum@plt>
mov    %rax,0x8(%rsp)
mov    (%rbx),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    11df <icmp_send+0x9f>
mov    (%rbx),%edi
mov    %rsp,%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0x4,%r9d
call   1060 <sendto@plt>
sar    $0x3f,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.icmp = type { i32, i32, i64, i64, i64 }
%struct.sockaddr = type { i32 }
@IPPROTO_IP = global i32 0
@IP_TTL = global i32 0
define i32 @icmp_send(i32* %0, %struct.sockaddr_in* %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.sockaddr_in*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca %struct.icmp
store i32* %0, i32** %7
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store i32* %4, i32** %11
%13 = getelementptr inbounds %struct.icmp, %struct.icmp* %12, i32 0, i32 0
store i32 8, i32* %13
%14 = getelementptr inbounds %struct.icmp, %struct.icmp* %12, i32 0, i32 4
store i64 0, i64* %14
%15 = load i32*, i32** %10
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.icmp, %struct.icmp* %12, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32*, i32** %11
%19 = load i32, i32* %18
%20 = call i64 @htons(i32 %19)
%21 = getelementptr inbounds %struct.icmp, %struct.icmp* %12, i32 0, i32 3
store i64 %20, i64* %21
%22 = getelementptr inbounds %struct.icmp, %struct.icmp* %12, i32 0, i32 2
store i64 0, i64* %22
%23 = bitcast %struct.icmp* %12 to i64*
%24 = call i64 @in_cksum(i64* %23, i32 8, i32 0)
%25 = getelementptr inbounds %struct.icmp, %struct.icmp* %12, i32 0, i32 2
store i64 %24, i64* %25
%26 = load i32*, i32** %7
%27 = load i32, i32* %26
%28 = load i32, i32* @IPPROTO_IP
%29 = load i32, i32* @IP_TTL
%30 = load i32*, i32** %9
%31 = call i64 @setsockopt(i32 %27, i32 %28, i32 %29, i32* %30, i32 4)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %5
store i32 -1, i32* %6
br label %43
34: ; preds = %5
%35 = load i32*, i32** %7
%36 = load i32, i32* %35
%37 = load %struct.sockaddr_in*, %struct.sockaddr_in** %8
%38 = bitcast %struct.sockaddr_in* %37 to %struct.sockaddr*
%39 = call i64 @sendto(i32 %36, %struct.icmp* %12, i32 8, i32 0, %struct.sockaddr* %38, i32 4)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %34
store i32 -1, i32* %6
br label %43
42: ; preds = %34
store i32 0, i32* %6
br label %43
43: ; preds = %42, %41, %33
%44 = load i32, i32* %6
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.icmp = type { i32, i32, i64, i64, i64 }
%struct.sockaddr = type { i32 }
@IPPROTO_IP = global i32 0
@IP_TTL = global i32 0
define i32 @icmp_send(i32* nocapture %0, %struct.sockaddr_in* %1, i32* %2, i32* nocapture %3, i32* nocapture %4) {
%6 = alloca %struct.icmp
%7 = bitcast %struct.icmp* %6 to i8*
%8 = getelementptr inbounds %struct.icmp, %struct.icmp* %6, i64 0, i32 0
store i32 8, i32* %8
%9 = getelementptr inbounds %struct.icmp, %struct.icmp* %6, i64 0, i32 4
store i64 0, i64* %9
%10 = load i32, i32* %3
%11 = getelementptr inbounds %struct.icmp, %struct.icmp* %6, i64 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* %4
%13 = tail call i64 @htons(i32 %12)
%14 = getelementptr inbounds %struct.icmp, %struct.icmp* %6, i64 0, i32 3
store i64 %13, i64* %14
%15 = getelementptr inbounds %struct.icmp, %struct.icmp* %6, i64 0, i32 2
store i64 0, i64* %15
%16 = bitcast %struct.icmp* %6 to i64*
%17 = call i64 @in_cksum(i64* %16, i32 8, i32 0)
store i64 %17, i64* %15
%18 = load i32, i32* %0
%19 = load i32, i32* @IPPROTO_IP
%20 = load i32, i32* @IP_TTL
%21 = call i64 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %2, i32 4)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %5
%24 = load i32, i32* %0
%25 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%26 = call i64 @sendto(i32 %24, %struct.icmp* %6, i32 8, i32 0, %struct.sockaddr* %25, i32 4)
%27 = ashr i64 %26, 63
%28 = trunc i64 %27 to i32
br label %29
29: ; preds = %23, %5
%30 = phi i32 [ -1, %5 ], [ %28, %23 ]
ret i32 %30
}