#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 tcphdr {
  int dummy;
};
struct iphdr {
  int /*<<< orphan*/ saddr;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct icmphdr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_ICMP;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ IP_HDRINCL;
int /*<<< orphan*/ SOCK_RAW;
int inet_ntoa(struct in_addr);
int /*<<< orphan*/ printf(char *, int, int);
int read(int, char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int fd;
  char buf[2000];
  struct iphdr *iph;
  struct tcphdr *tcph;
  struct icmphdr *icmph;
  struct in_addr dr;
  int isip = 1;
  int r;
  fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
  setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &isip, sizeof(int));
  while (1) {
    r = read(fd, buf, 2000);
    iph = (struct iphdr *)buf;
    dr.s_addr = iph->saddr;
    printf("ip->%d:%d\n", inet_ntoa(dr), r);
    if (r <= 0)
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x810,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x804(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
mov    0x2e4e(%rip),%rax
mov    (%rax),%edx
lea    -0x804(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x8(%rbp),%edi
lea    -0x7e0(%rbp),%rsi
mov    $0x7d0,%edx
call   1060 <read@plt>
mov    %eax,-0x808(%rbp)
lea    -0x7e0(%rbp),%rax
mov    %rax,-0x7e8(%rbp)
mov    -0x7e8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x800(%rbp)
mov    -0x800(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %eax,%esi
mov    -0x808(%rbp),%edx
lea    0xdf9(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x0,-0x808(%rbp)
jg     121e <main+0xce>
jmp    1223 <main+0xd3>
jmp    11b6 <main+0x66>
xor    %eax,%eax
add    $0x810,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x7e8,%rsp
movl   $0x1,0xc(%rsp)
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,%ebx
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%r14
lea    0xe49(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x7d0,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r15,%rdi
mov    %eax,%esi
mov    %ebp,%edx
call   1050 <printf@plt>
test   %ebp,%ebp
jg     11c0 <main+0x70>
xor    %eax,%eax
add    $0x7e8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i32 }
%struct.tcphdr = type { i32 }
%struct.icmphdr = type { i32 }
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_ICMP = global i32 0
@IPPROTO_IP = global i32 0
@IP_HDRINCL = global i32 0
@.str = [11 x i8] c"ip->%d:%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2000 x i8]
%4 = alloca %struct.iphdr*
%5 = alloca %struct.tcphdr*
%6 = alloca %struct.icmphdr*
%7 = alloca %struct.in_addr
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %8
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_RAW
%12 = load i32, i32* @IPPROTO_ICMP
%13 = call i32 @socket(i32 %10, i32 %11, i32 %12)
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = load i32, i32* @IPPROTO_IP
%16 = load i32, i32* @IP_HDRINCL
%17 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %8, i32 4)
br label %18
18: ; preds = %0, %36
%19 = load i32, i32* %2
%20 = getelementptr inbounds [2000 x i8], [2000 x i8]* %3, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 2000)
store i32 %21, i32* %9
%22 = getelementptr inbounds [2000 x i8], [2000 x i8]* %3, i64 0, i64 0
%23 = bitcast i8* %22 to %struct.iphdr*
store %struct.iphdr* %23, %struct.iphdr** %4
%24 = load %struct.iphdr*, %struct.iphdr** %4
%25 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %7, i32 0, i32 0
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %7, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @inet_ntoa(i32 %29)
%31 = load i32, i32* %9
%32 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %30, i32 %31)
%33 = load i32, i32* %9
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %18
br label %37
36: ; preds = %18
br label %18
37: ; preds = %35
ret i32 0
}

LLVM IR (-O2):
%struct.tcphdr = type { i32 }
%struct.icmphdr = type { i32 }
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@IPPROTO_ICMP = global i32 0
@IPPROTO_IP = global i32 0
@IP_HDRINCL = global i32 0
@.str = [11 x i8] c"ip->%d:%d\0A\00"
define i32 @main() {
%1 = alloca [2000 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [2000 x i8], [2000 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_RAW
%7 = load i32, i32* @IPPROTO_ICMP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = load i32, i32* @IPPROTO_IP
%10 = load i32, i32* @IP_HDRINCL
%11 = call i32 @setsockopt(i32 %8, i32 %9, i32 %10, i32* %2, i32 4)
%12 = bitcast [2000 x i8]* %1 to i32*
br label %13
13: ; preds = %13, %0
%14 = call i32 @read(i32 %8, i8* %3, i32 2000)
%15 = load i32, i32* %12
%16 = call i32 @inet_ntoa(i32 %15)
%17 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %14)
%18 = icmp slt i32 %14, 1
br i1 %18, label %19, label %13
19: ; preds = %13
ret i32 0
}