#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int dummy;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ bzero(char *, size_t);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
size_t recv(int, char *, size_t, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
void dataConnect(int sock, struct sockaddr_in *client_addr, char *filename,
                 char *opts) {
  int maxfd;
  size_t n, BUFFER_SIZE = 1024;
  char buffer[BUFFER_SIZE];
  bzero(buffer, 256);
  fd_set rfds;
  struct timeval tv;
  int retval;
  FILE *fp = fopen(filename, opts);
  if (!fp)
    perror("Error creating file.");
  printf("In dataConnect.\n");
  FD_ZERO(&rfds);
  FD_SET(sock, &rfds);
  maxfd = sock + 1;
  int i = 0;
  while (i++ < 1000) {
    bzero(buffer, BUFFER_SIZE);
    tv.tv_sec = 20;
    tv.tv_usec = 0;
    retval = select(maxfd, &rfds, NULL, NULL, &tv);
    if (retval == -1) {
      fclose(fp);
      perror("select()");
    } else if (retval) {
      n = recv(sock, buffer, BUFFER_SIZE, 0);
      if (n < 0)
        perror("ERROR reading from socket");
      if (n != 0) {
        if (fwrite(buffer, 1, n, fp) != n)
          fprintf(stderr, "Not %lu bytes written...\n", n);
      }
    } else {
      fclose(fp);
      perror("Timeout");
    }
  }
  printf("Out of loop\n");
}


x86 assembly (clang, -O0):
<dataConnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x400,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x80(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x48(%rbp)
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    122b <dataConnect+0x7b>
lea    0xdda(%rip),%rdi
call   10a0 <perror@plt>
lea    0xde3(%rip),%rdi
call   1050 <printf@plt>
lea    -0x4c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x4c(%rbp),%rsi
call   10d0 <FD_SET@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x74(%rbp)
mov    -0x74(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x74(%rbp)
cmp    $0x3e8,%eax
jge    1370 <dataConnect+0x1c0>
mov    -0x80(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10b0 <bzero@plt>
movl   $0x14,-0x60(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x4c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x60(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0xffffffff,-0x64(%rbp)
jne    12cc <dataConnect+0x11c>
mov    -0x70(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd64(%rip),%rdi
call   10a0 <perror@plt>
jmp    136b <dataConnect+0x1bb>
cmpl   $0x0,-0x64(%rbp)
je     1351 <dataConnect+0x1a1>
mov    -0x80(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x38(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jae    1303 <dataConnect+0x153>
lea    0xd31(%rip),%rdi
call   10a0 <perror@plt>
cmpq   $0x0,-0x30(%rbp)
je     134c <dataConnect+0x19c>
mov    -0x80(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x70(%rbp),%rcx
mov    $0x1,%esi
call   10c0 <fwrite@plt>
cmp    -0x30(%rbp),%rax
je     1347 <dataConnect+0x197>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xd07(%rip),%rsi
call   1060 <fprintf@plt>
jmp    134c <dataConnect+0x19c>
jmp    1366 <dataConnect+0x1b6>
mov    -0x70(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd02(%rip),%rdi
call   10a0 <perror@plt>
jmp    136b <dataConnect+0x1bb>
jmp    125c <dataConnect+0xac>
lea    0xcf4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dataConnect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %edi,%r12d
lea    0x20(%rsp),%rdi
mov    $0x100,%esi
call   10b0 <bzero@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1090 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     1307 <dataConnect+0x157>
lea    0xe49(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   10d0 <FD_SET@plt>
lea    0x1(%r12),%ebp
mov    $0x3e8,%r13d
lea    0x20(%rsp),%rbx
jmp    124a <dataConnect+0x9a>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xe03(%rip),%rdi
call   10a0 <perror@plt>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%r13d
je     12e9 <dataConnect+0x139>
mov    $0x400,%esi
mov    %rbx,%rdi
call   10b0 <bzero@plt>
movl   $0x14,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    %ebp,%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
call   1080 <select@plt>
test   %eax,%eax
je     1226 <dataConnect+0x76>
cmp    $0xffffffff,%eax
je     12d5 <dataConnect+0x125>
mov    $0x400,%edx
mov    %r12d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
je     1240 <dataConnect+0x90>
mov    %rax,%r15
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   10c0 <fwrite@plt>
cmp    %r15,%rax
je     1240 <dataConnect+0x90>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    0xd56(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
jmp    1240 <dataConnect+0x90>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xd31(%rip),%rdi
jmp    1235 <dataConnect+0x85>
lea    0xd60(%rip),%rdi
call   1040 <puts@plt>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf2(%rip),%rdi
call   10a0 <perror@plt>
jmp    11f0 <dataConnect+0x40>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
@.str = [21 x i8] c"Error creating file.\00"
@.str.1 = [17 x i8] c"In dataConnect.\0A\00"
@.str.2 = [9 x i8] c"select()\00"
@.str.3 = [26 x i8] c"ERROR reading from socket\00"
@stderr = global i32 0
@.str.4 = [26 x i8] c"Not %lu bytes written...\0A\00"
@.str.5 = [8 x i8] c"Timeout\00"
@.str.6 = [13 x i8] c"Out of loop\0A\00"
define void @dataConnect(i32 %0, %struct.sockaddr_in* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca %struct.sockaddr_in*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
%15 = alloca %struct.timeval
%16 = alloca i32
%17 = alloca i32*
%18 = alloca i32
store i32 %0, i32* %5
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
store i64 1024, i64* %11
%19 = load i64, i64* %11
%20 = call i8* @llvm.stacksave()
store i8* %20, i8** %12
%21 = alloca i8, i64 %19
store i64 %19, i64* %13
%22 = call i32 @bzero(i8* %21, i64 256)
%23 = load i8*, i8** %7
%24 = load i8*, i8** %8
%25 = call i32* @fopen(i8* %23, i8* %24)
store i32* %25, i32** %17
%26 = load i32*, i32** %17
%27 = icmp ne i32* %26, null
br i1 %27, label %30, label %28
28: ; preds = %4
%29 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %4
%31 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%32 = call i32 @FD_ZERO(i32* %14)
%33 = load i32, i32* %5
%34 = call i32 @FD_SET(i32 %33, i32* %14)
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %9
store i32 0, i32* %18
br label %37
37: ; preds = %85, %30
%38 = load i32, i32* %18
%39 = add nsw i32 %38, 1
store i32 %39, i32* %18
%40 = icmp slt i32 %38, 1000
br i1 %40, label %41, label %86
41: ; preds = %37
%42 = load i64, i64* %11
%43 = call i32 @bzero(i8* %21, i64 %42)
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
store i32 20, i32* %44
%45 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
store i64 0, i64* %45
%46 = load i32, i32* %9
%47 = call i32 @select(i32 %46, i32* %14, i32* null, i32* null, %struct.timeval* %15)
store i32 %47, i32* %16
%48 = load i32, i32* %16
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %41
%51 = load i32*, i32** %17
%52 = call i32 @fclose(i32* %51)
%53 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %85
54: ; preds = %41
%55 = load i32, i32* %16
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %80
57: ; preds = %54
%58 = load i32, i32* %5
%59 = load i64, i64* %11
%60 = call i64 @recv(i32 %58, i8* %21, i64 %59, i32 0)
store i64 %60, i64* %10
%61 = load i64, i64* %10
%62 = icmp ult i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %57
%64 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %65
65: ; preds = %63, %57
%66 = load i64, i64* %10
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %79
68: ; preds = %65
%69 = load i64, i64* %10
%70 = load i32*, i32** %17
%71 = call i64 @fwrite(i8* %21, i32 1, i64 %69, i32* %70)
%72 = load i64, i64* %10
%73 = icmp ne i64 %71, %72
br i1 %73, label %74, label %78
74: ; preds = %68
%75 = load i32, i32* @stderr
%76 = load i64, i64* %10
%77 = call i32 @fprintf(i32 %75, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i64 %76)
br label %78
78: ; preds = %74, %68
br label %79
79: ; preds = %78, %65
br label %84
80: ; preds = %54
%81 = load i32*, i32** %17
%82 = call i32 @fclose(i32* %81)
%83 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %84
84: ; preds = %80, %79
br label %85
85: ; preds = %84, %50
br label %37
86: ; preds = %37
%87 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
%88 = load i8*, i8** %12
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.timeval = type { i32, i64 }
@.str = [21 x i8] c"Error creating file.\00"
@.str.2 = [9 x i8] c"select()\00"
@stderr = global i32 0
@.str.4 = [26 x i8] c"Not %lu bytes written...\0A\00"
@.str.5 = [8 x i8] c"Timeout\00"
@str = [16 x i8] c"In dataConnect.\00"
@str.7 = [12 x i8] c"Out of loop\00"
define void @dataConnect(i32 %0, %struct.sockaddr_in* nocapture readnone %1, i8* nocapture %2, i8* nocapture %3) {
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca [1024 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%9 = call i32 @bzero(i8* %8, i64 256)
%10 = bitcast i32* %5 to i8*
%11 = bitcast %struct.timeval* %6 to i8*
%12 = tail call i32* @fopen(i8* %2, i8* %3)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %4
%15 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %4
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%18 = call i32 @FD_ZERO(i32* %5)
%19 = call i32 @FD_SET(i32 %0, i32* %5)
%20 = add nsw i32 %0, 1
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
br label %23
23: ; preds = %16, %42
%24 = phi i32 [ 1, %16 ], [ %43, %42 ]
%25 = call i32 @bzero(i8* %8, i64 1024)
store i32 20, i32* %21
store i64 0, i64* %22
%26 = call i32 @select(i32 %20, i32* %5, i32* null, i32* null, %struct.timeval* %6)
switch i32 %26, label %30 [
i32 -1, label %27
i32 0, label %39
]
27: ; preds = %23
%28 = call i32 @fclose(i32* %12)
%29 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %42
30: ; preds = %23
%31 = call i64 @recv(i32 %0, i8* %8, i64 1024, i32 0)
%32 = icmp eq i64 %31, 0
br i1 %32, label %42, label %33
33: ; preds = %30
%34 = call i64 @fwrite(i8* %8, i32 1, i64 %31, i32* %12)
%35 = icmp eq i64 %34, %31
br i1 %35, label %42, label %36
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i64 %31)
br label %42
39: ; preds = %23
%40 = call i32 @fclose(i32* %12)
%41 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %39, %33, %36, %30, %27
%43 = add nuw nsw i32 %24, 1
%44 = icmp eq i32 %43, 1001
br i1 %44, label %45, label %23
45: ; preds = %42
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.7, i64 0, i64 0))
ret void
}