#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, size_t);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(int /*<<< orphan*/);
int /*<<< orphan*/ inet_ntoa(struct in_addr);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_to_gateway(char *hostname) {
  struct hostent *server_resolve;
  struct in_addr *server_address;
  struct sockaddr_in server;
  size_t address_length;
  int server_socket;
  server_resolve = gethostbyname(hostname);
  if (server_resolve == NULL) {
    fprintf(stderr, "Address not found for %s\n", hostname);
    exit(EXIT_FAILURE);
  }
  server_address = (struct in_addr *)server_resolve->h_addr_list[0];
  server.sin_family = AF_INET;
  server.sin_port = htons(3242);
  server.sin_addr.s_addr = inet_addr(inet_ntoa(*server_address));
  server_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (server_socket == -1) {
    perror("Error creating socket");
    exit(EXIT_FAILURE);
  }
  address_length = sizeof(struct sockaddr_in);
  if (connect(server_socket, (struct sockaddr *)&server, address_length) ==
      -1) {
    perror("Error connecting to gateway");
    exit(EXIT_FAILURE);
  }
  return server_socket;
}


x86 assembly (clang, -O0):
<connect_to_gateway>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11db <connect_to_gateway+0x4b>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe38(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0xcaa,%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%edi
call   1050 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1257 <connect_to_gateway+0xc7>
lea    0xdd6(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movq   $0xc,-0x30(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
jne    1292 <connect_to_gateway+0x102>
lea    0xdb1(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_gateway>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1212 <connect_to_gateway+0x82>
mov    (%rax),%rax
mov    (%rax),%rbx
mov    0x2e37(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    $0xcaa,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
call   1030 <inet_ntoa@plt>
mov    %eax,%edi
call   1050 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    (%r14),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     122c <connect_to_gateway+0x9c>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    $0xc,%edx
mov    %eax,%edi
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     1235 <connect_to_gateway+0xa5>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xdde(%rip),%rsi
mov    %rbx,%rdx
call   1070 <fprintf@plt>
jmp    1241 <connect_to_gateway+0xb1>
lea    0xde7(%rip),%rdi
jmp    123c <connect_to_gateway+0xac>
lea    0xdf4(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Address not found for %s\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Error creating socket\00"
@.str.2 = [28 x i8] c"Error connecting to gateway\00"
define i32 @connect_to_gateway(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.hostent*
%4 = alloca %struct.in_addr*
%5 = alloca %struct.sockaddr_in
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call %struct.hostent* @gethostbyname(i8* %8)
store %struct.hostent* %9, %struct.hostent** %3
%10 = load %struct.hostent*, %struct.hostent** %3
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %2
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %1
%19 = load %struct.hostent*, %struct.hostent** %3
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 0
%21 = load i64*, i64** %20
%22 = getelementptr inbounds i64, i64* %21, i64 0
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to %struct.in_addr*
store %struct.in_addr* %24, %struct.in_addr** %4
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %25, i32* %26
%27 = call i32 @htons(i32 3242)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %27, i32* %28
%29 = load %struct.in_addr*, %struct.in_addr** %4
%30 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @inet_ntoa(i32 %31)
%33 = call i32 @inet_addr(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%35 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %34, i32 0, i32 0
store i32 %33, i32* %35
%36 = load i32, i32* @AF_INET
%37 = load i32, i32* @SOCK_STREAM
%38 = call i32 @socket(i32 %36, i32 %37, i32 0)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %45
41: ; preds = %18
%42 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %18
store i64 12, i64* %6
%46 = load i32, i32* %7
%47 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%48 = load i64, i64* %6
%49 = call i32 @connect(i32 %46, %struct.sockaddr* %47, i64 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %45
%56 = load i32, i32* %7
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64* }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Address not found for %s\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Error creating socket\00"
@.str.2 = [28 x i8] c"Error connecting to gateway\00"
define i32 @connect_to_gateway(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = tail call %struct.hostent* @gethostbyname(i8* %0)
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%13 = load i64*, i64** %12
%14 = load i64, i64* %13
%15 = inttoptr i64 %14 to %struct.in_addr*
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 3242)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %15, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i32 @inet_ntoa(i32 %21)
%23 = tail call i32 @inet_addr(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = load i32, i32* @SOCK_STREAM
%27 = tail call i32 @socket(i32 %25, i32 %26, i32 0)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %11
%30 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %11
%34 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%35 = call i32 @connect(i32 %27, %struct.sockaddr* %34, i64 12)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %33
ret i32 %27
}