#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 sockaddr_in {
  int dummy;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
scalar_t__ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
int tconnect(struct in_addr *, int);
int /*<<< orphan*/ write(int, char const *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int ii, sfd, errors;
  char *server;
  int port;
  int nconns, ccreat = 0;
  struct hostent *he;
  struct in_addr inadr;
  struct sockaddr_in sin;
  if (argc < 4) {
    printf("use: %s server port numconns\n", argv[0]);
    return 1;
  }
  server = argv[1];
  port = atoi(argv[2]);
  nconns = atoi(argv[3]);
  if (inet_aton(server, &inadr) == 0) {
    if ((he = gethostbyname(server)) == NULL) {
      fprintf(stderr, "unable to resolve: %s\n", server);
      return -1;
    }
    memcpy(&inadr.s_addr, he->h_addr_list[0], he->h_length);
  }
  for (ii = 0; ii < nconns; ii++) {
    errors = 0;
  retry:
    if ((sfd = tconnect(&inadr, port)) != -1) {
      char const *req = "GET / HTTP/1.0\r\n";
      write(sfd, req, strlen(req));
      ++ccreat;
      errors = 0;
      printf("%d\n", ccreat);
    } else {
      sleep(1);
      if (++errors < 4)
        goto retry;
      break;
    }
  }
  printf("%d connections created ...\n", ccreat);
  while (1)
    sleep(10);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11e8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1361 <main+0x1c1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1090 <inet_aton@plt>
cmp    $0x0,%rax
jne    1280 <main+0xe0>
mov    -0x28(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    1267 <main+0xc7>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdc8(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1361 <main+0x1c1>
lea    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1341 <main+0x1a1>
movl   $0x0,-0x1c(%rbp)
mov    -0x2c(%rbp),%esi
lea    -0x48(%rbp),%rdi
call   10a0 <tconnect@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
je     1308 <main+0x168>
lea    0xd7c(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x58(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x34(%rbp),%esi
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    132e <main+0x18e>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x4,%eax
jge    1329 <main+0x189>
jmp    129a <main+0xfa>
jmp    1341 <main+0x1a1>
jmp    1333 <main+0x193>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1287 <main+0xe7>
mov    -0x34(%rbp),%esi
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
jmp    1352 <main+0x1b2>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
cmp    $0x3,%edi
jg     11d9 <main+0x39>
mov    0x0(%rbp),%rsi
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbp),%r15
mov    0x10(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebx
mov    0x18(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %r15,%rdi
call   1090 <inet_aton@plt>
test   %rax,%rax
je     12c8 <main+0x128>
test   %r14d,%r14d
jle    12ee <main+0x14e>
mov    %rsp,%r15
lea    0xe1a(%rip),%r12
xor    %ebp,%ebp
jmp    124d <main+0xad>
nop
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
add    $0x1,%ebp
lea    0xe07(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    %ebp,%r14d
je     12c3 <main+0x123>
mov    %r15,%rdi
mov    %ebx,%esi
call   10a0 <tconnect@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    1220 <main+0x80>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   10a0 <tconnect@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    1220 <main+0x80>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   10a0 <tconnect@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    1220 <main+0x80>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   10a0 <tconnect@plt>
mov    %eax,%r13d
cmp    $0xffffffff,%eax
jne    1220 <main+0x80>
mov    $0x1,%edi
call   10c0 <sleep@plt>
jmp    12f0 <main+0x150>
mov    %r14d,%ebp
jmp    12f0 <main+0x150>
mov    %r15,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     130c <main+0x16c>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
mov    %rsp,%rdi
call   1080 <memcpy@plt>
test   %r14d,%r14d
jg     1211 <main+0x71>
xor    %ebp,%ebp
lea    0xd53(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   10c0 <sleep@plt>
jmp    1300 <main+0x160>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
lea    0xd02(%rip),%rsi
mov    %r15,%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    11ca <main+0x2a>

LLVM IR (-O0):
%struct.hostent = type { i32, i32* }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32 }
@.str = [30 x i8] c"use: %s server port numconns\0A\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"unable to resolve: %s\0A\00"
@.str.2 = [17 x i8] c"GET / HTTP/1.0\0D\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [28 x i8] c"%d connections created ...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.hostent*
%14 = alloca %struct.in_addr
%15 = alloca %struct.sockaddr_in
%16 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %12
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 4
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 1, i32* %3
br label %94
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
store i8* %27, i8** %9
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %10
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 3
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %11
%36 = load i8*, i8** %9
%37 = call i64 @inet_aton(i8* %36, %struct.in_addr* %14)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %58
39: ; preds = %24
%40 = load i8*, i8** %9
%41 = call %struct.hostent* @gethostbyname(i8* %40)
store %struct.hostent* %41, %struct.hostent** %13
%42 = icmp eq %struct.hostent* %41, null
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %9
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %45)
store i32 -1, i32* %3
br label %94
47: ; preds = %39
%48 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %14, i32 0, i32 0
%49 = load %struct.hostent*, %struct.hostent** %13
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %49, i32 0, i32 1
%51 = load i32*, i32** %50
%52 = getelementptr inbounds i32, i32* %51, i64 0
%53 = load i32, i32* %52
%54 = load %struct.hostent*, %struct.hostent** %13
%55 = getelementptr inbounds %struct.hostent, %struct.hostent* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @memcpy(i32* %48, i32 %53, i32 %56)
br label %58
58: ; preds = %47, %24
store i32 0, i32* %6
br label %59
59: ; preds = %86, %58
%60 = load i32, i32* %6
%61 = load i32, i32* %11
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %89
63: ; preds = %59
store i32 0, i32* %8
br label %64
64: ; preds = %83, %63
%65 = load i32, i32* %10
%66 = call i32 @tconnect(%struct.in_addr* %14, i32 %65)
store i32 %66, i32* %7
%67 = icmp ne i32 %66, -1
br i1 %67, label %68, label %78
68: ; preds = %64
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8** %16
%69 = load i32, i32* %7
%70 = load i8*, i8** %16
%71 = load i8*, i8** %16
%72 = call i32 @strlen(i8* %71)
%73 = call i32 @write(i32 %69, i8* %70, i32 %72)
%74 = load i32, i32* %12
%75 = add nsw i32 %74, 1
store i32 %75, i32* %12
store i32 0, i32* %8
%76 = load i32, i32* %12
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %76)
br label %85
78: ; preds = %64
%79 = call i32 @sleep(i32 1)
%80 = load i32, i32* %8
%81 = add nsw i32 %80, 1
store i32 %81, i32* %8
%82 = icmp slt i32 %81, 4
br i1 %82, label %83, label %84
83: ; preds = %78
br label %64
84: ; preds = %78
br label %89
85: ; preds = %68
br label %86
86: ; preds = %85
%87 = load i32, i32* %6
%88 = add nsw i32 %87, 1
store i32 %88, i32* %6
br label %59
89: ; preds = %84, %59
%90 = load i32, i32* %12
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %90)
br label %92
92: ; preds = %89, %92
%93 = call i32 @sleep(i32 10)
br label %92
94: ; preds = %43, %19
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32, i32* }
@.str = [30 x i8] c"use: %s server port numconns\0A\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"unable to resolve: %s\0A\00"
@.str.2 = [17 x i8] c"GET / HTTP/1.0\0D\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@.str.4 = [28 x i8] c"%d connections created ...\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.in_addr
%4 = bitcast %struct.in_addr* %3 to i8*
%5 = icmp slt i32 %0, 4
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %66
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = getelementptr inbounds i8*, i8** %1, i64 3
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = call i64 @inet_aton(i8* %11, %struct.in_addr* %3)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %9
%21 = call %struct.hostent* @gethostbyname(i8* %11)
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %66
26: ; preds = %20
%27 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %3, i64 0, i32 0
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i64 0, i32 1
%29 = load i32*, i32** %28
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @memcpy(i32* %27, i32 %30, i32 %32)
br label %34
34: ; preds = %26, %9
%35 = icmp sgt i32 %17, 0
br i1 %35, label %36, label %61
36: ; preds = %34, %40
%37 = phi i32 [ %44, %40 ], [ 0, %34 ]
%38 = call i32 @tconnect(%struct.in_addr* %3, i32 %14)
%39 = icmp eq i32 %38, -1
br i1 %39, label %47, label %40
40: ; preds = %55, %51, %47, %36
%41 = phi i32 [ %38, %36 ], [ %49, %47 ], [ %53, %51 ], [ %57, %55 ]
%42 = call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @write(i32 %41, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %42)
%44 = add nuw nsw i32 %37, 1
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %44)
%46 = icmp eq i32 %44, %17
br i1 %46, label %61, label %36
47: ; preds = %36
%48 = call i32 @sleep(i32 1)
%49 = call i32 @tconnect(%struct.in_addr* %3, i32 %14)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %40
51: ; preds = %47
%52 = call i32 @sleep(i32 1)
%53 = call i32 @tconnect(%struct.in_addr* %3, i32 %14)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %40
55: ; preds = %51
%56 = call i32 @sleep(i32 1)
%57 = call i32 @tconnect(%struct.in_addr* %3, i32 %14)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %40
59: ; preds = %55
%60 = call i32 @sleep(i32 1)
br label %61
61: ; preds = %40, %59, %34
%62 = phi i32 [ 0, %34 ], [ %37, %59 ], [ %17, %40 ]
%63 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %62)
br label %64
64: ; preds = %64, %61
%65 = call i32 @sleep(i32 10)
br label %64
66: ; preds = %23, %6
%67 = phi i32 [ 1, %6 ], [ -1, %23 ]
ret i32 %67
}