#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void initShell(int *sock) {
  FILE *configFile;
  char ip[64];
  int port;
  if (!(configFile = fopen(".config", "r"))) {
    printf("cannot read config file...\n");
    exit(1);
  }
  fscanf(configFile, "%s", ip);
  fscanf(configFile, "%d", &port);
  fclose(configFile);
  struct sockaddr_in addr;
  int i;
  *sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (*sock < 0) {
    printf("eek! couldn't open socket\n");
    exit(-1);
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = inet_addr(ip);
  addr.sin_port = htons((unsigned short)port);
  i = connect(*sock, (struct sockaddr *)&addr, sizeof(addr));
  if (i < 0) {
    printf("client could not connect!\n");
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<initShell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe4d(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11e3 <initShell+0x43>
lea    0xe36(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rdx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1070 <fscanf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe25(%rip),%rsi
lea    -0x54(%rbp),%rdx
mov    $0x0,%al
call   1070 <fscanf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    0x2daf(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0x0,(%rax)
jge    1263 <initShell+0xc3>
lea    0xdd8(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
lea    -0x50(%rbp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x54(%rbp),%eax
movzwl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0x60(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
jge    12d0 <initShell+0x130>
lea    0xd86(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initShell>:
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rdi,%r14
lea    0xe4f(%rip),%rdi
lea    0xe50(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     127d <initShell+0xdd>
mov    %rax,%rbx
lea    0xe3a(%rip),%rsi
lea    0x20(%rsp),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1070 <fscanf@plt>
lea    0xe27(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1070 <fscanf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    0x2dc3(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
call   10c0 <socket@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     1293 <initShell+0xf3>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x20(%rsp),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x14(%rsp)
movzwl 0xc(%rsp),%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %eax,%eax
js     129c <initShell+0xfc>
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd8c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdab(%rip),%rdi
jmp    12a3 <initShell+0x103>
lea    0xd88(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c".config\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [28 x i8] c"cannot read config file...\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [3 x i8] c"%d\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.5 = [27 x i8] c"eek! couldn't open socket\0A\00"
@AF_INET = global i32 0
@.str.6 = [27 x i8] c"client could not connect!\0A\00"
define void @initShell(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca [64 x i8]
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32* %0, i32** %2
%8 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = load i32*, i32** %3
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%16 = call i32 (i32*, i8*, ...) @fscanf(i32* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%17 = load i32*, i32** %3
%18 = call i32 (i32*, i8*, ...) @fscanf(i32* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %5)
%19 = load i32*, i32** %3
%20 = call i32 @fclose(i32* %19)
%21 = load i32, i32* @PF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = load i32, i32* @IPPROTO_TCP
%24 = call i32 @socket(i32 %21, i32 %22, i32 %23)
%25 = load i32*, i32** %2
store i32 %24, i32* %25
%26 = load i32*, i32** %2
%27 = load i32, i32* %26
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %13
%30 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0))
%31 = call i32 @exit(i32 -1)
unreachable
32: ; preds = %13
%33 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %34, i32* %35
%36 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%37 = call i32 @inet_addr(i8* %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* %5
%41 = trunc i32 %40 to i16
%42 = call i32 @htons(i16 zeroext %41)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i32*, i32** %2
%45 = load i32, i32* %44
%46 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%47 = call i32 @connect(i32 %45, %struct.sockaddr* %46, i32 12)
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %32
%51 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.6, i64 0, i64 0))
%52 = call i32 @exit(i32 -1)
unreachable
53: ; preds = %32
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c".config\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [3 x i8] c"%d\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AF_INET = global i32 0
@str = [27 x i8] c"cannot read config file...\00"
@str.7 = [26 x i8] c"client could not connect!\00"
@str.8 = [26 x i8] c"eek! couldn't open socket\00"
define void @initShell(i32* nocapture %0) {
%2 = alloca [64 x i8]
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = call i32 (i32*, i8*, ...) @fscanf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%14 = call i32 (i32*, i8*, ...) @fscanf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %3)
%15 = call i32 @fclose(i32* %7)
%16 = bitcast %struct.sockaddr_in* %4 to i8*
%17 = load i32, i32* @PF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = load i32, i32* @IPPROTO_TCP
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
store i32 %20, i32* %0
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %12
%26 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @inet_addr(i8* %5)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %3
%32 = trunc i32 %31 to i16
%33 = call i32 @htons(i16 zeroext %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %0
%36 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%37 = call i32 @connect(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %25
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %25
ret void
}