#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 int /*<<< orphan*/ uid_t;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ errx(int, char *);
int /*<<< orphan*/ geteuid();
scalar_t__ getuid();
scalar_t__ seteuid(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int get_socket_priv(int type) {
  uid_t olduid;
  int sock;
  if (getuid() != 0)
    errx(-1, "get_sock_priv: running without real uid 0");
  olduid = geteuid();
  if (seteuid(0) < 0)
    err(-1, "get_sock_priv: seteuid(0)");
  sock = socket(PF_INET, type, 0);
  if (seteuid(olduid) < 0)
    err(-1, "get_sock_priv: seteuid(%d)", olduid);
  return (sock);
}


x86 assembly (clang, -O0):
<get_socket_priv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
je     118d <get_socket_priv+0x2d>
mov    $0xffffffff,%edi
lea    0xe78(%rip),%rsi
call   1040 <errx@plt>
mov    $0x0,%al
call   1050 <geteuid@plt>
mov    %eax,-0x8(%rbp)
xor    %edi,%edi
call   1080 <seteuid@plt>
cmp    $0x0,%rax
jge    11bb <get_socket_priv+0x5b>
mov    $0xffffffff,%edi
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <seteuid@plt>
cmp    $0x0,%rax
jge    11f9 <get_socket_priv+0x99>
mov    -0x8(%rbp),%edx
mov    $0xffffffff,%edi
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_socket_priv>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <getuid@plt>
test   %rax,%rax
je     1182 <get_socket_priv+0x22>
lea    0xe88(%rip),%rsi
mov    $0xffffffff,%edi
call   1040 <errx@plt>
xor    %eax,%eax
call   1050 <geteuid@plt>
mov    %eax,%ebp
xor    %edi,%edi
call   1080 <seteuid@plt>
test   %rax,%rax
jns    11aa <get_socket_priv+0x4a>
lea    0xe8c(%rip),%rsi
mov    $0xffffffff,%edi
xor    %eax,%eax
call   1060 <err@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1080 <seteuid@plt>
test   %rax,%rax
jns    11df <get_socket_priv+0x7f>
lea    0xe73(%rip),%rsi
mov    $0xffffffff,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <err@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"get_sock_priv: running without real uid 0\00"
@.str.1 = [26 x i8] c"get_sock_priv: seteuid(0)\00"
@PF_INET = global i32 0
@.str.2 = [27 x i8] c"get_sock_priv: seteuid(%d)\00"
define i32 @get_socket_priv(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i64 (...) @getuid()
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @errx(i32 -1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = call i32 (...) @geteuid()
store i32 %10, i32* %3
%11 = call i64 @seteuid(i32 0)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 (i32, i8*, ...) @err(i32 -1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = load i32, i32* @PF_INET
%17 = load i32, i32* %2
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %4
%19 = load i32, i32* %3
%20 = call i64 @seteuid(i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %15
%23 = load i32, i32* %3
%24 = call i32 (i32, i8*, ...) @err(i32 -1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %15
%26 = load i32, i32* %4
ret i32 %26
}

LLVM IR (-O2):
@.str = [42 x i8] c"get_sock_priv: running without real uid 0\00"
@.str.1 = [26 x i8] c"get_sock_priv: seteuid(0)\00"
@PF_INET = global i32 0
@.str.2 = [27 x i8] c"get_sock_priv: seteuid(%d)\00"
define i32 @get_socket_priv(i32 %0) {
%2 = tail call i64 (...) @getuid()
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 @errx(i32 -1, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i32 (...) @geteuid()
%8 = tail call i64 @seteuid(i32 0)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 (i32, i8*, ...) @err(i32 -1, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = load i32, i32* @PF_INET
%14 = tail call i32 @socket(i32 %13, i32 %0, i32 0)
%15 = tail call i64 @seteuid(i32 %7)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 (i32, i8*, ...) @err(i32 -1, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %19
19: ; preds = %17, %12
ret i32 %14
}