#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_6__ TYPE_3__;
typedef struct TYPE_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct TYPE_5__ {
  void *sequence;
  void *id;
};
struct TYPE_6__ {
  TYPE_2__ echo;
};
struct icmphdr {
  int /*<<< orphan*/ checksum;
  TYPE_3__ un;
  scalar_t__ code;
  int /*<<< orphan*/ type;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ ICMP_ECHO;
int /*<<< orphan*/ IPPROTO_ICMP;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ checksum(void *, size_t);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(void *);
unsigned short getpid();
scalar_t__ getuid();
void *htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ printf(char *, ...);
int sendto(int, void *, size_t, int /*<<< orphan*/, struct sockaddr *, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
size_t strlen(char const *);
int main() {
  if (getuid() != 0) {
    printf("Please launch me as root.\n");
    return EXIT_FAILURE;
  }
  int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  if (sock < 0) {
    printf("Error when creating socket.\n");
    return EXIT_FAILURE;
  }
  struct sockaddr_in destination;
  destination.sin_family = AF_INET;
  destination.sin_addr.s_addr = inet_addr("127.0.0.1");
  const char *msg = "Ceci est un message top secret.";
  size_t payload_size = strlen(msg);
  size_t packet_length = sizeof(struct icmphdr) + payload_size;
  void *packet = malloc(packet_length);
  struct icmphdr *icmp = packet;
  char *payload = (packet + sizeof(struct icmphdr));
  icmp->type = ICMP_ECHO;
  icmp->code = 0;
  icmp->un.echo.id = htons(getpid());
  memcpy(payload, msg, strlen(msg));
  unsigned short i;
  for (i = 0; i <= 1000; ++i) {
    icmp->un.echo.sequence = htons(i);
    icmp->checksum = checksum(packet, packet_length);
    int ret = sendto(sock, packet, packet_length, 0,
                     (struct sockaddr *)&destination, sizeof destination);
    printf("Nous avons envoyé %d octets.\n", ret);
    sleep(1);
  }
  free(packet);
  close(sock);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <getuid@plt>
cmp    $0x0,%rax
je     121f <main+0x3f>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13d0 <main+0x1f0>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
mov    0x2da0(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    126b <main+0x8b>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13d0 <main+0x1f0>
mov    0x2d76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xdba(%rip),%rdi
call   10c0 <inet_addr@plt>
mov    %eax,-0x10(%rbp)
lea    0xdb5(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x28,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   10e0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
add    $0x28,%rax
mov    %rax,-0x40(%rbp)
mov    0x2cf6(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,0x20(%rax)
mov    -0x38(%rbp),%rax
movq   $0x0,0x18(%rax)
mov    $0x0,%al
call   1040 <getpid@plt>
movzwl %ax,%edi
call   1070 <htons@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
call   10d0 <memcpy@plt>
movw   $0x0,-0x42(%rbp)
movzwl -0x42(%rbp),%eax
cmp    $0x3e8,%eax
jg     13b3 <main+0x1d3>
movzwl -0x42(%rbp),%edi
call   1070 <htons@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <checksum@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x10(%rbp),%r8
xor    %ecx,%ecx
mov    $0x8,%r9d
call   10a0 <sendto@plt>
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%esi
lea    0xcd1(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    $0x1,%edi
call   10f0 <sleep@plt>
mov    -0x42(%rbp),%ax
add    $0x1,%ax
mov    %ax,-0x42(%rbp)
jmp    1330 <main+0x150>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%edi
call   10b0 <close@plt>
mov    0x2bed(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1060 <getuid@plt>
test   %rax,%rax
je     1210 <main+0x30>
lea    0xe66(%rip),%rdi
call   1040 <puts@plt>
mov    0x2db5(%rip),%rax
jmp    1320 <main+0x140>
mov    0x2dd1(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
test   %eax,%eax
js     132f <main+0x14f>
mov    %eax,%r12d
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
lea    0xdb8(%rip),%rdi
call   10c0 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    $0x47,%edi
call   10e0 <malloc@plt>
mov    %rax,%rbx
lea    0x28(%rax),%r14
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rbx)
movq   $0x0,0x18(%rbx)
xor    %eax,%eax
call   1050 <getpid@plt>
movzwl %ax,%edi
call   1070 <htons@plt>
mov    %rax,0x10(%rbx)
lea    0xd7a(%rip),%rsi
mov    $0x1f,%edx
mov    %r14,%rdi
call   10d0 <memcpy@plt>
lea    0x8(%rsp),%r14
lea    0xd81(%rip),%r15
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzwl %bp,%edi
call   1070 <htons@plt>
mov    %rax,0x8(%rbx)
mov    $0x47,%esi
mov    %rbx,%rdi
call   1090 <checksum@plt>
mov    %eax,(%rbx)
mov    $0x47,%edx
mov    %r12d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0x8,%r9d
call   10a0 <sendto@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%edi
call   10f0 <sleep@plt>
add    $0x1,%ebp
movzwl %bp,%eax
cmp    $0x3e9,%eax
jne    12b0 <main+0xd0>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r12d,%edi
call   10b0 <close@plt>
mov    0x2c98(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd13(%rip),%rdi
jmp    11ff <main+0x1f>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { i32 }
%struct.icmphdr = type { i32, %struct.TYPE_6__, i64, i32 }
%struct.TYPE_6__ = type { %struct.TYPE_5__ }
%struct.TYPE_5__ = type { i8*, i8* }
%struct.sockaddr = type { i32 }
@.str = [27 x i8] c"Please launch me as root.\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_ICMP = global i32 0
@.str.1 = [29 x i8] c"Error when creating socket.\0A\00"
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [32 x i8] c"Ceci est un message top secret.\00"
@ICMP_ECHO = global i32 0
@.str.4 = [31 x i8] c"Nous avons envoy\C3\A9 %d octets.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
%8 = alloca %struct.icmphdr*
%9 = alloca i8*
%10 = alloca i16
%11 = alloca i32
store i32 0, i32* %1
%12 = call i64 (...) @getuid()
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %1
br label %92
17: ; preds = %0
%18 = load i32, i32* @AF_INET
%19 = load i32, i32* @SOCK_RAW
%20 = load i32, i32* @IPPROTO_ICMP
%21 = call i32 @socket(i32 %18, i32 %19, i32 %20)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
store i32 %26, i32* %1
br label %92
27: ; preds = %17
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %28, i32* %29
%30 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %31, i32 0, i32 0
store i32 %30, i32* %32
store i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8** %4
%33 = load i8*, i8** %4
%34 = call i64 @strlen(i8* %33)
store i64 %34, i64* %5
%35 = load i64, i64* %5
%36 = add i64 40, %35
store i64 %36, i64* %6
%37 = load i64, i64* %6
%38 = call i8* @malloc(i64 %37)
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = bitcast i8* %39 to %struct.icmphdr*
store %struct.icmphdr* %40, %struct.icmphdr** %8
%41 = load i8*, i8** %7
%42 = getelementptr i8, i8* %41, i64 40
store i8* %42, i8** %9
%43 = load i32, i32* @ICMP_ECHO
%44 = load %struct.icmphdr*, %struct.icmphdr** %8
%45 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %44, i32 0, i32 3
store i32 %43, i32* %45
%46 = load %struct.icmphdr*, %struct.icmphdr** %8
%47 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %46, i32 0, i32 2
store i64 0, i64* %47
%48 = call zeroext i16 (...) @getpid()
%49 = call i8* @htons(i16 zeroext %48)
%50 = load %struct.icmphdr*, %struct.icmphdr** %8
%51 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %50, i32 0, i32 1
%52 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %51, i32 0, i32 0
%53 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %52, i32 0, i32 1
store i8* %49, i8** %53
%54 = load i8*, i8** %9
%55 = load i8*, i8** %4
%56 = load i8*, i8** %4
%57 = call i64 @strlen(i8* %56)
%58 = call i32 @memcpy(i8* %54, i8* %55, i64 %57)
store i16 0, i16* %10
br label %59
59: ; preds = %83, %27
%60 = load i16, i16* %10
%61 = zext i16 %60 to i32
%62 = icmp sle i32 %61, 1000
br i1 %62, label %63, label %86
63: ; preds = %59
%64 = load i16, i16* %10
%65 = call i8* @htons(i16 zeroext %64)
%66 = load %struct.icmphdr*, %struct.icmphdr** %8
%67 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %66, i32 0, i32 1
%68 = getelementptr inbounds %struct.TYPE_6__, %struct.TYPE_6__* %67, i32 0, i32 0
%69 = getelementptr inbounds %struct.TYPE_5__, %struct.TYPE_5__* %68, i32 0, i32 0
store i8* %65, i8** %69
%70 = load i8*, i8** %7
%71 = load i64, i64* %6
%72 = call i32 @checksum(i8* %70, i64 %71)
%73 = load %struct.icmphdr*, %struct.icmphdr** %8
%74 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %73, i32 0, i32 0
store i32 %72, i32* %74
%75 = load i32, i32* %2
%76 = load i8*, i8** %7
%77 = load i64, i64* %6
%78 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%79 = call i32 @sendto(i32 %75, i8* %76, i64 %77, i32 0, %struct.sockaddr* %78, i32 8)
store i32 %79, i32* %11
%80 = load i32, i32* %11
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %80)
%82 = call i32 @sleep(i32 1)
br label %83
83: ; preds = %63
%84 = load i16, i16* %10
%85 = add i16 %84, 1
store i16 %85, i16* %10
br label %59
86: ; preds = %59
%87 = load i8*, i8** %7
%88 = call i32 @free(i8* %87)
%89 = load i32, i32* %2
%90 = call i32 @close(i32 %89)
%91 = load i32, i32* @EXIT_SUCCESS
store i32 %91, i32* %1
br label %92
92: ; preds = %86, %24, %14
%93 = load i32, i32* %1
ret i32 %93
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { i32 }
%struct.icmphdr = type { i32, %struct.TYPE_6__, i64, i32 }
%struct.TYPE_6__ = type { %struct.TYPE_5__ }
%struct.TYPE_5__ = type { i8*, i8* }
%struct.sockaddr = type { i32 }
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_ICMP = global i32 0
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [32 x i8] c"Ceci est un message top secret.\00"
@ICMP_ECHO = global i32 0
@.str.4 = [31 x i8] c"Nous avons envoy\C3\A9 %d octets.\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [28 x i8] c"Error when creating socket.\00"
@str.5 = [26 x i8] c"Please launch me as root.\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = tail call i64 (...) @getuid()
%3 = icmp eq i64 %2, 0
br i1 %3, label %7, label %4
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.5, i64 0, i64 0))
%6 = load i32, i32* @EXIT_FAILURE
br label %48
7: ; preds = %0
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_RAW
%10 = load i32, i32* @IPPROTO_ICMP
%11 = tail call i32 @socket(i32 %8, i32 %9, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
br label %48
16: ; preds = %7
%17 = bitcast %struct.sockaddr_in* %1 to i8*
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %18, i32* %19
%20 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %20, i32* %21
%22 = tail call dereferenceable_or_null(71) i8* @malloc(i64 71)
%23 = bitcast i8* %22 to %struct.icmphdr*
%24 = getelementptr i8, i8* %22, i64 40
%25 = load i32, i32* @ICMP_ECHO
%26 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %23, i64 0, i32 3
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %23, i64 0, i32 2
store i64 0, i64* %27
%28 = tail call zeroext i16 (...) @getpid()
%29 = tail call i8* @htons(i16 zeroext %28)
%30 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %23, i64 0, i32 1, i32 0, i32 1
store i8* %29, i8** %30
%31 = tail call i32 @memcpy(i8* %24, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 31)
%32 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %23, i64 0, i32 1, i32 0, i32 0
%33 = getelementptr inbounds %struct.icmphdr, %struct.icmphdr* %23, i64 0, i32 0
%34 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
br label %35
35: ; preds = %16, %35
%36 = phi i16 [ 0, %16 ], [ %42, %35 ]
%37 = call i8* @htons(i16 zeroext %36)
store i8* %37, i8** %32
%38 = call i32 @checksum(i8* %22, i64 71)
store i32 %38, i32* %33
%39 = call i32 @sendto(i32 %11, i8* %22, i64 71, i32 0, %struct.sockaddr* %34, i32 8)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %39)
%41 = call i32 @sleep(i32 1)
%42 = add nuw nsw i16 %36, 1
%43 = icmp eq i16 %42, 1001
br i1 %43, label %44, label %35
44: ; preds = %35
%45 = call i32 @free(i8* %22)
%46 = call i32 @close(i32 %11)
%47 = load i32, i32* @EXIT_SUCCESS
br label %48
48: ; preds = %13, %44, %4
%49 = phi i32 [ %6, %4 ], [ %15, %13 ], [ %47, %44 ]
ret i32 %49
}