#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 protoent {
  int p_proto;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_RAW;
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int create_icmp_socket(void) {
  struct protoent *proto;
  int sock;
  proto = getprotobyname("icmp");
  if ((sock = socket(AF_INET, SOCK_RAW, (proto ? proto->p_proto : 1))) < 0) {
    printf("NSD Ping: open socket error\n");
  }
  setuid(getuid());
  return sock;
}


x86 assembly (clang, -O0):
<create_icmp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea1(%rip),%rdi
call   1050 <getprotobyname@plt>
mov    %rax,-0x8(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1199 <create_icmp_socket+0x49>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    11a6 <create_icmp_socket+0x56>
mov    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11a6 <create_icmp_socket+0x56>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11cc <create_icmp_socket+0x7c>
lea    0xe3e(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,%edi
call   1060 <setuid@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_icmp_socket>:
push   %rbx
lea    0xea8(%rip),%rdi
call   1050 <getprotobyname@plt>
mov    0x2e84(%rip),%rcx
mov    (%rcx),%edi
mov    0x2e73(%rip),%rcx
mov    (%rcx),%esi
test   %rax,%rax
je     1178 <create_icmp_socket+0x28>
mov    (%rax),%edx
jmp    117d <create_icmp_socket+0x2d>
mov    $0x1,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1194 <create_icmp_socket+0x44>
lea    0xe76(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%edi
call   1060 <setuid@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.protoent = type { i32 }
@.str = [5 x i8] c"icmp\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@.str.1 = [29 x i8] c"NSD Ping: open socket error\0A\00"
define i32 @create_icmp_socket() {
%1 = alloca %struct.protoent*
%2 = alloca i32
%3 = call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store %struct.protoent* %3, %struct.protoent** %1
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_RAW
%6 = load %struct.protoent*, %struct.protoent** %1
%7 = icmp ne %struct.protoent* %6, null
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load %struct.protoent*, %struct.protoent** %1
%10 = getelementptr inbounds %struct.protoent, %struct.protoent* %9, i32 0, i32 0
%11 = load i32, i32* %10
br label %13
12: ; preds = %0
br label %13
13: ; preds = %12, %8
%14 = phi i32 [ %11, %8 ], [ 1, %12 ]
%15 = call i32 @socket(i32 %4, i32 %5, i32 %14)
store i32 %15, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %13
%20 = call i32 (...) @getuid()
%21 = call i32 @setuid(i32 %20)
%22 = load i32, i32* %2
ret i32 %22
}

LLVM IR (-O2):
%struct.protoent = type { i32 }
@.str = [5 x i8] c"icmp\00"
@AF_INET = global i32 0
@SOCK_RAW = global i32 0
@str = [28 x i8] c"NSD Ping: open socket error\00"
define i32 @create_icmp_socket() {
%1 = tail call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_RAW
%4 = icmp eq %struct.protoent* %1, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = getelementptr inbounds %struct.protoent, %struct.protoent* %1, i64 0, i32 0
%7 = load i32, i32* %6
br label %8
8: ; preds = %0, %5
%9 = phi i32 [ %7, %5 ], [ 1, %0 ]
%10 = tail call i32 @socket(i32 %2, i32 %3, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %14
14: ; preds = %12, %8
%15 = tail call i32 (...) @getuid()
%16 = tail call i32 @setuid(i32 %15)
ret i32 %10
}