#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ clientname;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *inet_ntoa(int /*<<< orphan*/);
scalar_t__ listen(int, int);
int make_socket(int);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_kzorp_result(int);
int /*<<< orphan*/ stderr;
int main(void) {
  int sock, new;
  struct sockaddr_in clientname;
  socklen_t size;
  sock = make_socket(12345);
  if (listen(sock, 1) < 0) {
    perror("listen");
    exit(EXIT_FAILURE);
  }
  fprintf(stderr, "Listening on port %d\n", 12345);
  size = sizeof(clientname);
  new = accept(sock, (struct sockaddr *)&clientname, &size);
  if (new < 0) {
    perror("accept");
    exit(EXIT_FAILURE);
  }
  fprintf(stderr, "Connect from %s:%hu\n", inet_ntoa(clientname.sin_addr),
          ntohs(clientname.sin_port));
  print_kzorp_result(new);
  close(new);
  close(sock);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x3039,%edi
call   1080 <make_socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    11ed <main+0x4d>
lea    0xe26(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    $0x3039,%edx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x8,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1247 <main+0xa7>
lea    0xde9(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <ntohs@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1090 <print_kzorp_result@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    $0x3039,%edi
call   1080 <make_socket@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x1,%esi
call   1060 <listen@plt>
test   %rax,%rax
jns    11d0 <main+0x30>
lea    0xe32(%rip),%rdi
jmp    1211 <main+0x71>
mov    0x2e09(%rip),%r14
mov    (%r14),%edi
lea    0xe26(%rip),%rsi
mov    $0x3039,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10b0 <accept@plt>
test   %eax,%eax
jns    1224 <main+0x84>
lea    0xe0c(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    %eax,%ebp
mov    (%r14),%r14d
mov    0x10(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r15
mov    0x14(%rsp),%edi
call   1070 <ntohs@plt>
lea    0xddf(%rip),%rsi
mov    %r14d,%edi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1090 <print_kzorp_result@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"listen\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Listening on port %d\0A\00"
@.str.2 = [7 x i8] c"accept\00"
@.str.3 = [21 x i8] c"Connect from %s:%hu\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @make_socket(i32 12345)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = call i64 @listen(i32 %7, i32 1)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 12345)
store i32 8, i32* %5
%17 = load i32, i32* %2
%18 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%19 = call i32 @accept(i32 %17, %struct.sockaddr* %18, i32* %5)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %14
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %14
%27 = load i32, i32* @stderr
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i8* @inet_ntoa(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @ntohs(i32 %32)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %30, i32 %33)
%35 = load i32, i32* %3
%36 = call i32 @print_kzorp_result(i32 %35)
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
%41 = load i32, i32* @EXIT_SUCCESS
%42 = call i32 @exit(i32 %41)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"listen\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [22 x i8] c"Listening on port %d\0A\00"
@.str.2 = [7 x i8] c"accept\00"
@.str.3 = [21 x i8] c"Connect from %s:%hu\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @make_socket(i32 12345)
%6 = tail call i64 @listen(i32 %5, i32 1)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 12345)
store i32 8, i32* %2
%15 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%16 = call i32 @accept(i32 %5, %struct.sockaddr* %15, i32* %2)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
%23 = load i32, i32* @stderr
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i8* @inet_ntoa(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @ntohs(i32 %28)
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %26, i32 %29)
%31 = call i32 @print_kzorp_result(i32 %16)
%32 = call i32 @close(i32 %16)
%33 = call i32 @close(i32 %5)
%34 = load i32, i32* @EXIT_SUCCESS
%35 = call i32 @exit(i32 %34)
unreachable
}