#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 scalar_t__ ssize_t;
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*/ fwrite(char *, int, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ recv(int, char *, size_t, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
void dataConnect(int sock, struct sockaddr_in *client_addr, char *filename) {
  int maxfd;
  ssize_t n;
  size_t BUFFER_SIZE = 1024;
  char buffer[BUFFER_SIZE];
  bzero(buffer, 256);
  fd_set rfds;
  struct timeval tv;
  int retval;
  if (client_addr == NULL)
    perror("useless thing");
  FILE *fp = fopen(filename, "w");
  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) {
        fwrite(buffer, 1, n, fp);
      }
    } 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)
movq   $0x400,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x78(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x40(%rbp)
mov    $0x100,%esi
call   10a0 <bzero@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1206 <dataConnect+0x66>
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
mov    -0x18(%rbp),%rdi
lea    0xdfd(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    1231 <dataConnect+0x91>
lea    0xde4(%rip),%rdi
call   1090 <perror@plt>
lea    0xded(%rip),%rdi
call   1050 <printf@plt>
lea    -0x44(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   10c0 <FD_SET@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x6c(%rbp)
mov    -0x6c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x6c(%rbp)
cmp    $0x3e8,%eax
jge    134e <dataConnect+0x1ae>
mov    -0x78(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10a0 <bzero@plt>
movl   $0x14,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x44(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x58(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
jne    12d2 <dataConnect+0x132>
mov    -0x68(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd6e(%rip),%rdi
call   1090 <perror@plt>
jmp    1349 <dataConnect+0x1a9>
cmpl   $0x0,-0x5c(%rbp)
je     132f <dataConnect+0x18f>
mov    -0x78(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x30(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1309 <dataConnect+0x169>
lea    0xd3b(%rip),%rdi
call   1090 <perror@plt>
cmpq   $0x0,-0x28(%rbp)
je     132a <dataConnect+0x18a>
mov    -0x78(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x68(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
jmp    1344 <dataConnect+0x1a4>
mov    -0x68(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd1a(%rip),%rdi
call   1090 <perror@plt>
jmp    1349 <dataConnect+0x1a9>
jmp    1262 <dataConnect+0xc2>
lea    0xd0c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x38(%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    %rdx,%rbx
mov    %rsi,%rbp
mov    %edi,%r12d
lea    0x20(%rsp),%rdi
mov    $0x100,%esi
call   10a0 <bzero@plt>
test   %rbp,%rbp
je     12f7 <dataConnect+0x157>
lea    0xe35(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r14
test   %rax,%rax
je     1308 <dataConnect+0x168>
lea    0xe5c(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   10c0 <FD_SET@plt>
lea    0x1(%r12),%ebp
mov    $0x3e8,%r13d
lea    0x20(%rsp),%rbx
jmp    124a <dataConnect+0xaa>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xe16(%rip),%rdi
call   1090 <perror@plt>
nopw   0x0(%rax,%rax,1)
add    $0xffffffff,%r13d
je     12d9 <dataConnect+0x139>
mov    $0x400,%esi
mov    %rbx,%rdi
call   10a0 <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   1070 <select@plt>
test   %eax,%eax
je     1223 <dataConnect+0x83>
cmp    $0xffffffff,%eax
je     12b7 <dataConnect+0x117>
mov    $0x400,%edx
mov    %r12d,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,%r15
test   %rax,%rax
js     12cb <dataConnect+0x12b>
je     1240 <dataConnect+0xa0>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   10b0 <fwrite@plt>
jmp    1240 <dataConnect+0xa0>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xd5f(%rip),%rdi
jmp    1232 <dataConnect+0x92>
lea    0xd5c(%rip),%rdi
call   1090 <perror@plt>
jmp    12a2 <dataConnect+0x102>
lea    0xd80(%rip),%rdi
call   1040 <puts@plt>
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd02(%rip),%rdi
call   1090 <perror@plt>
jmp    11d2 <dataConnect+0x32>
lea    0xd01(%rip),%rdi
call   1090 <perror@plt>
jmp    11ed <dataConnect+0x4d>

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

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