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

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int send_protocol_id_to_lobby(int, int);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
int ssgl_connect_to_lobby(char *ssgl_hostname, int client) {
  int rc;
  struct addrinfo *lobbyserverinfo, *i;
  struct addrinfo hints;
  int lobby_sock;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  rc = getaddrinfo(ssgl_hostname, "gamelobby", &hints, &lobbyserverinfo);
  if (rc) {
    rc = getaddrinfo(ssgl_hostname, "2914", &hints, &lobbyserverinfo);
    if (rc)
      return rc;
  }
  for (i = lobbyserverinfo; i != NULL; i = i->ai_next) {
    if (i->ai_family == AF_INET) {
      break;
    }
  }
  if (i == NULL)
    return -1;
  lobby_sock = socket(AF_INET, SOCK_STREAM, i->ai_protocol);
  if (lobby_sock < 0)
    return lobby_sock;
  rc = connect(lobby_sock, i->ai_addr, i->ai_addrlen);
  if (rc < 0)
    return rc;
  rc = send_protocol_id_to_lobby(lobby_sock, client);
  if (rc) {
    shutdown(lobby_sock, SHUT_RDWR);
    close(lobby_sock);
    return rc;
  }
  freeaddrinfo(lobbyserverinfo);
  return lobby_sock;
}


x86 assembly (clang, -O0):
<ssgl_connect_to_lobby>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe30(%rip),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     121f <ssgl_connect_to_lobby+0x9f>
mov    -0x10(%rbp),%rdi
lea    0xe15(%rip),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     121a <ssgl_connect_to_lobby+0x9a>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <ssgl_connect_to_lobby+0x1ab>
jmp    121f <ssgl_connect_to_lobby+0x9f>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1264 <ssgl_connect_to_lobby+0xe4>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2d98(%rip),%rcx
cmp    (%rcx),%rax
jne    124e <ssgl_connect_to_lobby+0xce>
jmp    1264 <ssgl_connect_to_lobby+0xe4>
jmp    1253 <ssgl_connect_to_lobby+0xd3>
mov    -0x28(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1227 <ssgl_connect_to_lobby+0xa7>
cmpq   $0x0,-0x28(%rbp)
jne    127b <ssgl_connect_to_lobby+0xfb>
movl   $0xffffffff,-0x4(%rbp)
jmp    132b <ssgl_connect_to_lobby+0x1ab>
mov    0x2d56(%rip),%rax
mov    (%rax),%rdi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    12b2 <ssgl_connect_to_lobby+0x132>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <ssgl_connect_to_lobby+0x1ab>
mov    -0x54(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <connect@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12e0 <ssgl_connect_to_lobby+0x160>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <ssgl_connect_to_lobby+0x1ab>
mov    -0x54(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1030 <send_protocol_id_to_lobby@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     131c <ssgl_connect_to_lobby+0x19c>
mov    -0x54(%rbp),%edi
mov    0x2cee(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
mov    -0x54(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    132b <ssgl_connect_to_lobby+0x1ab>
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssgl_connect_to_lobby>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e2b(%rip),%r12
mov    (%r12),%rax
mov    %rax,0x8(%rsp)
mov    0x2e0b(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x2c(%rsp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
lea    0xe28(%rip),%rsi
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %rbx,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     120b <ssgl_connect_to_lobby+0x8b>
lea    0xe19(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
mov    %rbp,%rdi
call   1080 <getaddrinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    128a <ssgl_connect_to_lobby+0x10a>
mov    (%rsp),%rbx
mov    $0xffffffff,%ebp
test   %rbx,%rbx
je     128a <ssgl_connect_to_lobby+0x10a>
mov    (%r12),%rdi
nopl   (%rax)
cmp    %rdi,(%rbx)
je     1230 <ssgl_connect_to_lobby+0xb0>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1220 <ssgl_connect_to_lobby+0xa0>
jmp    128a <ssgl_connect_to_lobby+0x10a>
mov    (%r15),%esi
mov    0x10(%rbx),%edx
call   10a0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     128a <ssgl_connect_to_lobby+0x10a>
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
mov    %ebp,%edi
call   1070 <connect@plt>
test   %eax,%eax
js     127d <ssgl_connect_to_lobby+0xfd>
mov    %ebp,%edi
mov    %r14d,%esi
call   1030 <send_protocol_id_to_lobby@plt>
test   %eax,%eax
je     1281 <ssgl_connect_to_lobby+0x101>
mov    %eax,%ebx
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1040 <shutdown@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%ebp
jmp    128a <ssgl_connect_to_lobby+0x10a>
mov    %eax,%ebp
jmp    128a <ssgl_connect_to_lobby+0x10a>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, %struct.addrinfo*, i32, i32 }
@AF_INET = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [10 x i8] c"gamelobby\00"
@.str.1 = [5 x i8] c"2914\00"
@SHUT_RDWR = global i32 0
define i32 @ssgl_connect_to_lobby(i8* %0, i32 %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* %4, i32 0, i32 40)
%8 = load i64, i64* @AF_INET
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i64 %8, i64* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 6
store i32 %10, i32* %11
%12 = load i32, i32* @AI_PASSIVE
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %12, i32* %13
%14 = call i32 @getaddrinfo(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = call i32 @getaddrinfo(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %4, %struct.addrinfo** %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %56
19: ; preds = %16, %2
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %56, label %22
22: ; preds = %19
%23 = load i64, i64* @AF_INET
br label %24
24: ; preds = %22, %29
%25 = phi %struct.addrinfo* [ %20, %22 ], [ %31, %29 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%27 = load i64, i64* %26
%28 = icmp eq i64 %27, %23
br i1 %28, label %33, label %29
29: ; preds = %24
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%31 = load %struct.addrinfo*, %struct.addrinfo** %30
%32 = icmp eq %struct.addrinfo* %31, null
br i1 %32, label %56, label %24
33: ; preds = %24
%34 = load i32, i32* @SOCK_STREAM
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 3
%36 = load i32, i32* %35
%37 = call i32 @socket(i64 %23, i32 %34, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %56, label %39
39: ; preds = %33
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 2
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%43 = load i32, i32* %42
%44 = call i32 @connect(i32 %37, i32 %41, i32 %43)
%45 = icmp slt i32 %44, 0
br i1 %45, label %56, label %46
46: ; preds = %39
%47 = call i32 @send_protocol_id_to_lobby(i32 %37, i32 %1)
%48 = icmp eq i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %46
%50 = load i32, i32* @SHUT_RDWR
%51 = call i32 @shutdown(i32 %37, i32 %50)
%52 = call i32 @close(i32 %37)
br label %56
53: ; preds = %46
%54 = load %struct.addrinfo*, %struct.addrinfo** %3
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
br label %56
56: ; preds = %29, %19, %39, %33, %16, %53, %49
%57 = phi i32 [ %47, %49 ], [ %37, %53 ], [ %17, %16 ], [ %37, %33 ], [ %44, %39 ], [ -1, %19 ], [ -1, %29 ]
ret i32 %57
}