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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *getenv(char *);
scalar_t__ geteuid();
scalar_t__ getuid();
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ process_client(int, struct sockaddr_in *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  uid_t uid = getuid(), euid = geteuid();
  if (euid != 0 && uid != 0) {
    printf("The agent must be run as root. Please try again\n");
    exit(errno);
  }
  int sockfd;
  struct sockaddr_in self;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Socket");
    exit(errno);
  }
  memset(&self, 0, sizeof(self));
  self.sin_family = AF_INET;
  char *port_str;
  if (argc == 2) {
    port_str = argv[1];
  } else {
    port_str = getenv("AGENT_PORT");
  }
  int port;
  if (port_str == NULL) {
    port = 49153;
  } else {
    port = atoi(port_str);
  }
  printf("Starting agent on port %d\n", port);
  self.sin_port = htons(port);
  self.sin_addr.s_addr = INADDR_ANY;
  if (bind(sockfd, (struct sockaddr *)&self, sizeof(self)) != 0) {
    perror("socket--bind");
    exit(errno);
  }
  if (listen(sockfd, 20) != 0) {
    perror("socket--listen");
    exit(errno);
  }
  while (true) {
    int clientfd;
    struct sockaddr_in client_addr;
    int addrlen = sizeof(client_addr);
    clientfd =
        accept(sockfd, (struct sockaddr *)&client_addr, (socklen_t *)&addrlen);
    process_client(clientfd, &client_addr);
    close(clientfd);
  }
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getuid@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <geteuid@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     124e <main+0x5e>
cmpq   $0x0,-0x18(%rbp)
je     124e <main+0x5e>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    128d <main+0x9d>
lea    0xdb7(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    12c4 <main+0xd4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
jmp    12d4 <main+0xe4>
lea    0xd6d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12eb <main+0xfb>
movl   $0xc001,-0x3c(%rbp)
jmp    12f7 <main+0x107>
mov    -0x38(%rbp),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x3c(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2cc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
je     1354 <main+0x164>
lea    0xd1d(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
mov    -0x24(%rbp),%edi
mov    $0x14,%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
je     1385 <main+0x195>
lea    0xcf9(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2c4a(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
jmp    138a <main+0x19a>
movl   $0xc,-0x54(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
lea    -0x54(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   1040 <process_client@plt>
mov    -0x40(%rbp),%edi
call   10a0 <close@plt>
jmp    138a <main+0x19a>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1060 <getuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10a0 <geteuid@plt>
test   %rax,%rax
je     122e <main+0x2e>
test   %rbx,%rbx
jne    132b <main+0x12b>
mov    0x2db3(%rip),%rbx
mov    (%rbx),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
test   %eax,%eax
js     1339 <main+0x139>
mov    %eax,%r15d
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
cmp    $0x2,%ebp
jne    1280 <main+0x80>
mov    0x8(%r14),%rdi
test   %rdi,%rdi
je     1294 <main+0x94>
call   1100 <atoi@plt>
mov    %eax,%ebx
jmp    1299 <main+0x99>
lea    0xd80(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rdi
test   %rdi,%rdi
jne    1277 <main+0x77>
mov    $0xc001,%ebx
lea    0xd72(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebx,%edi
call   1070 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   10d0 <bind@plt>
test   %rax,%rax
jne    1342 <main+0x142>
mov    %r15d,%edi
mov    $0x14,%esi
call   10c0 <listen@plt>
test   %rax,%rax
jne    134b <main+0x14b>
lea    0x18(%rsp),%rbp
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movl   $0xc,0x4(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %r14,%rdx
call   10f0 <accept@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
call   1040 <process_client@plt>
mov    %ebx,%edi
call   10b0 <close@plt>
jmp    1300 <main+0x100>
lea    0xd17(%rip),%rdi
call   1050 <puts@plt>
jmp    1357 <main+0x157>
lea    0xcc0(%rip),%rdi
jmp    1352 <main+0x152>
lea    0xce4(%rip),%rdi
jmp    1352 <main+0x152>
lea    0xce8(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2c6a(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [49 x i8] c"The agent must be run as root. Please try again\0A\00"
@errno = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"Socket\00"
@.str.2 = [11 x i8] c"AGENT_PORT\00"
@.str.3 = [27 x i8] c"Starting agent on port %d\0A\00"
@INADDR_ANY = global i32 0
@.str.4 = [13 x i8] c"socket--bind\00"
@.str.5 = [15 x i8] c"socket--listen\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_in
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i64 (...) @getuid()
store i64 %15, i64* %6
%16 = call i64 (...) @geteuid()
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %2
%20 = load i64, i64* %6
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @errno
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %19, %2
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @errno
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %26
%36 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %37, i32* %38
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, 2
br i1 %40, label %41, label %45
41: ; preds = %35
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
store i8* %44, i8** %10
br label %47
45: ; preds = %35
%46 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i8* %46, i8** %10
br label %47
47: ; preds = %45, %41
%48 = load i8*, i8** %10
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %51
50: ; preds = %47
store i32 49153, i32* %11
br label %54
51: ; preds = %47
%52 = load i8*, i8** %10
%53 = call i32 @atoi(i8* %52)
store i32 %53, i32* %11
br label %54
54: ; preds = %51, %50
%55 = load i32, i32* %11
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %11
%58 = call i32 @htons(i32 %57)
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %58, i32* %59
%60 = load i32, i32* @INADDR_ANY
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%62 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %61, i32 0, i32 0
store i32 %60, i32* %62
%63 = load i32, i32* %8
%64 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%65 = call i64 @bind(i32 %63, %struct.sockaddr* %64, i32 12)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %71
67: ; preds = %54
%68 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%69 = load i32, i32* @errno
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %54
%72 = load i32, i32* %8
%73 = call i64 @listen(i32 %72, i32 20)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %79
75: ; preds = %71
%76 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%77 = load i32, i32* @errno
%78 = call i32 @exit(i32 %77)
unreachable
79: ; preds = %71
br label %80
80: ; preds = %79, %80
store i32 12, i32* %14
%81 = load i32, i32* %8
%82 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%83 = call i32 @accept(i32 %81, %struct.sockaddr* %82, i32* %14)
store i32 %83, i32* %12
%84 = load i32, i32* %12
%85 = call i32 @process_client(i32 %84, %struct.sockaddr_in* %13)
%86 = load i32, i32* %12
%87 = call i32 @close(i32 %86)
br label %80
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@errno = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"Socket\00"
@.str.2 = [11 x i8] c"AGENT_PORT\00"
@.str.3 = [27 x i8] c"Starting agent on port %d\0A\00"
@INADDR_ANY = global i32 0
@.str.4 = [13 x i8] c"socket--bind\00"
@.str.5 = [15 x i8] c"socket--listen\00"
@str = [48 x i8] c"The agent must be run as root. Please try again\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = tail call i64 (...) @getuid()
%7 = tail call i64 (...) @geteuid()
%8 = icmp ne i64 %7, 0
%9 = icmp ne i64 %6, 0
%10 = select i1 %8, i1 %9, i1 false
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
%13 = load i32, i32* @errno
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = bitcast %struct.sockaddr_in* %3 to i8*
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @errno
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %15
%26 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = icmp eq i32 %0, 2
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = getelementptr inbounds i8*, i8** %1, i64 1
%32 = load i8*, i8** %31
br label %35
33: ; preds = %25
%34 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = phi i8* [ %32, %30 ], [ %34, %33 ]
%37 = icmp eq i8* %36, null
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = call i32 @atoi(i8* %36)
br label %40
40: ; preds = %35, %38
%41 = phi i32 [ %39, %38 ], [ 49153, %35 ]
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = call i32 @htons(i32 %41)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %43, i32* %44
%45 = load i32, i32* @INADDR_ANY
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %45, i32* %46
%47 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%48 = call i64 @bind(i32 %19, %struct.sockaddr* %47, i32 12)
%49 = icmp eq i64 %48, 0
br i1 %49, label %54, label %50
50: ; preds = %40
%51 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32, i32* @errno
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %40
%55 = call i64 @listen(i32 %19, i32 20)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = bitcast %struct.sockaddr_in* %4 to i8*
%59 = bitcast i32* %5 to i8*
%60 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %65
61: ; preds = %54
%62 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%63 = load i32, i32* @errno
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %57, %65
store i32 12, i32* %5
%66 = call i32 @accept(i32 %19, %struct.sockaddr* %60, i32* %5)
%67 = call i32 @process_client(i32 %66, %struct.sockaddr_in* %4)
%68 = call i32 @close(i32 %66)
br label %65
}