#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_port;
  int /*<<< orphan*/ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ ip;
typedef int /*<<< orphan*/ h;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int server_listen(char *port) {
  struct sockaddr_in *sin;
  struct addrinfo h, *ai;
  char ip[INET_ADDRSTRLEN];
  int e;
  int fd;
  memset(&h, 0, sizeof(h));
  h.ai_family = AF_INET;
  h.ai_socktype = SOCK_STREAM;
  h.ai_flags = AI_PASSIVE;
  e = getaddrinfo(NULL, port, &h, &ai);
  if (e) {
    ;
    exit(1);
  }
  if (ai == NULL) {
    ;
    exit(1);
  }
  fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  if (fd < 0) {
    perror("socket");
    exit(1);
  }
  if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
    perror("bind");
    exit(1);
  }
  if (listen(fd, 10) < 0) {
    perror("listen");
    exit(1);
  }
  sin = (struct sockaddr_in *)ai->ai_addr;
  inet_ntop(AF_INET, &sin->sin_addr, ip, sizeof(ip));
  printf("listening on %s:%d...\n", ip, ntohs(sin->sin_port));
  return fd;
}


x86 assembly (clang, -O0):
<server_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     123c <server_listen+0x9c>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x38(%rbp)
jne    1251 <server_listen+0xb1>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0x0,-0x50(%rbp)
jge    128e <server_listen+0xee>
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x50(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12c4 <server_listen+0x124>
lea    0xd52(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x50(%rbp),%edi
mov    $0xa,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    12f1 <server_listen+0x151>
lea    0xd2a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x58(%rbp),%rdx
mov    -0x60(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rcx),%rcx
mov    %rcx,-0x10(%rbp)
mov    0x2cd5(%rip),%rcx
mov    (%rcx),%edi
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    %eax,%ecx
call   1030 <inet_ntop@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1070 <ntohs@plt>
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
lea    0xcdf(%rip),%rdi
call   1040 <printf@plt>
mov    -0x50(%rbp),%eax
mov    -0x40(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_listen>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
mov    %rsp,-0x38(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%r15
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x58(%rbp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    0x2df3(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,-0x44(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
lea    -0x30(%rbp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
jne    12b8 <server_listen+0x118>
mov    -0x30(%rbp),%rax
test   %rax,%rax
je     12b8 <server_listen+0x118>
mov    0x14(%rax),%edi
mov    0xc(%rax),%edx
mov    0x10(%rax),%esi
call   10c0 <socket@plt>
test   %eax,%eax
js     12c2 <server_listen+0x122>
mov    %eax,%r12d
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    0x8(%rax),%edx
mov    %r12d,%edi
call   1080 <bind@plt>
test   %rax,%rax
js     12cb <server_listen+0x12b>
mov    %r12d,%edi
mov    $0xa,%esi
call   1060 <listen@plt>
test   %rax,%rax
js     12d4 <server_listen+0x134>
mov    -0x30(%rbp),%rax
mov    (%rax),%rbx
mov    0x0(%r13),%edi
lea    0x4(%rbx),%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
mov    (%rbx),%edi
call   1070 <ntohs@plt>
lea    0xd7b(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
mov    -0x38(%rbp),%rsp
mov    %r12d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd37(%rip),%rdi
jmp    12db <server_listen+0x13b>
lea    0xd35(%rip),%rdi
jmp    12db <server_listen+0x13b>
lea    0xd31(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.addrinfo = type { i64, i32, i32, i32, i32, i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [23 x i8] c"listening on %s:%d...\0A\00"
define i32 @server_listen(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in*
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i32, i32* @INET_ADDRSTRLEN
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
%14 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 4
store i32 %15, i32* %16
%17 = load i32, i32* @SOCK_STREAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 3
store i32 %17, i32* %18
%19 = load i32, i32* @AI_PASSIVE
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 5
store i32 %19, i32* %20
%21 = load i8*, i8** %2
%22 = call i32 @getaddrinfo(i32* null, i8* %21, %struct.addrinfo* %4, %struct.addrinfo** %5)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %1
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %1
%28 = load %struct.addrinfo*, %struct.addrinfo** %5
%29 = icmp eq %struct.addrinfo* %28, null
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %27
%33 = load %struct.addrinfo*, %struct.addrinfo** %5
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 4
%35 = load i32, i32* %34
%36 = load %struct.addrinfo*, %struct.addrinfo** %5
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 3
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %5
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 2
%41 = load i32, i32* %40
%42 = call i32 @socket(i32 %35, i32 %38, i32 %41)
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %32
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %32
%49 = load i32, i32* %9
%50 = load %struct.addrinfo*, %struct.addrinfo** %5
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 0
%52 = load i64, i64* %51
%53 = load %struct.addrinfo*, %struct.addrinfo** %5
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = call i64 @bind(i32 %49, i64 %52, i32 %55)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %48
%59 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %48
%62 = load i32, i32* %9
%63 = call i64 @listen(i32 %62, i32 10)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %68
65: ; preds = %61
%66 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %61
%69 = load %struct.addrinfo*, %struct.addrinfo** %5
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 0
%71 = load i64, i64* %70
%72 = inttoptr i64 %71 to %struct.sockaddr_in*
store %struct.sockaddr_in* %72, %struct.sockaddr_in** %3
%73 = load i32, i32* @AF_INET
%74 = load %struct.sockaddr_in*, %struct.sockaddr_in** %3
%75 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %74, i32 0, i32 1
%76 = trunc i64 %11 to i32
%77 = call i32 @inet_ntop(i32 %73, i32* %75, i8* %13, i32 %76)
%78 = load %struct.sockaddr_in*, %struct.sockaddr_in** %3
%79 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %78, i32 0, i32 0
%80 = load i32, i32* %79
%81 = call i32 @ntohs(i32 %80)
%82 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %13, i32 %81)
%83 = load i32, i32* %9
%84 = load i8*, i8** %6
ret i32 %83
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32, i32 }
%struct.sockaddr_in = type { i32, i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [23 x i8] c"listening on %s:%d...\0A\00"
define i32 @server_listen(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = load i32, i32* @INET_ADDRSTRLEN
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 32)
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @AI_PASSIVE
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 5
store i32 %15, i32* %16
%17 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %1
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %1
%22 = load %struct.addrinfo*, %struct.addrinfo** %3
%23 = icmp eq %struct.addrinfo* %22, null
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %21
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 4
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 3
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %22, i64 0, i32 2
%32 = load i32, i32* %31
%33 = call i32 @socket(i32 %28, i32 %30, i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %26
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %26
%39 = load %struct.addrinfo*, %struct.addrinfo** %3
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 0
%41 = load i64, i64* %40
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i64 0, i32 1
%43 = load i32, i32* %42
%44 = call i64 @bind(i32 %33, i64 %41, i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %38
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %38
%50 = call i64 @listen(i32 %33, i32 10)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %49
%56 = load %struct.addrinfo*, %struct.addrinfo** %3
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i64 0, i32 0
%58 = load i64, i64* %57
%59 = inttoptr i64 %58 to %struct.sockaddr_in*
%60 = load i32, i32* @AF_INET
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %59, i64 0, i32 1
%62 = call i32 @inet_ntop(i32 %60, i32* %61, i8* %9, i32 %6)
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %59, i64 0, i32 0
%64 = load i32, i32* %63
%65 = call i32 @ntohs(i32 %64)
%66 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i8* %9, i32 %65)
ret i32 %33
}