#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 */
typedef int uid_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ server_addr;
typedef int /*<<< orphan*/ client_addr;
typedef int /*<<< orphan*/ addr_buff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
unsigned int atoi(char *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ geteuid();
int getresuid(int *, int *, int *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned int);
char *inet_ntop(int /*<<< orphan*/, TYPE_1__ *, char *, int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ seteuid(int);
scalar_t__ setreuid(int, int);
int /*<<< orphan*/ sleep(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int listenfd, connfd;
  struct sockaddr_in client_addr, server_addr;
  socklen_t client_addr_len, server_addr_len;
  char addr_buff[256];
  unsigned int port_number, user_id;
  uid_t ruid, euid, suid;
  if (argc != 3) {
    fprintf(stderr, "Invocation: %s <PORT> <USERID>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  port_number = atoi(argv[1]);
  user_id = atoi(argv[2]);
  if (port_number < 1024) {
    if (geteuid() != 0) {
      fprintf(stderr, "You need root privileges to "
                      "bind to well-known port (<1024).\n");
      exit(EXIT_FAILURE);
    }
  }
  listenfd = socket(PF_INET, SOCK_STREAM, 0);
  if (listenfd == -1) {
    perror("socket()");
    exit(EXIT_FAILURE);
  }
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  server_addr.sin_port = htons(port_number);
  server_addr_len = sizeof(server_addr);
  if (getresuid(&ruid, &euid, &suid) == -1) {
    perror("getresgid()");
    exit(EXIT_FAILURE);
  }
  fprintf(stdout, "UID: %u, EUID: %u, SUID: %u\n", ruid, euid, suid);
  fprintf(stdout, "Binding to port %u...\n", port_number);
  if (bind(listenfd, (struct sockaddr *)&server_addr, server_addr_len) == -1) {
    perror("bind()");
    exit(EXIT_FAILURE);
  }
  seteuid((uid_t)user_id);
  if (setreuid((uid_t)user_id, (uid_t)user_id)) {
    perror("seteuid()");
    exit(EXIT_FAILURE);
  }
  if (getresuid(&ruid, &euid, &suid) == -1) {
    perror("getresgid()");
    exit(EXIT_FAILURE);
  }
  fprintf(stdout, "UID: %u, EUID: %u, SUID: %u\n", ruid, euid, suid);
  if (euid == 0 || suid == 0) {
    fprintf(stderr, "Run server as unprivileged user!\n");
    exit(EXIT_FAILURE);
  }
  if (listen(listenfd, 2) == -1) {
    perror("listen()");
    exit(EXIT_FAILURE);
  }
  fprintf(stdout, "Server is listening for incoming connection...\n");
  client_addr_len = sizeof(client_addr);
  connfd = accept(listenfd, (struct sockaddr *)&client_addr, &client_addr_len);
  if (connfd == -1) {
    perror("accept()");
    exit(EXIT_FAILURE);
  }
  fprintf(
      stdout, "TCP connection accepted from %s:%d\n",
      inet_ntop(AF_INET, &client_addr.sin_addr, addr_buff, sizeof(addr_buff)),
      ntohs(client_addr.sin_port));
  sleep(5);
  fprintf(stdout, "Closing connected socket (sending FIN to client)...\n");
  close(connfd);
  close(listenfd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     127f <main+0x4f>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd96(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1110 <atoi@plt>
mov    %eax,-0x144(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1110 <atoi@plt>
mov    %eax,-0x148(%rbp)
cmpl   $0x400,-0x144(%rbp)
jae    12f0 <main+0xc0>
mov    $0x0,%al
call   1070 <geteuid@plt>
cmp    $0x0,%rax
je     12eb <main+0xbb>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xd4a(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
jmp    12f0 <main+0xc0>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
mov    0x2cc8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1330 <main+0x100>
lea    0xd41(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x34(%rbp)
mov    -0x144(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x38(%rbp)
movl   $0xc,-0x40(%rbp)
lea    -0x14c(%rbp),%rdi
lea    -0x150(%rbp),%rsi
lea    -0x154(%rbp),%rdx
call   10a0 <getresuid@plt>
cmp    $0xffffffff,%eax
jne    13af <main+0x17f>
lea    0xccb(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2c10(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x2bfa(%rip),%rax
mov    (%rax),%edi
mov    -0x14c(%rbp),%edx
mov    -0x150(%rbp),%ecx
mov    -0x154(%rbp),%r8d
lea    0xca1(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2bd0(%rip),%rax
mov    (%rax),%edi
mov    -0x144(%rbp),%edx
lea    0xca1(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1428 <main+0x1f8>
lea    0xc92(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2b97(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    -0x148(%rbp),%edi
call   1150 <seteuid@plt>
mov    -0x148(%rbp),%edi
mov    -0x148(%rbp),%esi
call   10c0 <setreuid@plt>
cmp    $0x0,%rax
je     1468 <main+0x238>
lea    0xc59(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2b57(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
lea    -0x14c(%rbp),%rdi
lea    -0x150(%rbp),%rsi
lea    -0x154(%rbp),%rdx
call   10a0 <getresuid@plt>
cmp    $0xffffffff,%eax
jne    14a5 <main+0x275>
lea    0xbd5(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2b1a(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x2b04(%rip),%rax
mov    (%rax),%edi
mov    -0x14c(%rbp),%edx
mov    -0x150(%rbp),%ecx
mov    -0x154(%rbp),%r8d
lea    0xbab(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
cmpl   $0x0,-0x150(%rbp)
je     14e9 <main+0x2b9>
cmpl   $0x0,-0x154(%rbp)
jne    150e <main+0x2de>
mov    0x2ae0(%rip),%rax
mov    (%rax),%edi
lea    0xbbf(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2ab1(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    153e <main+0x30e>
lea    0xbaf(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2a81(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x2a6b(%rip),%rax
mov    (%rax),%edi
lea    0xb95(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1100 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1593 <main+0x363>
lea    0xb93(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2a2c(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x2a16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x164(%rbp)
mov    0x2a3f(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
lea    -0x140(%rbp),%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,-0x160(%rbp)
mov    -0x28(%rbp),%edi
call   10d0 <ntohs@plt>
mov    -0x164(%rbp),%edi
mov    -0x160(%rbp),%rdx
mov    %eax,%ecx
lea    0xb33(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x5,%edi
call   1130 <sleep@plt>
mov    0x29af(%rip),%rax
mov    (%rax),%edi
lea    0xb36(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    0x2980(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
mov    %rsi,%rbp
cmp    $0x3,%edi
jne    12a1 <main+0x71>
mov    0x8(%rbp),%rdi
call   1110 <atoi@plt>
mov    %eax,%ebx
mov    0x10(%rbp),%rdi
call   1110 <atoi@plt>
mov    %eax,%ebp
cmp    $0x3ff,%ebx
ja     1277 <main+0x47>
xor    %eax,%eax
call   1070 <geteuid@plt>
test   %rax,%rax
jne    1324 <main+0xf4>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
cmp    $0xffffffff,%eax
jne    12c1 <main+0x91>
lea    0xdc2(%rip),%rdi
jmp    1416 <main+0x1e6>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xd4b(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    141b <main+0x1eb>
mov    %eax,%r14d
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d0c(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x18(%rsp)
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
call   10a0 <getresuid@plt>
cmp    $0xffffffff,%eax
jne    1340 <main+0x110>
lea    0xd48(%rip),%rdi
jmp    1416 <main+0x1e6>
mov    0x2ca5(%rip),%rax
mov    (%rax),%edi
lea    0xcec(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    141b <main+0x1eb>
mov    0x2c69(%rip),%r12
mov    (%r12),%edi
mov    0x8(%rsp),%edx
mov    0x4(%rsp),%ecx
mov    (%rsp),%r8d
lea    0xd15(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    (%r12),%edi
lea    0xd20(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1399 <main+0x169>
lea    0xd10(%rip),%rdi
jmp    1416 <main+0x1e6>
mov    %ebp,%edi
call   1150 <seteuid@plt>
mov    %ebp,%edi
mov    %ebp,%esi
call   10c0 <setreuid@plt>
test   %rax,%rax
jne    140f <main+0x1df>
lea    0x8(%rsp),%rdi
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
call   10a0 <getresuid@plt>
cmp    $0xffffffff,%eax
je     1318 <main+0xe8>
mov    (%r12),%edi
mov    0x8(%rsp),%edx
mov    0x4(%rsp),%ecx
mov    (%rsp),%r8d
lea    0xc93(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
cmpl   $0x0,0x4(%rsp)
je     1429 <main+0x1f9>
cmpl   $0x0,(%rsp)
je     1429 <main+0x1f9>
mov    %r14d,%edi
mov    $0x2,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
jne    143e <main+0x20e>
lea    0xccd(%rip),%rdi
jmp    1416 <main+0x1e6>
lea    0xc98(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2b96(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x2ba0(%rip),%rax
mov    (%rax),%edi
lea    0xc7f(%rip),%rsi
jmp    1334 <main+0x104>
mov    (%r12),%edi
lea    0xc9a(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   1100 <accept@plt>
cmp    $0xffffffff,%eax
jne    1478 <main+0x248>
lea    0xc9d(%rip),%rdi
jmp    1416 <main+0x1e6>
mov    %eax,%ebx
mov    (%r12),%r15d
mov    0x0(%r13),%edi
lea    0x24(%rsp),%rsi
lea    0x30(%rsp),%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%rbp
mov    0x20(%rsp),%edi
call   10d0 <ntohs@plt>
lea    0xc73(%rip),%rsi
mov    %r15d,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x5,%edi
call   1130 <sleep@plt>
mov    (%r12),%edi
lea    0xc73(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    0x2abe(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"Invocation: %s <PORT> <USERID>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [62 x i8] c"You need root privileges to bind to well-known port (<1024).\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [12 x i8] c"getresgid()\00"
@stdout = global i32 0
@.str.4 = [29 x i8] c"UID: %u, EUID: %u, SUID: %u\0A\00"
@.str.5 = [23 x i8] c"Binding to port %u...\0A\00"
@.str.6 = [7 x i8] c"bind()\00"
@.str.7 = [10 x i8] c"seteuid()\00"
@.str.8 = [34 x i8] c"Run server as unprivileged user!\0A\00"
@.str.9 = [9 x i8] c"listen()\00"
@.str.10 = [48 x i8] c"Server is listening for incoming connection...\0A\00"
@.str.11 = [9 x i8] c"accept()\00"
@.str.12 = [36 x i8] c"TCP connection accepted from %s:%d\0A\00"
@.str.13 = [53 x i8] c"Closing connected socket (sending FIN to client)...\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
%12 = alloca [256 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 3
br i1 %19, label %20, label %28
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %13
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
store i32 %36, i32* %14
%37 = load i32, i32* %13
%38 = icmp ult i32 %37, 1024
br i1 %38, label %39, label %48
39: ; preds = %28
%40 = call i64 (...) @geteuid()
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %39
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %39
br label %48
48: ; preds = %47, %28
%49 = load i32, i32* @PF_INET
%50 = load i32, i32* @SOCK_STREAM
%51 = call i32 @socket(i32 %49, i32 %50, i32 0)
store i32 %51, i32* %6
%52 = load i32, i32* %6
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %58
54: ; preds = %48
%55 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %48
%59 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%60 = load i32, i32* @AF_INET
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %60, i32* %61
%62 = load i32, i32* @INADDR_ANY
%63 = call i32 @htonl(i32 %62)
%64 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%65 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %64, i32 0, i32 0
store i32 %63, i32* %65
%66 = load i32, i32* %13
%67 = call i32 @htons(i32 %66)
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %67, i32* %68
store i32 12, i32* %11
%69 = call i32 @getresuid(i32* %15, i32* %16, i32* %17)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %75
71: ; preds = %58
%72 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %58
%76 = load i32, i32* @stdout
%77 = load i32, i32* %15
%78 = load i32, i32* %16
%79 = load i32, i32* %17
%80 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %77, i32 %78, i32 %79)
%81 = load i32, i32* @stdout
%82 = load i32, i32* %13
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %82)
%84 = load i32, i32* %6
%85 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%86 = load i32, i32* %11
%87 = call i32 @bind(i32 %84, %struct.sockaddr* %85, i32 %86)
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %93
89: ; preds = %75
%90 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%91 = load i32, i32* @EXIT_FAILURE
%92 = call i32 @exit(i32 %91)
unreachable
93: ; preds = %75
%94 = load i32, i32* %14
%95 = call i32 @seteuid(i32 %94)
%96 = load i32, i32* %14
%97 = load i32, i32* %14
%98 = call i64 @setreuid(i32 %96, i32 %97)
%99 = icmp ne i64 %98, 0
br i1 %99, label %100, label %104
100: ; preds = %93
%101 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%102 = load i32, i32* @EXIT_FAILURE
%103 = call i32 @exit(i32 %102)
unreachable
104: ; preds = %93
%105 = call i32 @getresuid(i32* %15, i32* %16, i32* %17)
%106 = icmp eq i32 %105, -1
br i1 %106, label %107, label %111
107: ; preds = %104
%108 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%109 = load i32, i32* @EXIT_FAILURE
%110 = call i32 @exit(i32 %109)
unreachable
111: ; preds = %104
%112 = load i32, i32* @stdout
%113 = load i32, i32* %15
%114 = load i32, i32* %16
%115 = load i32, i32* %17
%116 = call i32 (i32, i8*, ...) @fprintf(i32 %112, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %113, i32 %114, i32 %115)
%117 = load i32, i32* %16
%118 = icmp eq i32 %117, 0
br i1 %118, label %122, label %119
119: ; preds = %111
%120 = load i32, i32* %17
%121 = icmp eq i32 %120, 0
br i1 %121, label %122, label %127
122: ; preds = %119, %111
%123 = load i32, i32* @stderr
%124 = call i32 (i32, i8*, ...) @fprintf(i32 %123, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
%125 = load i32, i32* @EXIT_FAILURE
%126 = call i32 @exit(i32 %125)
unreachable
127: ; preds = %119
%128 = load i32, i32* %6
%129 = call i32 @listen(i32 %128, i32 2)
%130 = icmp eq i32 %129, -1
br i1 %130, label %131, label %135
131: ; preds = %127
%132 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%133 = load i32, i32* @EXIT_FAILURE
%134 = call i32 @exit(i32 %133)
unreachable
135: ; preds = %127
%136 = load i32, i32* @stdout
%137 = call i32 (i32, i8*, ...) @fprintf(i32 %136, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.10, i64 0, i64 0))
store i32 12, i32* %10
%138 = load i32, i32* %6
%139 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%140 = call i32 @accept(i32 %138, %struct.sockaddr* %139, i32* %10)
store i32 %140, i32* %7
%141 = load i32, i32* %7
%142 = icmp eq i32 %141, -1
br i1 %142, label %143, label %147
143: ; preds = %135
%144 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0))
%145 = load i32, i32* @EXIT_FAILURE
%146 = call i32 @exit(i32 %145)
unreachable
147: ; preds = %135
%148 = load i32, i32* @stdout
%149 = load i32, i32* @AF_INET
%150 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%151 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%152 = call i8* @inet_ntop(i32 %149, %struct.TYPE_2__* %150, i8* %151, i32 256)
%153 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%154 = load i32, i32* %153
%155 = call i32 @ntohs(i32 %154)
%156 = call i32 (i32, i8*, ...) @fprintf(i32 %148, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.12, i64 0, i64 0), i8* %152, i32 %155)
%157 = call i32 @sleep(i32 5)
%158 = load i32, i32* @stdout
%159 = call i32 (i32, i8*, ...) @fprintf(i32 %158, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.13, i64 0, i64 0))
%160 = load i32, i32* %7
%161 = call i32 @close(i32 %160)
%162 = load i32, i32* %6
%163 = call i32 @close(i32 %162)
%164 = load i32, i32* @EXIT_SUCCESS
%165 = call i32 @exit(i32 %164)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [32 x i8] c"Invocation: %s <PORT> <USERID>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [62 x i8] c"You need root privileges to bind to well-known port (<1024).\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [12 x i8] c"getresgid()\00"
@stdout = global i32 0
@.str.4 = [29 x i8] c"UID: %u, EUID: %u, SUID: %u\0A\00"
@.str.5 = [23 x i8] c"Binding to port %u...\0A\00"
@.str.6 = [7 x i8] c"bind()\00"
@.str.7 = [10 x i8] c"seteuid()\00"
@.str.8 = [34 x i8] c"Run server as unprivileged user!\0A\00"
@.str.9 = [9 x i8] c"listen()\00"
@.str.10 = [48 x i8] c"Server is listening for incoming connection...\0A\00"
@.str.11 = [9 x i8] c"accept()\00"
@.str.12 = [36 x i8] c"TCP connection accepted from %s:%d\0A\00"
@.str.13 = [53 x i8] c"Closing connected socket (sending FIN to client)...\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = bitcast %struct.sockaddr_in* %3 to i8*
%11 = bitcast %struct.sockaddr_in* %4 to i8*
%12 = bitcast i32* %5 to i8*
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%14 = bitcast i32* %7 to i8*
%15 = bitcast i32* %8 to i8*
%16 = bitcast i32* %9 to i8*
%17 = icmp eq i32 %0, 3
br i1 %17, label %24, label %18
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %1
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call i32 @atoi(i8* %26)
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = tail call i32 @atoi(i8* %29)
%31 = icmp ult i32 %27, 1024
br i1 %31, label %32, label %40
32: ; preds = %24
%33 = tail call i64 (...) @geteuid()
%34 = icmp eq i64 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %32, %24
%41 = load i32, i32* @PF_INET
%42 = load i32, i32* @SOCK_STREAM
%43 = tail call i32 @socket(i32 %41, i32 %42, i32 0)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = tail call i32 @exit(i32 %47)
unreachable
49: ; preds = %40
%50 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%51 = load i32, i32* @AF_INET
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %51, i32* %52
%53 = load i32, i32* @INADDR_ANY
%54 = call i32 @htonl(i32 %53)
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %54, i32* %55
%56 = call i32 @htons(i32 %27)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %56, i32* %57
%58 = call i32 @getresuid(i32* %7, i32* %8, i32* %9)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %64
60: ; preds = %49
%61 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %49
%65 = load i32, i32* @stdout
%66 = load i32, i32* %7
%67 = load i32, i32* %8
%68 = load i32, i32* %9
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %66, i32 %67, i32 %68)
%70 = load i32, i32* @stdout
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%72 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%73 = call i32 @bind(i32 %43, %struct.sockaddr* %72, i32 12)
%74 = icmp eq i32 %73, -1
br i1 %74, label %75, label %79
75: ; preds = %64
%76 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%77 = load i32, i32* @EXIT_FAILURE
%78 = call i32 @exit(i32 %77)
unreachable
79: ; preds = %64
%80 = call i32 @seteuid(i32 %30)
%81 = call i64 @setreuid(i32 %30, i32 %30)
%82 = icmp eq i64 %81, 0
br i1 %82, label %87, label %83
83: ; preds = %79
%84 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%85 = load i32, i32* @EXIT_FAILURE
%86 = call i32 @exit(i32 %85)
unreachable
87: ; preds = %79
%88 = call i32 @getresuid(i32* %7, i32* %8, i32* %9)
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %94
90: ; preds = %87
%91 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%92 = load i32, i32* @EXIT_FAILURE
%93 = call i32 @exit(i32 %92)
unreachable
94: ; preds = %87
%95 = load i32, i32* @stdout
%96 = load i32, i32* %7
%97 = load i32, i32* %8
%98 = load i32, i32* %9
%99 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %96, i32 %97, i32 %98)
%100 = load i32, i32* %8
%101 = icmp eq i32 %100, 0
%102 = load i32, i32* %9
%103 = icmp eq i32 %102, 0
%104 = select i1 %101, i1 true, i1 %103
br i1 %104, label %105, label %110
105: ; preds = %94
%106 = load i32, i32* @stderr
%107 = call i32 (i32, i8*, ...) @fprintf(i32 %106, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.8, i64 0, i64 0))
%108 = load i32, i32* @EXIT_FAILURE
%109 = call i32 @exit(i32 %108)
unreachable
110: ; preds = %94
%111 = call i32 @listen(i32 %43, i32 2)
%112 = icmp eq i32 %111, -1
br i1 %112, label %113, label %117
113: ; preds = %110
%114 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%115 = load i32, i32* @EXIT_FAILURE
%116 = call i32 @exit(i32 %115)
unreachable
117: ; preds = %110
%118 = load i32, i32* @stdout
%119 = call i32 (i32, i8*, ...) @fprintf(i32 %118, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.10, i64 0, i64 0))
store i32 12, i32* %5
%120 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%121 = call i32 @accept(i32 %43, %struct.sockaddr* %120, i32* %5)
%122 = icmp eq i32 %121, -1
br i1 %122, label %123, label %127
123: ; preds = %117
%124 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0))
%125 = load i32, i32* @EXIT_FAILURE
%126 = call i32 @exit(i32 %125)
unreachable
127: ; preds = %117
%128 = load i32, i32* @stdout
%129 = load i32, i32* @AF_INET
%130 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%131 = call i8* @inet_ntop(i32 %129, %struct.TYPE_2__* %130, i8* %13, i32 256)
%132 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%133 = load i32, i32* %132
%134 = call i32 @ntohs(i32 %133)
%135 = call i32 (i32, i8*, ...) @fprintf(i32 %128, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.12, i64 0, i64 0), i8* %131, i32 %134)
%136 = call i32 @sleep(i32 5)
%137 = load i32, i32* @stdout
%138 = call i32 (i32, i8*, ...) @fprintf(i32 %137, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.13, i64 0, i64 0))
%139 = call i32 @close(i32 %121)
%140 = call i32 @close(i32 %43)
%141 = load i32, i32* @EXIT_SUCCESS
%142 = call i32 @exit(i32 %141)
unreachable
}