#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *);
int make_l2psocket() {
  int c;
  int sock;
  char srvIP[15];
  struct hostent *host;
  struct sockaddr_in server_addr;
  sprintf(srvIP, "193.61.194.29");
  host = gethostbyname(srvIP);
  if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    printf("l2pclient: Socket fail\n");
    sock = -1;
  } else {
    memset(&server_addr, -1, sizeof(server_addr));
    server_addr.sin_family = PF_INET;
    server_addr.sin_port = htons(2020);
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    bzero(&(server_addr.sin_zero), 8);
    c = connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
    if (c == -1) {
      printf("l2pclient: Connection to server fail\n");
      sock = -1;
    }
  }
  return sock;
}


x86 assembly (clang, -O0):
<make_l2psocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x17(%rbp),%rdi
lea    0xe6d(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x17(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11e2 <make_l2psocket+0x62>
lea    0xe3d(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    125e <make_l2psocket+0xde>
lea    -0x30(%rbp),%rdi
mov    $0xffffffff,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x7e4,%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
jne    1259 <make_l2psocket+0xd9>
lea    0xdd9(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x8(%rbp)
jmp    125e <make_l2psocket+0xde>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_l2psocket>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
movabs $0x312e31362e333931,%rax
31 2e 31
mov    %rax,0x18(%rsp)
movabs $0x39322e3439312e,%rax
mov    %rax,0x1e(%rsp)
lea    0x18(%rsp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,%r14
mov    0x2e2b(%rip),%r12
mov    (%r12),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1228 <make_l2psocket+0xb8>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
mov    $0xffffffff,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x14(%rsp)
mov    $0x7e4,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%r14),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r15,%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1239 <make_l2psocket+0xc9>
lea    0xde8(%rip),%rdi
jmp    122f <make_l2psocket+0xbf>
lea    0xe04(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"193.61.194.29\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [24 x i8] c"l2pclient: Socket fail\0A\00"
@.str.2 = [38 x i8] c"l2pclient: Connection to server fail\0A\00"
define i32 @make_l2psocket() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [15 x i8]
%4 = alloca %struct.hostent*
%5 = alloca %struct.sockaddr_in
%6 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%7 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%9 = call %struct.hostent* @gethostbyname(i8* %8)
store %struct.hostent* %9, %struct.hostent** %4
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %39
16: ; preds = %0
%17 = call i32 @memset(%struct.sockaddr_in* %5, i32 -1, i32 16)
%18 = load i32, i32* @PF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 3
store i32 %18, i32* %19
%20 = call i32 @htons(i32 2020)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%23 = load %struct.hostent*, %struct.hostent** %4
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = inttoptr i64 %25 to %struct.in_addr*
%27 = bitcast %struct.in_addr* %22 to i8*
%28 = bitcast %struct.in_addr* %26 to i8*
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%30 = call i32 @bzero(i32* %29, i32 8)
%31 = load i32, i32* %2
%32 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%33 = call i32 @connect(i32 %31, %struct.sockaddr* %32, i32 4)
store i32 %33, i32* %1
%34 = load i32, i32* %1
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %16
%37 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %38
38: ; preds = %36, %16
br label %39
39: ; preds = %38, %14
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@.str = [14 x i8] c"193.61.194.29\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@str = [37 x i8] c"l2pclient: Connection to server fail\00"
@str.3 = [23 x i8] c"l2pclient: Socket fail\00"
define i32 @make_l2psocket() {
%1 = alloca [15 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [15 x i8], [15 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = call %struct.hostent* @gethostbyname(i8* %3)
%6 = load i32, i32* @PF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %26, label %10
10: ; preds = %0
%11 = call i32 @memset(%struct.sockaddr_in* %2, i32 -1, i32 16)
%12 = load i32, i32* @PF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 %12, i32* %13
%14 = call i32 @htons(i32 2020)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%17 = load i64, i64* %16
%18 = inttoptr i64 %17 to i32*
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
%20 = load i32, i32* %18
store i32 %20, i32* %19
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%22 = call i32 @bzero(i32* %21, i32 8)
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i32 @connect(i32 %8, %struct.sockaddr* %23, i32 4)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %10, %0
%27 = phi i8* [ getelementptr inbounds ([23 x i8], [23 x i8]* @str.3, i64 0, i64 0), %0 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0), %10 ]
%28 = call i32 @puts(i8* dereferenceable(1) %27)
br label %29
29: ; preds = %26, %10
%30 = phi i32 [ %8, %10 ], [ -1, %26 ]
ret i32 %30
}