#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 */
typedef int /*<<< orphan*/ wrkBuf;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ srvAddr;
typedef int /*<<< orphan*/ cliAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SOCK_STREAM;
int S_IRUSR;
int S_IWUSR;
int accept(int, struct sockaddr *, unsigned int *);
int /*<<< orphan*/ atoi(char *);
scalar_t__ bind(int, struct sockaddr *, unsigned int);
int /*<<< orphan*/ errout(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_addr(char *);
scalar_t__ listen(int, int);
int open(char *, int, int);
scalar_t__ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
scalar_t__ write(int, char *, scalar_t__);
int main(int argc, char *argv[]) {
  int fds;
  int fdc;
  int fdf;
  struct sockaddr_in srvAddr;
  struct sockaddr_in cliAddr;
  unsigned int srvAddrLen;
  unsigned int cliAddrLen;
  char wrkBuf[4096];
  ssize_t nRead;
  if (argc != 4) {
    fprintf(stderr, "usage: minitcpsrvr ip-addr port outfile\n");
    exit(1);
  }
  if (!strcmp(argv[3], "-")) {
    fdf = 1;
  } else {
    fdf = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (fdf == -1)
      errout(argv[3]);
  }
  fds = socket(AF_INET, SOCK_STREAM, 0);
  srvAddr.sin_family = AF_INET;
  srvAddr.sin_addr.s_addr = inet_addr(argv[1]);
  srvAddr.sin_port = htons(atoi(argv[2]));
  srvAddrLen = sizeof(srvAddr);
  if (bind(fds, (struct sockaddr *)&srvAddr, srvAddrLen) != 0)
    errout("bind");
  if (listen(fds, 20) != 0)
    errout("listen");
  cliAddrLen = sizeof(cliAddr);
  fdc = accept(fds, (struct sockaddr *)&cliAddr, &cliAddrLen);
  while (1) {
    nRead = read(fdc, wrkBuf, sizeof(wrkBuf));
    if (nRead == 0)
      break;
    if (write(fdf, wrkBuf, nRead) != nRead)
      errout("write");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     1222 <main+0x42>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xded(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xdf8(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    124b <main+0x6b>
movl   $0x1,-0x1c(%rbp)
jmp    12a4 <main+0xc4>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    0x2d95(%rip),%rax
or     (%rax),%esi
mov    0x2d84(%rip),%rax
or     (%rax),%esi
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
mov    0x2d52(%rip),%rax
or     (%rax),%edx
call   10c0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    129f <main+0xbf>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <errout@plt>
jmp    12a4 <main+0xc4>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
mov    0x2d04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x3c(%rbp),%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
je     131f <main+0x13f>
lea    0xd11(%rip),%rdi
call   1090 <errout@plt>
mov    -0x14(%rbp),%edi
mov    $0x14,%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
je     1342 <main+0x162>
lea    0xcf3(%rip),%rdi
call   1090 <errout@plt>
movl   $0xc,-0x40(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x1040(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,-0x1048(%rbp)
cmpq   $0x0,-0x1048(%rbp)
jne    138a <main+0x1aa>
jmp    13be <main+0x1de>
mov    -0x1c(%rbp),%edi
lea    -0x1040(%rbp),%rsi
mov    -0x1048(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x1048(%rbp),%rax
je     13b9 <main+0x1d9>
lea    0xc83(%rip),%rdi
call   1090 <errout@plt>
jmp    135c <main+0x17c>
xor    %eax,%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1030,%rsp
cmp    $0x4,%edi
jne    1377 <main+0x197>
mov    %rsi,%r14
mov    0x18(%rsi),%rbx
lea    0xe23(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1260 <main+0x80>
mov    0x2d87(%rip),%rax
mov    0x2dd8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc5(%rip),%rax
or     (%rax),%esi
mov    0x2d7c(%rip),%rax
mov    0x2d95(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   10c0 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    1266 <main+0x86>
mov    0x18(%r14),%rdi
call   1090 <errout@plt>
mov    $0xffffffff,%r15d
jmp    1266 <main+0x86>
mov    $0x1,%r15d
mov    0x2d6b(%rip),%rbx
mov    (%rbx),%edi
mov    0x2d42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebp
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%r14),%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x14(%rsp)
mov    0x10(%r14),%rdi
call   10e0 <atoi@plt>
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
je     12ca <main+0xea>
lea    0xd66(%rip),%rdi
call   1090 <errout@plt>
mov    %ebp,%edi
mov    $0x14,%esi
call   10a0 <listen@plt>
test   %rax,%rax
je     12e7 <main+0x107>
lea    0xd4e(%rip),%rdi
call   1090 <errout@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebp,%edi
call   10d0 <accept@plt>
mov    %eax,%r12d
lea    0x30(%rsp),%rsi
mov    %eax,%edi
mov    $0x1000,%edx
call   1050 <read@plt>
test   %rax,%rax
je     1365 <main+0x185>
mov    %rax,%rbp
lea    0x30(%rsp),%rbx
lea    0xd0f(%rip),%r14
jmp    1348 <main+0x168>
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1050 <read@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1365 <main+0x185>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
cmp    %rbp,%rax
je     1330 <main+0x150>
mov    %r14,%rdi
call   1090 <errout@plt>
jmp    1330 <main+0x150>
xor    %eax,%eax
add    $0x1030,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c42(%rip),%rax
mov    (%rax),%edi
lea    0xc79(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [41 x i8] c"usage: minitcpsrvr ip-addr port outfile\0A\00"
@.str.1 = [2 x i8] c"-\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [6 x i8] c"write\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 %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca i32
%13 = alloca [4096 x i8]
%14 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 4
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 3
%24 = load i8*, i8** %23
%25 = call i32 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp ne i32 %25, 0
br i1 %26, label %28, label %27
27: ; preds = %21
store i32 1, i32* %8
br label %49
28: ; preds = %21
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 3
%31 = load i8*, i8** %30
%32 = load i32, i32* @O_WRONLY
%33 = load i32, i32* @O_CREAT
%34 = or i32 %32, %33
%35 = load i32, i32* @O_TRUNC
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IRUSR
%38 = load i32, i32* @S_IWUSR
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %31, i32 %36, i32 %39)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %28
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 3
%46 = load i8*, i8** %45
%47 = call i32 @errout(i8* %46)
br label %48
48: ; preds = %43, %28
br label %49
49: ; preds = %48, %27
%50 = load i32, i32* @AF_INET
%51 = load i32, i32* @SOCK_STREAM
%52 = call i32 @socket(i32 %50, i32 %51, i32 0)
store i32 %52, i32* %6
%53 = load i32, i32* @AF_INET
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %53, i32* %54
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8*, i8** %56
%58 = call i32 @inet_addr(i8* %57)
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%60 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %59, i32 0, i32 0
store i32 %58, i32* %60
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 2
%63 = load i8*, i8** %62
%64 = call i32 @atoi(i8* %63)
%65 = call i32 @htons(i32 %64)
%66 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %65, i32* %66
store i32 12, i32* %11
%67 = load i32, i32* %6
%68 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%69 = load i32, i32* %11
%70 = call i64 @bind(i32 %67, %struct.sockaddr* %68, i32 %69)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %49
%73 = call i32 @errout(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %74
74: ; preds = %72, %49
%75 = load i32, i32* %6
%76 = call i64 @listen(i32 %75, i32 20)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %74
%79 = call i32 @errout(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %80
80: ; preds = %78, %74
store i32 12, i32* %12
%81 = load i32, i32* %6
%82 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%83 = call i32 @accept(i32 %81, %struct.sockaddr* %82, i32* %12)
store i32 %83, i32* %7
br label %84
84: ; preds = %80, %100
%85 = load i32, i32* %7
%86 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%87 = call i64 @read(i32 %85, i8* %86, i32 4096)
store i64 %87, i64* %14
%88 = load i64, i64* %14
%89 = icmp eq i64 %88, 0
br i1 %89, label %90, label %91
90: ; preds = %84
br label %101
91: ; preds = %84
%92 = load i32, i32* %8
%93 = getelementptr inbounds [4096 x i8], [4096 x i8]* %13, i64 0, i64 0
%94 = load i64, i64* %14
%95 = call i64 @write(i32 %92, i8* %93, i64 %94)
%96 = load i64, i64* %14
%97 = icmp ne i64 %95, %96
br i1 %97, label %98, label %100
98: ; preds = %91
%99 = call i32 @errout(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %100
100: ; preds = %98, %91
br label %84
101: ; preds = %90
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [41 x i8] c"usage: minitcpsrvr ip-addr port outfile\0A\00"
@.str.1 = [2 x i8] c"-\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [5 x i8] c"bind\00"
@.str.3 = [7 x i8] c"listen\00"
@.str.4 = [6 x i8] c"write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [4096 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%11 = icmp eq i32 %0, 4
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 3
%18 = load i8*, i8** %17
%19 = tail call i32 @strcmp(i8* dereferenceable(1) %18, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32 %19, 0
br i1 %20, label %35, label %21
21: ; preds = %16
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_CREAT
%24 = or i32 %23, %22
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IWUSR
%29 = or i32 %28, %27
%30 = tail call i32 @open(i8* %18, i32 %26, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %21
%33 = load i8*, i8** %17
%34 = tail call i32 @errout(i8* %33)
br label %35
35: ; preds = %16, %21, %32
%36 = phi i32 [ -1, %32 ], [ %30, %21 ], [ 1, %16 ]
%37 = load i32, i32* @AF_INET
%38 = load i32, i32* @SOCK_STREAM
%39 = tail call i32 @socket(i32 %37, i32 %38, i32 0)
%40 = load i32, i32* @AF_INET
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %40, i32* %41
%42 = getelementptr inbounds i8*, i8** %1, i64 1
%43 = load i8*, i8** %42
%44 = tail call i32 @inet_addr(i8* %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %44, i32* %45
%46 = getelementptr inbounds i8*, i8** %1, i64 2
%47 = load i8*, i8** %46
%48 = tail call i32 @atoi(i8* %47)
%49 = tail call i32 @htons(i32 %48)
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %49, i32* %50
%51 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%52 = call i64 @bind(i32 %39, %struct.sockaddr* %51, i32 12)
%53 = icmp eq i64 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %35
%55 = call i32 @errout(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %54, %35
%57 = call i64 @listen(i32 %39, i32 20)
%58 = icmp eq i64 %57, 0
br i1 %58, label %61, label %59
59: ; preds = %56
%60 = call i32 @errout(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %59, %56
store i32 12, i32* %5
%62 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%63 = call i32 @accept(i32 %39, %struct.sockaddr* %62, i32* %5)
%64 = call i64 @read(i32 %63, i8* %10, i32 4096)
%65 = icmp eq i64 %64, 0
br i1 %65, label %75, label %66
66: ; preds = %61, %72
%67 = phi i64 [ %73, %72 ], [ %64, %61 ]
%68 = call i64 @write(i32 %36, i8* %10, i64 %67)
%69 = icmp eq i64 %68, %67
br i1 %69, label %72, label %70
70: ; preds = %66
%71 = call i32 @errout(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %72
72: ; preds = %70, %66
%73 = call i64 @read(i32 %63, i8* %10, i32 4096)
%74 = icmp eq i64 %73, 0
br i1 %74, label %75, label %66
75: ; preds = %72, %61
ret i32 0
}