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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ *fopen(char *, char *);
long fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
long htonl(long);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, long *, int);
int main() {
  int socket_desc;
  const char *message = "Hello from client\n";
  socket_desc = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in server;
  server.sin_addr.s_addr = inet_addr("127.0.0.1");
  server.sin_family = AF_INET;
  server.sin_port = htons(2345);
  if (socket_desc == -1) {
    printf(
        "Client encountered an error when creating socket for communication\n");
    return 1;
  }
  if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) {
    printf("Connection error\n");
    return 1;
  }
  printf("Connection successful\n");
  FILE *fd = fopen("/var/cache/dnf/x86_64/21/@System.solv", "r");
  long int len = fseek(fd, 0, SEEK_END);
  printf("length of file: %d\n", ftell(fd));
  long int send_num = htonl(len);
  write(socket_desc, &send_num, sizeof(send_num));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    0xe36(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x18(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x929,%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1222 <main+0x82>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <main+0x132>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
cmp    $0x0,%rax
jge    1257 <main+0xb7>
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <main+0x132>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe1e(%rip),%rdi
lea    0xe3d(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,%esi
lea    0xe0c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1060 <htonl@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e1f(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
lea    0xe23(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    (%rbx),%eax
mov    %eax,0x4(%rsp)
mov    $0x929,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
cmp    $0xffffffff,%r14d
je     128b <main+0xdb>
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
js     1294 <main+0xe4>
lea    0xe25(%rip),%rdi
call   1030 <puts@plt>
lea    0xddf(%rip),%rdi
lea    0xe37(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    0x2da7(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1090 <ftell@plt>
lea    0xdcf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1070 <htonl@plt>
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x8,%edx
call   1040 <write@plt>
jmp    12a5 <main+0xf5>
lea    0xdd9(%rip),%rdi
jmp    129b <main+0xeb>
lea    0xdbf(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [19 x i8] c"Hello from client\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [68 x i8] c"Client encountered an error when creating socket for communication\0A\00"
@.str.3 = [18 x i8] c"Connection error\0A\00"
@.str.4 = [23 x i8] c"Connection successful\0A\00"
@.str.5 = [38 x i8] c"/var/cache/dnf/x86_64/21/@System.solv\00"
@.str.6 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.7 = [20 x i8] c"length of file: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %1
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %3
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 0
store i32 %11, i32* %13
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %14, i32* %15
%16 = call i32 @htons(i32 2345)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* %2
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %42
22: ; preds = %0
%23 = load i32, i32* %2
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %42
29: ; preds = %22
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32* @fopen(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %31, i32** %5
%32 = load i32*, i32** %5
%33 = load i32, i32* @SEEK_END
%34 = call i64 @fseek(i32* %32, i32 0, i32 %33)
store i64 %34, i64* %6
%35 = load i32*, i32** %5
%36 = call i32 @ftell(i32* %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 %36)
%38 = load i64, i64* %6
%39 = call i64 @htonl(i64 %38)
store i64 %39, i64* %7
%40 = load i32, i32* %2
%41 = call i32 @write(i32 %40, i64* %7, i32 8)
store i32 0, i32* %1
br label %42
42: ; preds = %29, %27, %20
%43 = load i32, i32* %1
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.5 = [38 x i8] c"/var/cache/dnf/x86_64/21/@System.solv\00"
@.str.6 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.7 = [20 x i8] c"length of file: %d\0A\00"
@str = [22 x i8] c"Connection successful\00"
@str.8 = [17 x i8] c"Connection error\00"
@str.9 = [67 x i8] c"Client encountered an error when creating socket for communication\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i64
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2, i32 0
store i32 %7, i32* %8
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %9, i32* %10
%11 = tail call i32 @htons(i32 2345)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %11, i32* %12
%13 = icmp eq i32 %5, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @str.9, i64 0, i64 0))
br label %32
16: ; preds = %0
%17 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%18 = call i64 @connect(i32 %5, %struct.sockaddr* %17, i32 12)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.8, i64 0, i64 0))
br label %32
22: ; preds = %16
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%24 = call i32* @fopen(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%25 = load i32, i32* @SEEK_END
%26 = call i64 @fseek(i32* %24, i32 0, i32 %25)
%27 = call i32 @ftell(i32* %24)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 %27)
%29 = bitcast i64* %2 to i8*
%30 = call i64 @htonl(i64 %26)
store i64 %30, i64* %2
%31 = call i32 @write(i32 %5, i64* %2, i32 8)
br label %32
32: ; preds = %22, %20, %14
%33 = phi i32 [ 1, %14 ], [ 1, %20 ], [ 0, %22 ]
ret i32 %33
}