#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 */
typedef int /*<<< orphan*/ uint8_t;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ peer_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ _IONBF;
int accept(int, struct sockaddr *, int *);
int atoi(char const *);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int listen_inet_socket(int);
int /*<<< orphan*/ perror_die(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ report_peer_connected(struct sockaddr_in *, int);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
int main(int argc, const char **argv) {
  setvbuf(stdout, NULL, _IONBF, 0);
  int portnum = 9988;
  if (argc >= 2) {
    portnum = atoi(argv[1]);
  }
  printf("Listening on port %d\n", portnum);
  int sockfd = listen_inet_socket(portnum);
  struct sockaddr_in peer_addr;
  socklen_t peer_addr_len = sizeof(peer_addr);
  int newsockfd = accept(sockfd, (struct sockaddr *)&peer_addr, &peer_addr_len);
  if (newsockfd < 0) {
    perror_die("ERROR on accept");
  }
  report_peer_connected(&peer_addr, peer_addr_len);
  int flags = fcntl(newsockfd, F_GETFL, 0);
  if (flags == -1) {
    perror_die("fcntl F_GETFL");
  }
  if (fcntl(newsockfd, F_SETFL, flags | O_NONBLOCK) == -1) {
    perror_die("fcntl F_SETFL O_NONBLOCK");
  }
  while (1) {
    uint8_t buf[1024];
    printf("Calling recv...\n");
    int len = recv(newsockfd, buf, sizeof(buf), 0);
    if (len < 0) {
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        usleep(200 * 1000);
        continue;
      }
      perror_die("recv");
    } else if (len == 0) {
      printf("Peer disconnected; I'm done.\n");
      break;
    }
    printf("recv returned %d bytes\n", len);
  }
  close(newsockfd);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1090 <setvbuf@plt>
movl   $0x2704,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     1207 <main+0x57>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <listen_inet_socket@plt>
mov    %eax,-0x18(%rbp)
movl   $0x4,-0x24(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1253 <main+0xa3>
lea    0xdc8(%rip),%rdi
call   1040 <perror_die@plt>
mov    -0x24(%rbp),%esi
lea    -0x20(%rbp),%rdi
call   10c0 <report_peer_connected@plt>
mov    -0x28(%rbp),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1060 <fcntl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    128b <main+0xdb>
lea    0xda0(%rip),%rdi
call   1040 <perror_die@plt>
mov    -0x28(%rbp),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
mov    -0x2c(%rbp),%edx
mov    0x2d47(%rip),%rax
or     (%rax),%edx
call   1060 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12bd <main+0x10d>
lea    0xd7c(%rip),%rdi
call   1040 <perror_die@plt>
jmp    12c2 <main+0x112>
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x28(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    134d <main+0x19d>
mov    0x2cc8(%rip),%rax
mov    (%rax),%rax
mov    0x2c96(%rip),%rcx
cmp    (%rcx),%rax
je     132d <main+0x17d>
mov    0x2cae(%rip),%rax
mov    (%rax),%rax
mov    0x2c9c(%rip),%rcx
cmp    (%rcx),%rax
jne    133c <main+0x18c>
mov    $0x30d40,%edi
call   10d0 <usleep@plt>
jmp    12c2 <main+0x112>
lea    0xd1b(%rip),%rdi
call   1040 <perror_die@plt>
jmp    1372 <main+0x1c2>
cmpl   $0x0,-0x1034(%rbp)
jne    136d <main+0x1bd>
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    138b <main+0x1db>
jmp    1372 <main+0x1c2>
mov    -0x1034(%rbp),%esi
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12c2 <main+0x112>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   10a0 <setvbuf@plt>
mov    $0x2704,%ebp
cmp    $0x2,%ebx
jl     1206 <main+0x46>
mov    0x8(%r14),%rdi
call   10c0 <atoi@plt>
mov    %eax,%ebp
lea    0xdf3(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebp,%edi
call   1060 <listen_inet_socket@plt>
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %eax,0xc(%rsp)
mov    %eax,%edi
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    124c <main+0x8c>
lea    0xdcf(%rip),%rdi
call   1040 <perror_die@plt>
mov    0x4(%rsp),%esi
lea    0x8(%rsp),%rdi
call   10d0 <report_peer_connected@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1070 <fcntl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    127f <main+0xbf>
lea    0xdac(%rip),%rdi
call   1040 <perror_die@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
mov    0x2d59(%rip),%rax
mov    (%rax),%edx
or     %ebx,%edx
mov    %ebp,%edi
call   1070 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    12ab <main+0xeb>
lea    0xd8e(%rip),%rdi
call   1040 <perror_die@plt>
lea    0xdb8(%rip),%r15
lea    0x10(%rsp),%rbx
mov    0x2ce2(%rip),%r12
lea    0xd8d(%rip),%r13
jmp    12df <main+0x11f>
nopw   0x0(%rax,%rax,1)
je     1335 <main+0x175>
mov    %r13,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15,%rdi
call   1050 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    12d0 <main+0x110>
mov    0x2cc2(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
je     1329 <main+0x169>
mov    0x2caa(%rip),%rcx
cmp    (%rcx),%rax
je     1329 <main+0x169>
lea    0xd2b(%rip),%rdi
call   1040 <perror_die@plt>
jmp    12d2 <main+0x112>
mov    $0x30d40,%edi
call   10e0 <usleep@plt>
jmp    12df <main+0x11f>
lea    0xd3e(%rip),%rdi
call   1050 <puts@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    0xc(%rsp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stdout = global i32 0
@_IONBF = global i32 0
@.str = [22 x i8] c"Listening on port %d\0A\00"
@.str.1 = [16 x i8] c"ERROR on accept\00"
@F_GETFL = global i32 0
@.str.2 = [14 x i8] c"fcntl F_GETFL\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [25 x i8] c"fcntl F_SETFL O_NONBLOCK\00"
@.str.4 = [17 x i8] c"Calling recv...\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.5 = [5 x i8] c"recv\00"
@.str.6 = [30 x i8] c"Peer disconnected; I'm done.\0A\00"
@.str.7 = [24 x i8] c"recv returned %d bytes\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 %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [1024 x i32]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @stdout
%15 = load i32, i32* @_IONBF
%16 = call i32 @setvbuf(i32 %14, i32* null, i32 %15, i32 0)
store i32 9988, i32* %6
%17 = load i32, i32* %4
%18 = icmp sge i32 %17, 2
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %6
br label %24
24: ; preds = %19, %2
%25 = load i32, i32* %6
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %6
%28 = call i32 @listen_inet_socket(i32 %27)
store i32 %28, i32* %7
store i32 4, i32* %9
%29 = load i32, i32* %7
%30 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%31 = call i32 @accept(i32 %29, %struct.sockaddr* %30, i32* %9)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %24
%35 = call i32 @perror_die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %24
%37 = load i32, i32* %9
%38 = call i32 @report_peer_connected(%struct.sockaddr_in* %8, i32 %37)
%39 = load i32, i32* %10
%40 = load i32, i32* @F_GETFL
%41 = call i32 @fcntl(i32 %39, i32 %40, i32 0)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %36
%45 = call i32 @perror_die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %46
46: ; preds = %44, %36
%47 = load i32, i32* %10
%48 = load i32, i32* @F_SETFL
%49 = load i32, i32* %11
%50 = load i32, i32* @O_NONBLOCK
%51 = or i32 %49, %50
%52 = call i32 @fcntl(i32 %47, i32 %48, i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %46
%55 = call i32 @perror_die(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %46
br label %57
57: ; preds = %56, %72, %82
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%59 = load i32, i32* %10
%60 = getelementptr inbounds [1024 x i32], [1024 x i32]* %12, i64 0, i64 0
%61 = call i32 @recv(i32 %59, i32* %60, i32 4096, i32 0)
store i32 %61, i32* %13
%62 = load i32, i32* %13
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %76
64: ; preds = %57
%65 = load i64, i64* @errno
%66 = load i64, i64* @EAGAIN
%67 = icmp eq i64 %65, %66
br i1 %67, label %72, label %68
68: ; preds = %64
%69 = load i64, i64* @errno
%70 = load i64, i64* @EWOULDBLOCK
%71 = icmp eq i64 %69, %70
br i1 %71, label %72, label %74
72: ; preds = %68, %64
%73 = call i32 @usleep(i32 200000)
br label %57
74: ; preds = %68
%75 = call i32 @perror_die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %82
76: ; preds = %57
%77 = load i32, i32* %13
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %81
79: ; preds = %76
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
br label %85
81: ; preds = %76
br label %82
82: ; preds = %81, %74
%83 = load i32, i32* %13
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %83)
br label %57
85: ; preds = %79
%86 = load i32, i32* %10
%87 = call i32 @close(i32 %86)
%88 = load i32, i32* %7
%89 = call i32 @close(i32 %88)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stdout = global i32 0
@_IONBF = global i32 0
@.str = [22 x i8] c"Listening on port %d\0A\00"
@.str.1 = [16 x i8] c"ERROR on accept\00"
@F_GETFL = global i32 0
@.str.2 = [14 x i8] c"fcntl F_GETFL\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.3 = [25 x i8] c"fcntl F_SETFL O_NONBLOCK\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.5 = [5 x i8] c"recv\00"
@.str.7 = [24 x i8] c"recv returned %d bytes\0A\00"
@str = [16 x i8] c"Calling recv...\00"
@str.8 = [29 x i8] c"Peer disconnected; I'm done.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [1024 x i32]
%6 = load i32, i32* @stdout
%7 = load i32, i32* @_IONBF
%8 = tail call i32 @setvbuf(i32 %6, i32* null, i32 %7, i32 0)
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
br label %14
14: ; preds = %10, %2
%15 = phi i32 [ %13, %10 ], [ 9988, %2 ]
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = tail call i32 @listen_inet_socket(i32 %15)
%18 = bitcast %struct.sockaddr_in* %3 to i8*
%19 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i32 @accept(i32 %17, %struct.sockaddr* %20, i32* %4)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = call i32 @perror_die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %14
%26 = load i32, i32* %4
%27 = call i32 @report_peer_connected(%struct.sockaddr_in* %3, i32 %26)
%28 = load i32, i32* @F_GETFL
%29 = call i32 @fcntl(i32 %21, i32 %28, i32 0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = call i32 @perror_die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %25
%34 = load i32, i32* @F_SETFL
%35 = load i32, i32* @O_NONBLOCK
%36 = or i32 %35, %29
%37 = call i32 @fcntl(i32 %21, i32 %34, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @perror_die(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %41
41: ; preds = %39, %33
%42 = bitcast [1024 x i32]* %5 to i8*
%43 = getelementptr inbounds [1024 x i32], [1024 x i32]* %5, i64 0, i64 0
br label %44
44: ; preds = %63, %41
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%46 = call i32 @recv(i32 %21, i32* %43, i32 4096, i32 0)
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %59
48: ; preds = %44
%49 = load i64, i64* @errno
%50 = load i64, i64* @EAGAIN
%51 = icmp eq i64 %49, %50
%52 = load i64, i64* @EWOULDBLOCK
%53 = icmp eq i64 %49, %52
%54 = select i1 %51, i1 true, i1 %53
br i1 %54, label %55, label %57
55: ; preds = %48
%56 = call i32 @usleep(i32 200000)
br label %63
57: ; preds = %48
%58 = call i32 @perror_die(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
br label %61
59: ; preds = %44
%60 = icmp eq i32 %46, 0
br i1 %60, label %64, label %61
61: ; preds = %59, %57
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i32 %46)
br label %63
63: ; preds = %61, %55
br label %44
64: ; preds = %59
%65 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.8, i64 0, i64 0))
%66 = call i32 @close(i32 %21)
%67 = call i32 @close(i32 %17)
ret i32 0
}