#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_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ member_0;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ dup2(int, int);
int /*<<< orphan*/ execve(char *, char **, char **);
int /*<<< orphan*/ getgid();
int /*<<< orphan*/ getuid();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ setgid(int /*<<< orphan*/);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[], char *env[]) {
  int s;
  struct sockaddr_in a = {0};
  a.sin_family = AF_INET;
  a.sin_addr.s_addr = INADDR_ANY;
  a.sin_port = htons(53);
  do {
    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
      break;
    if (bind(s, (struct sockaddr *)&a, sizeof a))
      break;
    setgid(getgid());
    setuid(getuid());
    if (dup2(s, 3) < 0)
      break;
    execve(argv[1], argv + 1, env);
  } while (0);
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x35,%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1219 <main+0x79>
jmp    128a <main+0xea>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     1239 <main+0x99>
jmp    128a <main+0xea>
mov    $0x0,%al
call   1080 <getgid@plt>
mov    %eax,%edi
call   10a0 <setgid@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,%edi
call   10b0 <setuid@plt>
mov    -0x1c(%rbp),%edi
mov    $0x3,%esi
call   1050 <dup2@plt>
cmp    $0x0,%rax
jge    1271 <main+0xd1>
jmp    128a <main+0xea>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
mov    -0x18(%rbp),%rdx
call   1070 <execve@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
movl   $0x0,0xc(%rsp)
mov    0x2e3b(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    $0x35,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    0x0(%rbp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     123b <main+0xab>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1080 <bind@plt>
test   %rax,%rax
jne    123b <main+0xab>
xor    %eax,%eax
call   1070 <getgid@plt>
mov    %eax,%edi
call   1090 <setgid@plt>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%edi
call   10a0 <setuid@plt>
mov    %ebp,%edi
mov    $0x3,%esi
call   1050 <dup2@plt>
test   %rax,%rax
js     123b <main+0xab>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <execve@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%10 = bitcast %struct.sockaddr_in* %9 to i8*
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @INADDR_ANY
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = call i32 @htons(i32 53)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %16, i32* %17
br label %18
18: ; preds = %3
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_DGRAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %47
24: ; preds = %18
%25 = load i32, i32* %8
%26 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%27 = call i64 @bind(i32 %25, %struct.sockaddr* %26, i32 16)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
br label %47
30: ; preds = %24
%31 = call i32 (...) @getgid()
%32 = call i32 @setgid(i32 %31)
%33 = call i32 (...) @getuid()
%34 = call i32 @setuid(i32 %33)
%35 = load i32, i32* %8
%36 = call i64 @dup2(i32 %35, i32 3)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %30
br label %47
39: ; preds = %30
%40 = load i8**, i8*** %6
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = load i8**, i8*** %6
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8**, i8*** %7
%46 = call i32 @execve(i8* %42, i8** %44, i8** %45)
br label %47
47: ; preds = %39, %38, %29, %23
ret i32 1
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_DGRAM = global i32 0
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
store i32 0, i32* %6
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @INADDR_ANY
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %9, i32* %10
%11 = tail call i32 @htons(i32 53)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %32, label %17
17: ; preds = %3
%18 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%19 = call i64 @bind(i32 %15, %struct.sockaddr* %18, i32 16)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = call i32 (...) @getgid()
%23 = call i32 @setgid(i32 %22)
%24 = call i32 (...) @getuid()
%25 = call i32 @setuid(i32 %24)
%26 = call i64 @dup2(i32 %15, i32 3)
%27 = icmp slt i64 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %21
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @execve(i8* %30, i8** %29, i8** %2)
br label %32
32: ; preds = %21, %17, %3, %28
ret i32 1
}