#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 addrinfo {
  int /*<<< orphan*/ 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*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
char *sockaddr2str(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int srv_run(const char *ip, const char *port) {
  int ret;
  int listen_sock;
  struct addrinfo hints, *ai_list;
  char *addrstr;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  ret = getaddrinfo(ip, port, &hints, &ai_list);
  do {
    if (ret != 0) {
      perror("getaddrinfo");
      ret = EXIT_FAILURE;
      goto error;
    }
  } while (0);
  listen_sock =
      socket(ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
  do {
    if (listen_sock == -1) {
      perror("socket");
      ret = EXIT_FAILURE;
      goto error_free;
    }
  } while (0);
  ret = bind(listen_sock, ai_list->ai_addr, ai_list->ai_addrlen);
  do {
    if (ret == -1) {
      perror("bind");
      ret = EXIT_FAILURE;
      goto error_free;
    }
  } while (0);
  ret = listen(listen_sock, 0);
  do {
    if (ret == -1) {
      perror("listen");
      ret = EXIT_FAILURE;
      goto close_srv;
    }
  } while (0);
  addrstr = sockaddr2str(ai_list->ai_addr);
  do {
    if (!addrstr) {
      perror("OOM");
      ret = EXIT_FAILURE;
      goto close_srv;
    }
  } while (0);
  printf("Running server on %s:%s\n", addrstr, "6060");
  free(addrstr);
  freeaddrinfo(ai_list);
  return listen_sock;
close_srv:
  close(listen_sock);
error_free:
  freeaddrinfo(ai_list);
error:
  return -1;
}


x86 assembly (clang, -O0):
<srv_run>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1233 <srv_run+0x83>
lea    0xde3(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    137f <srv_run+0x1cf>
jmp    1238 <srv_run+0x88>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    127c <srv_run+0xcc>
lea    0xda6(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1376 <srv_run+0x1c6>
jmp    1281 <srv_run+0xd1>
mov    -0x20(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    (%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%edx
call   1090 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12c0 <srv_run+0x110>
lea    0xd69(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1376 <srv_run+0x1c6>
jmp    12c5 <srv_run+0x115>
mov    -0x20(%rbp),%edi
xor    %esi,%esi
call   1080 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12f9 <srv_run+0x149>
lea    0xd35(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    136e <srv_run+0x1be>
jmp    12fe <srv_run+0x14e>
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1070 <sockaddr2str@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1335 <srv_run+0x185>
lea    0xd00(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    136e <srv_run+0x1be>
jmp    133a <srv_run+0x18a>
mov    -0x48(%rbp),%rsi
lea    0xcde(%rip),%rdi
lea    0xcf0(%rip),%rdx
call   1040 <printf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1386 <srv_run+0x1d6>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<srv_run>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2df5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
jne    1286 <srv_run+0xd6>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1294 <srv_run+0xe4>
mov    %eax,%ebp
mov    (%rsp),%rax
mov    (%rax),%esi
mov    0x4(%rax),%edx
mov    %ebp,%edi
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     12a2 <srv_run+0xf2>
mov    %ebp,%edi
xor    %esi,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     12b0 <srv_run+0x100>
mov    (%rsp),%rax
mov    (%rax),%edi
call   1070 <sockaddr2str@plt>
test   %rax,%rax
je     12b9 <srv_run+0x109>
mov    %rax,%rbx
lea    0xdbf(%rip),%rdi
lea    0xdd1(%rip),%rdx
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
jmp    12da <srv_run+0x12a>
lea    0xd73(%rip),%rdi
call   10a0 <perror@plt>
jmp    12d5 <srv_run+0x125>
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cc <srv_run+0x11c>
lea    0xd6a(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cc <srv_run+0x11c>
lea    0xd61(%rip),%rdi
jmp    12c0 <srv_run+0x110>
lea    0xd5f(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [12 x i8] c"getaddrinfo\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [4 x i8] c"OOM\00"
@.str.5 = [25 x i8] c"Running server on %s:%s\0A\00"
@.str.6 = [5 x i8] c"6060\00"
define i32 @srv_run(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 24)
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i32 %14, i32* %15
%16 = load i32, i32* @AI_PASSIVE
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 5
store i32 %16, i32* %17
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = call i32 @getaddrinfo(i8* %18, i8* %19, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %20, i32* %6
br label %21
21: ; preds = %2
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
store i32 %26, i32* %6
br label %98
27: ; preds = %21
br label %28
28: ; preds = %27
%29 = load %struct.addrinfo*, %struct.addrinfo** %9
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 4
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %9
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 3
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %9
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 2
%37 = load i32, i32* %36
%38 = call i32 @socket(i32 %31, i32 %34, i32 %37)
store i32 %38, i32* %7
br label %39
39: ; preds = %28
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%44 = load i32, i32* @EXIT_FAILURE
store i32 %44, i32* %6
br label %95
45: ; preds = %39
br label %46
46: ; preds = %45
%47 = load i32, i32* %7
%48 = load %struct.addrinfo*, %struct.addrinfo** %9
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %9
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = call i32 @bind(i32 %47, i32 %50, i32 %53)
store i32 %54, i32* %6
br label %55
55: ; preds = %46
%56 = load i32, i32* %6
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %55
%59 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%60 = load i32, i32* @EXIT_FAILURE
store i32 %60, i32* %6
br label %95
61: ; preds = %55
br label %62
62: ; preds = %61
%63 = load i32, i32* %7
%64 = call i32 @listen(i32 %63, i32 0)
store i32 %64, i32* %6
br label %65
65: ; preds = %62
%66 = load i32, i32* %6
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%70 = load i32, i32* @EXIT_FAILURE
store i32 %70, i32* %6
br label %92
71: ; preds = %65
br label %72
72: ; preds = %71
%73 = load %struct.addrinfo*, %struct.addrinfo** %9
%74 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %73, i32 0, i32 0
%75 = load i32, i32* %74
%76 = call i8* @sockaddr2str(i32 %75)
store i8* %76, i8** %10
br label %77
77: ; preds = %72
%78 = load i8*, i8** %10
%79 = icmp ne i8* %78, null
br i1 %79, label %83, label %80
80: ; preds = %77
%81 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%82 = load i32, i32* @EXIT_FAILURE
store i32 %82, i32* %6
br label %92
83: ; preds = %77
br label %84
84: ; preds = %83
%85 = load i8*, i8** %10
%86 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %85, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%87 = load i8*, i8** %10
%88 = call i32 @free(i8* %87)
%89 = load %struct.addrinfo*, %struct.addrinfo** %9
%90 = call i32 @freeaddrinfo(%struct.addrinfo* %89)
%91 = load i32, i32* %7
store i32 %91, i32* %3
br label %99
92: ; preds = %80, %68
%93 = load i32, i32* %7
%94 = call i32 @close(i32 %93)
br label %95
95: ; preds = %92, %58, %42
%96 = load %struct.addrinfo*, %struct.addrinfo** %9
%97 = call i32 @freeaddrinfo(%struct.addrinfo* %96)
br label %98
98: ; preds = %95, %24
store i32 -1, i32* %3
br label %99
99: ; preds = %98, %84
%100 = load i32, i32* %3
ret i32 %100
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [12 x i8] c"getaddrinfo\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [4 x i8] c"OOM\00"
@.str.5 = [25 x i8] c"Running server on %s:%s\0A\00"
@.str.6 = [5 x i8] c"6060\00"
define i32 @srv_run(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = load i32, i32* @AI_PASSIVE
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %12, i32* %13
%14 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %61
18: ; preds = %2
%19 = load %struct.addrinfo*, %struct.addrinfo** %4
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 4
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 3
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i64 0, i32 2
%25 = load i32, i32* %24
%26 = call i32 @socket(i32 %21, i32 %23, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %18
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %58
30: ; preds = %18
%31 = load %struct.addrinfo*, %struct.addrinfo** %4
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 0
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 1
%35 = load i32, i32* %34
%36 = call i32 @bind(i32 %26, i32 %33, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %58
40: ; preds = %30
%41 = call i32 @listen(i32 %26, i32 0)
%42 = icmp eq i32 %41, -1
br i1 %42, label %54, label %43
43: ; preds = %40
%44 = load %struct.addrinfo*, %struct.addrinfo** %4
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i64 0, i32 0
%46 = load i32, i32* %45
%47 = call i8* @sockaddr2str(i32 %46)
%48 = icmp eq i8* %47, null
br i1 %48, label %54, label %49
49: ; preds = %43
%50 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %47, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%51 = call i32 @free(i8* %47)
%52 = load %struct.addrinfo*, %struct.addrinfo** %4
%53 = call i32 @freeaddrinfo(%struct.addrinfo* %52)
br label %61
54: ; preds = %43, %40
%55 = phi i8* [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), %40 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), %43 ]
%56 = call i32 @perror(i8* %55)
%57 = call i32 @close(i32 %26)
br label %58
58: ; preds = %54, %38, %28
%59 = load %struct.addrinfo*, %struct.addrinfo** %4
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
br label %61
61: ; preds = %16, %58, %49
%62 = phi i32 [ %26, %49 ], [ -1, %58 ], [ -1, %16 ]
ret i32 %62
}