#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*/ getuid();
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*/ setuid(int /*<<< orphan*/);
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;
  uid_t ruid, euid, suid;
  if (argc != 2) {
    fprintf(stderr, "Invocation: %s <PORT>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  port_number = atoi(argv[1]);
  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);
  }
  setuid(getuid());
  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    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-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   10a0 <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)
cmpl   $0x400,-0x144(%rbp)
jae    12dd <main+0xad>
mov    $0x0,%al
call   1080 <geteuid@plt>
cmp    $0x0,%rax
je     12d8 <main+0xa8>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
jmp    12dd <main+0xad>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1150 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    131d <main+0xed>
lea    0xd4b(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2cb4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2ca0(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x34(%rbp)
mov    -0x144(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x38(%rbp)
movl   $0xc,-0x40(%rbp)
lea    -0x148(%rbp),%rdi
lea    -0x14c(%rbp),%rsi
lea    -0x150(%rbp),%rdx
call   10b0 <getresuid@plt>
cmp    $0xffffffff,%eax
jne    139c <main+0x16c>
lea    0xcd5(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2c23(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x2c0d(%rip),%rax
mov    (%rax),%edi
mov    -0x148(%rbp),%edx
mov    -0x14c(%rbp),%ecx
mov    -0x150(%rbp),%r8d
lea    0xcab(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2be3(%rip),%rax
mov    (%rax),%edi
mov    -0x144(%rbp),%edx
lea    0xcab(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1415 <main+0x1e5>
lea    0xc9c(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2baa(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%edi
call   1130 <setuid@plt>
lea    -0x148(%rbp),%rdi
lea    -0x14c(%rbp),%rsi
lea    -0x150(%rbp),%rdx
call   10b0 <getresuid@plt>
cmp    $0xffffffff,%eax
jne    1460 <main+0x230>
lea    0xc11(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2b5f(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x2b49(%rip),%rax
mov    (%rax),%edi
mov    -0x148(%rbp),%edx
mov    -0x14c(%rbp),%ecx
mov    -0x150(%rbp),%r8d
lea    0xbe7(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
cmpl   $0x0,-0x14c(%rbp)
je     14a4 <main+0x274>
cmpl   $0x0,-0x150(%rbp)
jne    14c9 <main+0x299>
mov    0x2b25(%rip),%rax
mov    (%rax),%edi
lea    0xbf1(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2af6(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   10c0 <listen@plt>
cmp    $0xffffffff,%eax
jne    14f9 <main+0x2c9>
lea    0xbe1(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2ac6(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x2ab0(%rip),%rax
mov    (%rax),%edi
lea    0xbc7(%rip),%rsi
mov    $0x0,%al
call   10a0 <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    154e <main+0x31e>
lea    0xbc5(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2a71(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    0x2a5b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x15c(%rbp)
mov    0x2a84(%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,-0x158(%rbp)
mov    -0x28(%rbp),%edi
call   10d0 <ntohs@plt>
mov    -0x15c(%rbp),%edi
mov    -0x158(%rbp),%rdx
mov    %eax,%ecx
lea    0xb65(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x5,%edi
call   1140 <sleep@plt>
mov    0x29f4(%rip),%rax
mov    (%rax),%edi
lea    0xb68(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    0x29c5(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x130,%rsp
cmp    $0x2,%edi
jne    1290 <main+0x60>
mov    0x8(%rsi),%rdi
call   1110 <atoi@plt>
mov    %eax,%ebp
cmp    $0x3ff,%eax
ja     1266 <main+0x36>
xor    %eax,%eax
call   1080 <geteuid@plt>
test   %rax,%rax
jne    1360 <main+0x130>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1150 <socket@plt>
cmp    $0xffffffff,%eax
jne    12af <main+0x7f>
lea    0xdca(%rip),%rdi
jmp    13a5 <main+0x175>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd5d(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    13aa <main+0x17a>
mov    %eax,%ebx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d1f(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
call   10b0 <getresuid@plt>
cmp    $0xffffffff,%eax
je     139e <main+0x16e>
mov    0x2ca1(%rip),%r12
mov    (%r12),%edi
mov    0x8(%rsp),%edx
mov    0x4(%rsp),%ecx
mov    (%rsp),%r8d
lea    0xd44(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%r12),%edi
lea    0xd4f(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   10a0 <fprintf@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1379 <main+0x149>
lea    0xd40(%rip),%rdi
jmp    13a5 <main+0x175>
mov    0x2c69(%rip),%rax
mov    (%rax),%edi
lea    0xca7(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    13aa <main+0x17a>
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%edi
call   1130 <setuid@plt>
lea    0x8(%rsp),%rdi
lea    0x4(%rsp),%rsi
mov    %rsp,%rdx
call   10b0 <getresuid@plt>
cmp    $0xffffffff,%eax
jne    13b8 <main+0x188>
lea    0xcb9(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2c07(%rip),%rax
mov    (%rax),%edi
call   1120 <exit@plt>
mov    (%r12),%edi
mov    0x8(%rsp),%edx
mov    0x4(%rsp),%ecx
mov    (%rsp),%r8d
lea    0xc9b(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
cmpl   $0x0,0x4(%rsp)
je     13fd <main+0x1cd>
cmpl   $0x0,(%rsp)
je     13fd <main+0x1cd>
mov    %ebx,%edi
mov    $0x2,%esi
call   10c0 <listen@plt>
cmp    $0xffffffff,%eax
jne    1412 <main+0x1e2>
lea    0xccc(%rip),%rdi
jmp    13a5 <main+0x175>
mov    0x2bcc(%rip),%rax
mov    (%rax),%edi
lea    0xc98(%rip),%rsi
jmp    1370 <main+0x140>
mov    (%r12),%edi
lea    0xcb3(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1100 <accept@plt>
cmp    $0xffffffff,%eax
jne    144e <main+0x21e>
lea    0xcb7(%rip),%rdi
jmp    13a5 <main+0x175>
mov    %eax,%ebp
mov    (%r12),%r14d
mov    (%r15),%edi
lea    0x24(%rsp),%rsi
lea    0x30(%rsp),%rdx
mov    $0x100,%ecx
call   1030 <inet_ntop@plt>
mov    %rax,%r15
mov    0x20(%rsp),%edi
call   10d0 <ntohs@plt>
lea    0xc8b(%rip),%rsi
mov    %r14d,%edi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x5,%edi
call   1140 <sleep@plt>
mov    (%r12),%edi
lea    0xc8b(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %ebx,%edi
call   1090 <close@plt>
mov    0x2aea(%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 = [23 x i8] c"Invocation: %s <PORT>\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 = [34 x i8] c"Run server as unprivileged user!\0A\00"
@.str.8 = [9 x i8] c"listen()\00"
@.str.9 = [48 x i8] c"Server is listening for incoming connection...\0A\00"
@.str.10 = [9 x i8] c"accept()\00"
@.str.11 = [36 x i8] c"TCP connection accepted from %s:%d\0A\00"
@.str.12 = [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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 2
br i1 %18, label %19, label %27
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %2
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %13
%32 = load i32, i32* %13
%33 = icmp ult i32 %32, 1024
br i1 %33, label %34, label %43
34: ; preds = %27
%35 = call i64 (...) @geteuid()
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %34
br label %43
43: ; preds = %42, %27
%44 = load i32, i32* @PF_INET
%45 = load i32, i32* @SOCK_STREAM
%46 = call i32 @socket(i32 %44, i32 %45, i32 0)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %53
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %43
%54 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%55 = load i32, i32* @AF_INET
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %55, i32* %56
%57 = load i32, i32* @INADDR_ANY
%58 = call i32 @htonl(i32 %57)
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%60 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %59, i32 0, i32 0
store i32 %58, i32* %60
%61 = load i32, i32* %13
%62 = call i32 @htons(i32 %61)
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %62, i32* %63
store i32 12, i32* %11
%64 = call i32 @getresuid(i32* %14, i32* %15, i32* %16)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %70
66: ; preds = %53
%67 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%68 = load i32, i32* @EXIT_FAILURE
%69 = call i32 @exit(i32 %68)
unreachable
70: ; preds = %53
%71 = load i32, i32* @stdout
%72 = load i32, i32* %14
%73 = load i32, i32* %15
%74 = load i32, i32* %16
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %72, i32 %73, i32 %74)
%76 = load i32, i32* @stdout
%77 = load i32, i32* %13
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %77)
%79 = load i32, i32* %6
%80 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%81 = load i32, i32* %11
%82 = call i32 @bind(i32 %79, %struct.sockaddr* %80, i32 %81)
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %88
84: ; preds = %70
%85 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%86 = load i32, i32* @EXIT_FAILURE
%87 = call i32 @exit(i32 %86)
unreachable
88: ; preds = %70
%89 = call i32 (...) @getuid()
%90 = call i32 @setuid(i32 %89)
%91 = call i32 @getresuid(i32* %14, i32* %15, i32* %16)
%92 = icmp eq i32 %91, -1
br i1 %92, label %93, label %97
93: ; preds = %88
%94 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%95 = load i32, i32* @EXIT_FAILURE
%96 = call i32 @exit(i32 %95)
unreachable
97: ; preds = %88
%98 = load i32, i32* @stdout
%99 = load i32, i32* %14
%100 = load i32, i32* %15
%101 = load i32, i32* %16
%102 = call i32 (i32, i8*, ...) @fprintf(i32 %98, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %99, i32 %100, i32 %101)
%103 = load i32, i32* %15
%104 = icmp eq i32 %103, 0
br i1 %104, label %108, label %105
105: ; preds = %97
%106 = load i32, i32* %16
%107 = icmp eq i32 %106, 0
br i1 %107, label %108, label %113
108: ; preds = %105, %97
%109 = load i32, i32* @stderr
%110 = call i32 (i32, i8*, ...) @fprintf(i32 %109, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%111 = load i32, i32* @EXIT_FAILURE
%112 = call i32 @exit(i32 %111)
unreachable
113: ; preds = %105
%114 = load i32, i32* %6
%115 = call i32 @listen(i32 %114, i32 2)
%116 = icmp eq i32 %115, -1
br i1 %116, label %117, label %121
117: ; preds = %113
%118 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
%119 = load i32, i32* @EXIT_FAILURE
%120 = call i32 @exit(i32 %119)
unreachable
121: ; preds = %113
%122 = load i32, i32* @stdout
%123 = call i32 (i32, i8*, ...) @fprintf(i32 %122, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.9, i64 0, i64 0))
store i32 12, i32* %10
%124 = load i32, i32* %6
%125 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%126 = call i32 @accept(i32 %124, %struct.sockaddr* %125, i32* %10)
store i32 %126, i32* %7
%127 = load i32, i32* %7
%128 = icmp eq i32 %127, -1
br i1 %128, label %129, label %133
129: ; preds = %121
%130 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0))
%131 = load i32, i32* @EXIT_FAILURE
%132 = call i32 @exit(i32 %131)
unreachable
133: ; preds = %121
%134 = load i32, i32* @stdout
%135 = load i32, i32* @AF_INET
%136 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%137 = getelementptr inbounds [256 x i8], [256 x i8]* %12, i64 0, i64 0
%138 = call i8* @inet_ntop(i32 %135, %struct.TYPE_2__* %136, i8* %137, i32 256)
%139 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%140 = load i32, i32* %139
%141 = call i32 @ntohs(i32 %140)
%142 = call i32 (i32, i8*, ...) @fprintf(i32 %134, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.11, i64 0, i64 0), i8* %138, i32 %141)
%143 = call i32 @sleep(i32 5)
%144 = load i32, i32* @stdout
%145 = call i32 (i32, i8*, ...) @fprintf(i32 %144, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.12, i64 0, i64 0))
%146 = load i32, i32* %7
%147 = call i32 @close(i32 %146)
%148 = load i32, i32* %6
%149 = call i32 @close(i32 %148)
%150 = load i32, i32* @EXIT_SUCCESS
%151 = call i32 @exit(i32 %150)
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 = [23 x i8] c"Invocation: %s <PORT>\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 = [34 x i8] c"Run server as unprivileged user!\0A\00"
@.str.8 = [9 x i8] c"listen()\00"
@.str.9 = [48 x i8] c"Server is listening for incoming connection...\0A\00"
@.str.10 = [9 x i8] c"accept()\00"
@.str.11 = [36 x i8] c"TCP connection accepted from %s:%d\0A\00"
@.str.12 = [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, 2
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 ([23 x i8], [23 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 = icmp ult i32 %27, 1024
br i1 %28, label %29, label %37
29: ; preds = %24
%30 = tail call i64 (...) @geteuid()
%31 = icmp eq i64 %30, 0
br i1 %31, label %37, label %32
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %29, %24
%38 = load i32, i32* @PF_INET
%39 = load i32, i32* @SOCK_STREAM
%40 = tail call i32 @socket(i32 %38, i32 %39, i32 0)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
%45 = tail call i32 @exit(i32 %44)
unreachable
46: ; preds = %37
%47 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%48 = load i32, i32* @AF_INET
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %48, i32* %49
%50 = load i32, i32* @INADDR_ANY
%51 = call i32 @htonl(i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %51, i32* %52
%53 = call i32 @htons(i32 %27)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %53, i32* %54
%55 = call i32 @getresuid(i32* %7, i32* %8, i32* %9)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %61
57: ; preds = %46
%58 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%59 = load i32, i32* @EXIT_FAILURE
%60 = call i32 @exit(i32 %59)
unreachable
61: ; preds = %46
%62 = load i32, i32* @stdout
%63 = load i32, i32* %7
%64 = load i32, i32* %8
%65 = load i32, i32* %9
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %63, i32 %64, i32 %65)
%67 = load i32, i32* @stdout
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%69 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%70 = call i32 @bind(i32 %40, %struct.sockaddr* %69, i32 12)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %76
72: ; preds = %61
%73 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %61
%77 = call i32 (...) @getuid()
%78 = call i32 @setuid(i32 %77)
%79 = call i32 @getresuid(i32* %7, i32* %8, i32* %9)
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %85
81: ; preds = %76
%82 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%83 = load i32, i32* @EXIT_FAILURE
%84 = call i32 @exit(i32 %83)
unreachable
85: ; preds = %76
%86 = load i32, i32* @stdout
%87 = load i32, i32* %7
%88 = load i32, i32* %8
%89 = load i32, i32* %9
%90 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %87, i32 %88, i32 %89)
%91 = load i32, i32* %8
%92 = icmp eq i32 %91, 0
%93 = load i32, i32* %9
%94 = icmp eq i32 %93, 0
%95 = select i1 %92, i1 true, i1 %94
br i1 %95, label %96, label %101
96: ; preds = %85
%97 = load i32, i32* @stderr
%98 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.7, i64 0, i64 0))
%99 = load i32, i32* @EXIT_FAILURE
%100 = call i32 @exit(i32 %99)
unreachable
101: ; preds = %85
%102 = call i32 @listen(i32 %40, i32 2)
%103 = icmp eq i32 %102, -1
br i1 %103, label %104, label %108
104: ; preds = %101
%105 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
%106 = load i32, i32* @EXIT_FAILURE
%107 = call i32 @exit(i32 %106)
unreachable
108: ; preds = %101
%109 = load i32, i32* @stdout
%110 = call i32 (i32, i8*, ...) @fprintf(i32 %109, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.9, i64 0, i64 0))
store i32 12, i32* %5
%111 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%112 = call i32 @accept(i32 %40, %struct.sockaddr* %111, i32* %5)
%113 = icmp eq i32 %112, -1
br i1 %113, label %114, label %118
114: ; preds = %108
%115 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0))
%116 = load i32, i32* @EXIT_FAILURE
%117 = call i32 @exit(i32 %116)
unreachable
118: ; preds = %108
%119 = load i32, i32* @stdout
%120 = load i32, i32* @AF_INET
%121 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%122 = call i8* @inet_ntop(i32 %120, %struct.TYPE_2__* %121, i8* %13, i32 256)
%123 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%124 = load i32, i32* %123
%125 = call i32 @ntohs(i32 %124)
%126 = call i32 (i32, i8*, ...) @fprintf(i32 %119, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.11, i64 0, i64 0), i8* %122, i32 %125)
%127 = call i32 @sleep(i32 5)
%128 = load i32, i32* @stdout
%129 = call i32 (i32, i8*, ...) @fprintf(i32 %128, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.12, i64 0, i64 0))
%130 = call i32 @close(i32 %112)
%131 = call i32 @close(i32 %40)
%132 = load i32, i32* @EXIT_SUCCESS
%133 = call i32 @exit(i32 %132)
unreachable
}