#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ s_in;
typedef int /*<<< orphan*/ one;

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ handle_dude(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void pwn(void) {
  int s, dude;
  int one = 1;
  struct sockaddr_in s_in;
  if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
    err(1, "socket()");
  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1)
    err(1, "setsockopt()");
  memset(&s_in, 0, sizeof(s_in));
  s_in.sin_family = PF_INET;
  s_in.sin_port = htons(80);
  s_in.sin_addr.s_addr = INADDR_ANY;
  if (bind(s, (struct sockaddr *)&s_in, sizeof(s_in)) == -1)
    err(1, "bind()");
  if (listen(s, 5) == -1)
    err(1, "listen()");
  while ((dude = accept(s, NULL, NULL)) != -1) {
    handle_dude(dude);
    close(dude);
  }
  err(1, "accept()");
  close(s);
}


x86 assembly (clang, -O0):
<pwn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0xc(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11ec <pwn+0x4c>
mov    $0x1,%edi
lea    0xe19(%rip),%rsi
call   1070 <err@plt>
mov    -0x4(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    122a <pwn+0x8a>
mov    $0x1,%edi
lea    0xde4(%rip),%rsi
call   1070 <err@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    128a <pwn+0xea>
mov    $0x1,%edi
lea    0xd91(%rip),%rsi
call   1070 <err@plt>
mov    -0x4(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12b1 <pwn+0x111>
mov    $0x1,%edi
lea    0xd71(%rip),%rsi
call   1070 <err@plt>
jmp    12b6 <pwn+0x116>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10b0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
je     12e6 <pwn+0x146>
mov    -0x8(%rbp),%edi
call   10a0 <handle_dude@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    12b6 <pwn+0x116>
mov    $0x1,%edi
lea    0xd34(%rip),%rsi
call   1070 <err@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pwn>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movl   $0x1,0xc(%rsp)
mov    0x2e09(%rip),%r14
mov    (%r14),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11e9 <pwn+0x49>
lea    0xe21(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1223 <pwn+0x83>
lea    0xdf0(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    127e <pwn+0xde>
lea    0xda2(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    12a0 <pwn+0x100>
lea    0xd87(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
cmp    $0xffffffff,%eax
je     12e0 <pwn+0x140>
mov    %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,%edi
call   10a0 <handle_dude@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    12c0 <pwn+0x120>
lea    0xd3f(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [9 x i8] c"socket()\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [13 x i8] c"setsockopt()\00"
@INADDR_ANY = global i32 0
@.str.2 = [7 x i8] c"bind()\00"
@.str.3 = [9 x i8] c"listen()\00"
@.str.4 = [9 x i8] c"accept()\00"
define void @pwn() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
store i32 1, i32* %3
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* %1
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32, i32* %1
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_REUSEADDR
%16 = call i32 @setsockopt(i32 %13, i32 %14, i32 %15, i32* %3, i32 4)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%22 = load i32, i32* @PF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 80)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %1
%30 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%31 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %20
%34 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %20
%36 = load i32, i32* %1
%37 = call i32 @listen(i32 %36, i32 5)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
br label %42
42: ; preds = %46, %41
%43 = load i32, i32* %1
%44 = call i32 @accept(i32 %43, i32* null, i32* null)
store i32 %44, i32* %2
%45 = icmp ne i32 %44, -1
br i1 %45, label %46, label %51
46: ; preds = %42
%47 = load i32, i32* %2
%48 = call i32 @handle_dude(i32 %47)
%49 = load i32, i32* %2
%50 = call i32 @close(i32 %49)
br label %42
51: ; preds = %42
%52 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%53 = load i32, i32* %1
%54 = call i32 @close(i32 %53)
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [9 x i8] c"socket()\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [13 x i8] c"setsockopt()\00"
@INADDR_ANY = global i32 0
@.str.2 = [7 x i8] c"bind()\00"
@.str.3 = [9 x i8] c"listen()\00"
@.str.4 = [9 x i8] c"accept()\00"
define void @pwn() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_REUSEADDR
%15 = call i32 @setsockopt(i32 %8, i32 %13, i32 %14, i32* %1, i32 4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%21 = load i32, i32* @PF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 80)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %25, i32* %26
%27 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%28 = call i32 @bind(i32 %8, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %19
%31 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %19
%33 = call i32 @listen(i32 %8, i32 5)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = call i32 @accept(i32 %8, i32* null, i32* null)
%39 = icmp eq i32 %38, -1
br i1 %39, label %46, label %40
40: ; preds = %37, %40
%41 = phi i32 [ %44, %40 ], [ %38, %37 ]
%42 = call i32 @handle_dude(i32 %41)
%43 = call i32 @close(i32 %41)
%44 = call i32 @accept(i32 %8, i32* null, i32* null)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %40
46: ; preds = %40, %37
%47 = call i32 @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%48 = call i32 @close(i32 %8)
ret void
}