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

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *);
scalar_t__ geteuid();
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ test_addr(int, int, int);
int /*<<< orphan*/ test_if(int, int, int);
int /*<<< orphan*/ test_loop(int, int, int);
int /*<<< orphan*/ test_ttl(int, int, int);
int /*<<< orphan*/ test_udp();
int main(int argc, char *argv[]) {
  int raw_sock, tcp_sock, udp_sock;
  if (geteuid() != 0)
    errx(-1, "FAIL: root privilege required");
  raw_sock = socket(PF_INET, SOCK_RAW, 0);
  if (raw_sock == -1)
    err(-1, "FAIL: socket(PF_INET, SOCK_RAW)");
  tcp_sock = socket(PF_INET, SOCK_STREAM, 0);
  if (raw_sock == -1)
    err(-1, "FAIL: socket(PF_INET, SOCK_STREAM)");
  udp_sock = socket(PF_INET, SOCK_DGRAM, 0);
  if (raw_sock == -1)
    err(-1, "FAIL: socket(PF_INET, SOCK_DGRAM)");
  test_ttl(raw_sock, tcp_sock, udp_sock);
  test_loop(raw_sock, tcp_sock, udp_sock);
  test_if(raw_sock, tcp_sock, udp_sock);
  test_addr(raw_sock, tcp_sock, udp_sock);
  close(udp_sock);
  close(tcp_sock);
  close(raw_sock);
  test_udp();
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <geteuid@plt>
cmp    $0x0,%rax
je     11d8 <main+0x38>
mov    $0xffffffff,%edi
lea    0xe2d(%rip),%rsi
call   1040 <errx@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    120f <main+0x6f>
mov    $0xffffffff,%edi
lea    0xe14(%rip),%rsi
call   1090 <err@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1246 <main+0xa6>
mov    $0xffffffff,%edi
lea    0xdfd(%rip),%rsi
call   1090 <err@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    127d <main+0xdd>
mov    $0xffffffff,%edi
lea    0xde9(%rip),%rsi
call   1090 <err@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   10b0 <test_ttl@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1070 <test_loop@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1080 <test_if@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   10a0 <test_addr@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   1030 <test_udp@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1050 <geteuid@plt>
test   %rax,%rax
je     11c1 <main+0x21>
lea    0xe49(%rip),%rsi
mov    $0xffffffff,%edi
call   1040 <errx@plt>
mov    0x2e00(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     120c <main+0x6c>
mov    (%rbx),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
jmp    1267 <main+0xc7>
lea    0xe0b(%rip),%rsi
mov    $0xffffffff,%edi
call   1090 <err@plt>
mov    (%rbx),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebp
lea    0xe06(%rip),%rsi
mov    $0xffffffff,%edi
call   1090 <err@plt>
mov    (%rbx),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
lea    0xe04(%rip),%rsi
mov    $0xffffffff,%edi
call   1090 <err@plt>
mov    %r14d,%edi
mov    %ebp,%esi
mov    %ebx,%edx
call   10b0 <test_ttl@plt>
mov    %r14d,%edi
mov    %ebp,%esi
mov    %ebx,%edx
call   1070 <test_loop@plt>
mov    %r14d,%edi
mov    %ebp,%esi
mov    %ebx,%edx
call   1080 <test_if@plt>
mov    %r14d,%edi
mov    %ebp,%esi
mov    %ebx,%edx
call   10a0 <test_addr@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1030 <test_udp@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"FAIL: root privilege required\00"
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@.str.1 = [32 x i8] c"FAIL: socket(PF_INET, SOCK_RAW)\00"
@SOCK_STREAM = global i32 0
@.str.2 = [35 x i8] c"FAIL: socket(PF_INET, SOCK_STREAM)\00"
@SOCK_DGRAM = global i32 0
@.str.3 = [34 x i8] c"FAIL: socket(PF_INET, SOCK_DGRAM)\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 (...) @geteuid()
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i32, i32* @PF_INET
%15 = load i32, i32* @SOCK_RAW
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @err(i32 -1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %13
%22 = load i32, i32* @PF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %7
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @err(i32 -1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = load i32, i32* @PF_INET
%31 = load i32, i32* @SOCK_DGRAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %8
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @err(i32 -1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %29
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = call i32 @test_ttl(i32 %38, i32 %39, i32 %40)
%42 = load i32, i32* %6
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = call i32 @test_loop(i32 %42, i32 %43, i32 %44)
%46 = load i32, i32* %6
%47 = load i32, i32* %7
%48 = load i32, i32* %8
%49 = call i32 @test_if(i32 %46, i32 %47, i32 %48)
%50 = load i32, i32* %6
%51 = load i32, i32* %7
%52 = load i32, i32* %8
%53 = call i32 @test_addr(i32 %50, i32 %51, i32 %52)
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %7
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
%60 = call i32 (...) @test_udp()
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"FAIL: root privilege required\00"
@PF_INET = global i32 0
@SOCK_RAW = global i32 0
@.str.1 = [32 x i8] c"FAIL: socket(PF_INET, SOCK_RAW)\00"
@SOCK_STREAM = global i32 0
@.str.2 = [35 x i8] c"FAIL: socket(PF_INET, SOCK_STREAM)\00"
@SOCK_DGRAM = global i32 0
@.str.3 = [34 x i8] c"FAIL: socket(PF_INET, SOCK_DGRAM)\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @geteuid()
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @errx(i32 -1, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = load i32, i32* @PF_INET
%9 = load i32, i32* @SOCK_RAW
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %19, label %12
12: ; preds = %7
%13 = load i32, i32* @PF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = load i32, i32* @PF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
br label %29
19: ; preds = %7
%20 = tail call i32 @err(i32 -1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = tail call i32 @err(i32 -1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @PF_INET
%26 = load i32, i32* @SOCK_DGRAM
%27 = tail call i32 @socket(i32 %25, i32 %26, i32 0)
%28 = tail call i32 @err(i32 -1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %12, %19
%30 = phi i32 [ %27, %19 ], [ %18, %12 ]
%31 = phi i32 [ %23, %19 ], [ %15, %12 ]
%32 = tail call i32 @test_ttl(i32 %10, i32 %31, i32 %30)
%33 = tail call i32 @test_loop(i32 %10, i32 %31, i32 %30)
%34 = tail call i32 @test_if(i32 %10, i32 %31, i32 %30)
%35 = tail call i32 @test_addr(i32 %10, i32 %31, i32 %30)
%36 = tail call i32 @close(i32 %30)
%37 = tail call i32 @close(i32 %31)
%38 = tail call i32 @close(i32 %10)
%39 = tail call i32 (...) @test_udp()
ret i32 0
}