#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 {
  unsigned short sin_port;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_NUMERICSERV;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
char *getenv(char *);
unsigned short htons(int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int sscanf(char *, char *, unsigned short *);
scalar_t__ strcmp(char *, char *);
unsigned short ssgl_get_gamelobby_port(char *proto) {
  struct addrinfo hints;
  struct addrinfo *lobbyserverinfo;
  int rc;
  unsigned short port = -1;
  char *override_port;
  override_port = getenv("SSGL_PORT");
  if (override_port) {
    rc = sscanf(override_port, "%hu", &port);
    if (rc == 1) {
      port = htons(port);
      return port;
    }
  }
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = 0;
  if (strcmp(proto, "tcp") == 0)
    hints.ai_protocol = IPPROTO_TCP;
  else if (strcmp(proto, "udp") == 0)
    hints.ai_protocol = IPPROTO_UDP;
  rc = getaddrinfo(NULL, "gamelobby", &hints, &lobbyserverinfo);
  if (rc == 0) {
    struct sockaddr_in *s = (struct sockaddr_in *)lobbyserverinfo->ai_addr;
    port = s->sin_port;
    freeaddrinfo(lobbyserverinfo);
  } else {
    hints.ai_flags |= AI_NUMERICSERV;
    rc = getaddrinfo(NULL, "2914", &hints, &lobbyserverinfo);
    if (rc == 0) {
      struct sockaddr_in *s = (struct sockaddr_in *)lobbyserverinfo->ai_addr;
      port = s->sin_port;
      freeaddrinfo(lobbyserverinfo);
    } else {
      port = htons(2914);
    }
  }
  return port;
}


x86 assembly (clang, -O0):
<ssgl_get_gamelobby_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movw   $0xffff,-0x36(%rbp)
lea    0xe77(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     11dd <ssgl_get_gamelobby_port+0x6d>
mov    -0x40(%rbp),%rdi
lea    0xe62(%rip),%rsi
lea    -0x36(%rbp),%rdx
call   1070 <sscanf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
jne    11d8 <ssgl_get_gamelobby_port+0x68>
movzwl -0x36(%rbp),%edi
call   1040 <htons@plt>
mov    %ax,-0x36(%rbp)
mov    -0x36(%rbp),%ax
mov    %ax,-0x2(%rbp)
jmp    131e <ssgl_get_gamelobby_port+0x1ae>
jmp    11dd <ssgl_get_gamelobby_port+0x6d>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdf7(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1237 <ssgl_get_gamelobby_port+0xc7>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1262 <ssgl_get_gamelobby_port+0xf2>
mov    -0x10(%rbp),%rdi
lea    0xdd0(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    125d <ssgl_get_gamelobby_port+0xed>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1262 <ssgl_get_gamelobby_port+0xf2>
xor    %eax,%eax
mov    %eax,%edi
lea    0xda9(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    12ab <ssgl_get_gamelobby_port+0x13b>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax),%ax
mov    %ax,-0x36(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    1316 <ssgl_get_gamelobby_port+0x1a6>
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd5b(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    1303 <ssgl_get_gamelobby_port+0x193>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    (%rax),%ax
mov    %ax,-0x36(%rbp)
mov    -0x30(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    1311 <ssgl_get_gamelobby_port+0x1a1>
mov    $0xb62,%edi
call   1040 <htons@plt>
mov    %ax,-0x36(%rbp)
jmp    1316 <ssgl_get_gamelobby_port+0x1a6>
mov    -0x36(%rbp),%ax
mov    %ax,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssgl_get_gamelobby_port>:
push   %rbx
sub    $0x30,%rsp
mov    %rdi,%rbx
movw   $0xffff,0xe(%rsp)
lea    0xe7a(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11b8 <ssgl_get_gamelobby_port+0x48>
lea    0xe73(%rip),%rsi
lea    0xe(%rsp),%rdx
mov    %rax,%rdi
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    11b8 <ssgl_get_gamelobby_port+0x48>
movzwl 0xe(%rsp),%edi
call   1040 <htons@plt>
jmp    1299 <ssgl_get_gamelobby_port+0x129>
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
movl   $0x0,0x20(%rsp)
lea    0xe1c(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     121c <ssgl_get_gamelobby_port+0xac>
lea    0xe0c(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    1229 <ssgl_get_gamelobby_port+0xb9>
mov    0x2d9e(%rip),%rax
jmp    1223 <ssgl_get_gamelobby_port+0xb3>
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
lea    0xde6(%rip),%rsi
lea    0x18(%rsp),%rdx
lea    0x10(%rsp),%rcx
xor    %edi,%edi
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     127f <ssgl_get_gamelobby_port+0x10f>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
or     %eax,0x20(%rsp)
lea    0xdc7(%rip),%rsi
lea    0x18(%rsp),%rdx
lea    0x10(%rsp),%rcx
xor    %edi,%edi
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     127f <ssgl_get_gamelobby_port+0x10f>
mov    $0xb62,%edi
call   1040 <htons@plt>
mov    %ax,0xe(%rsp)
jmp    1294 <ssgl_get_gamelobby_port+0x124>
mov    0x10(%rsp),%rdi
mov    (%rdi),%rax
movzwl (%rax),%eax
mov    %ax,0xe(%rsp)
call   1090 <freeaddrinfo@plt>
movzwl 0xe(%rsp),%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32 }
%struct.sockaddr_in = type { i16 }
@.str = [10 x i8] c"SSGL_PORT\00"
@.str.1 = [4 x i8] c"%hu\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [4 x i8] c"tcp\00"
@IPPROTO_TCP = global i32 0
@.str.3 = [4 x i8] c"udp\00"
@IPPROTO_UDP = global i32 0
@.str.4 = [10 x i8] c"gamelobby\00"
@AI_NUMERICSERV = global i32 0
@.str.5 = [5 x i8] c"2914\00"
define zeroext i16 @ssgl_get_gamelobby_port(i8* %0) {
%2 = alloca i16
%3 = alloca i8*
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca i32
%7 = alloca i16
%8 = alloca i8*
%9 = alloca %struct.sockaddr_in*
%10 = alloca %struct.sockaddr_in*
store i8* %0, i8** %3
store i16 -1, i16* %7
%11 = call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %8
%12 = load i8*, i8** %8
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %25
14: ; preds = %1
%15 = load i8*, i8** %8
%16 = call i32 @sscanf(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i16* %7)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i16, i16* %7
%21 = zext i16 %20 to i32
%22 = call zeroext i16 @htons(i32 %21)
store i16 %22, i16* %7
%23 = load i16, i16* %7
store i16 %23, i16* %2
br label %83
24: ; preds = %14
br label %25
25: ; preds = %24, %1
%26 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 24)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 4
store i32 %27, i32* %28
%29 = load i32, i32* @SOCK_STREAM
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 3
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 1
store i32 0, i32* %31
%32 = load i8*, i8** %3
%33 = call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %25
%36 = load i32, i32* @IPPROTO_TCP
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 2
store i32 %36, i32* %37
br label %46
38: ; preds = %25
%39 = load i8*, i8** %3
%40 = call i64 @strcmp(i8* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* @IPPROTO_UDP
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 2
store i32 %43, i32* %44
br label %45
45: ; preds = %42, %38
br label %46
46: ; preds = %45, %35
%47 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %5)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %60
50: ; preds = %46
%51 = load %struct.addrinfo*, %struct.addrinfo** %5
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = load i64, i64* %52
%54 = inttoptr i64 %53 to %struct.sockaddr_in*
store %struct.sockaddr_in* %54, %struct.sockaddr_in** %9
%55 = load %struct.sockaddr_in*, %struct.sockaddr_in** %9
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %55, i32 0, i32 0
%57 = load i16, i16* %56
store i16 %57, i16* %7
%58 = load %struct.addrinfo*, %struct.addrinfo** %5
%59 = call i32 @freeaddrinfo(%struct.addrinfo* %58)
br label %81
60: ; preds = %46
%61 = load i32, i32* @AI_NUMERICSERV
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 1
%63 = load i32, i32* %62
%64 = or i32 %63, %61
store i32 %64, i32* %62
%65 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %5)
store i32 %65, i32* %6
%66 = load i32, i32* %6
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %78
68: ; preds = %60
%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** %10
%73 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%74 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %73, i32 0, i32 0
%75 = load i16, i16* %74
store i16 %75, i16* %7
%76 = load %struct.addrinfo*, %struct.addrinfo** %5
%77 = call i32 @freeaddrinfo(%struct.addrinfo* %76)
br label %80
78: ; preds = %60
%79 = call zeroext i16 @htons(i32 2914)
store i16 %79, i16* %7
br label %80
80: ; preds = %78, %68
br label %81
81: ; preds = %80, %50
%82 = load i16, i16* %7
store i16 %82, i16* %2
br label %83
83: ; preds = %81, %19
%84 = load i16, i16* %2
ret i16 %84
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32 }
%struct.sockaddr_in = type { i16 }
@.str = [10 x i8] c"SSGL_PORT\00"
@.str.1 = [4 x i8] c"%hu\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [4 x i8] c"tcp\00"
@IPPROTO_TCP = global i32 0
@.str.3 = [4 x i8] c"udp\00"
@IPPROTO_UDP = global i32 0
@.str.4 = [10 x i8] c"gamelobby\00"
@AI_NUMERICSERV = global i32 0
@.str.5 = [5 x i8] c"2914\00"
define zeroext i16 @ssgl_get_gamelobby_port(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca i16
%5 = bitcast %struct.addrinfo* %2 to i8*
%6 = bitcast %struct.addrinfo** %3 to i8*
%7 = bitcast i16* %4 to i8*
store i16 -1, i16* %4
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %17, label %10
10: ; preds = %1
%11 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i16* %4)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i16, i16* %4
%15 = zext i16 %14 to i32
%16 = call zeroext i16 @htons(i32 %15)
br label %62
17: ; preds = %10, %1
%18 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 24)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %19, i32* %20
%21 = load i32, i32* @SOCK_STREAM
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 1
store i32 0, i32* %23
%24 = call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %17
%27 = call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26, %17
%30 = phi i32* [ @IPPROTO_TCP, %17 ], [ @IPPROTO_UDP, %26 ]
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 2
store i32 %31, i32* %32
br label %33
33: ; preds = %29, %26
%34 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %44
36: ; preds = %33
%37 = load %struct.addrinfo*, %struct.addrinfo** %3
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 0
%39 = load i64, i64* %38
%40 = inttoptr i64 %39 to %struct.sockaddr_in*
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %40, i64 0, i32 0
%42 = load i16, i16* %41
store i16 %42, i16* %4
%43 = call i32 @freeaddrinfo(%struct.addrinfo* %37)
br label %60
44: ; preds = %33
%45 = load i32, i32* @AI_NUMERICSERV
%46 = load i32, i32* %23
%47 = or i32 %46, %45
store i32 %47, i32* %23
%48 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %3)
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %58
50: ; preds = %44
%51 = load %struct.addrinfo*, %struct.addrinfo** %3
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i64 0, i32 0
%53 = load i64, i64* %52
%54 = inttoptr i64 %53 to %struct.sockaddr_in*
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %54, i64 0, i32 0
%56 = load i16, i16* %55
store i16 %56, i16* %4
%57 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
br label %60
58: ; preds = %44
%59 = call zeroext i16 @htons(i32 2914)
store i16 %59, i16* %4
br label %60
60: ; preds = %50, %58, %36
%61 = load i16, i16* %4
br label %62
62: ; preds = %60, %13
%63 = phi i16 [ %16, %13 ], [ %61, %60 ]
ret i16 %63
}