#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 */
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 int socklen_t;
typedef int /*<<< orphan*/ si_server;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int atoi(char *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int create_file(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  struct sockaddr_in si_server, cli_addr;
  char buf[5];
  int len;
  int n = 1;
  int PORTNO = atoi(argv[1]);
  int connection;
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  bzero((char *)&si_server, sizeof(si_server));
  si_server.sin_family = AF_INET;
  si_server.sin_addr.s_addr = htonl(INADDR_ANY);
  si_server.sin_port = htons(PORTNO);
  socklen_t cli_len;
  bind(sockfd, (struct sockaddr *)&si_server, sizeof(si_server));
  listen(sockfd, 5);
  cli_len = sizeof(cli_addr);
  connection = accept(sockfd, (struct sockaddr *)&cli_addr, &cli_len);
  printf("%d\n", connection);
  int fp = create_file(connection);
  if (!fp)
    error("ERROR CREATE");
  for (;;) {
    int nbytes = read(connection, buf, 5);
    buf[5] = '\0';
    if (!nbytes)
      break;
    write(fp, buf, nbytes);
  }
  close(sockfd);
  close(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x44(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x4c(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x44(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    -0x4c(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0xc,-0x50(%rbp)
mov    -0x4c(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%esi
lea    0xd5f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%edi
call   10d0 <create_file@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jne    12c7 <main+0xe7>
lea    0xd42(%rip),%rdi
call   10b0 <error@plt>
jmp    12cc <main+0xec>
mov    -0x48(%rbp),%edi
lea    -0x35(%rbp),%rsi
mov    $0x5,%edx
call   1080 <read@plt>
mov    %eax,-0x58(%rbp)
movb   $0x0,-0x30(%rbp)
cmpl   $0x0,-0x58(%rbp)
jne    12f3 <main+0x113>
jmp    1307 <main+0x127>
mov    -0x54(%rbp),%edi
lea    -0x35(%rbp),%rsi
mov    -0x58(%rbp),%edx
call   1030 <write@plt>
jmp    12cc <main+0xec>
mov    -0x4c(%rbp),%edi
call   1070 <close@plt>
mov    -0x54(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x8(%rsi),%rdi
call   10f0 <atoi@plt>
mov    %eax,%ebx
mov    0x2dec(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%r14d
lea    0x18(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x20(%rsp)
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x1c(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %r14d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0xc,0x14(%rsp)
lea    0x28(%rsp),%rsi
lea    0x14(%rsp),%rdx
mov    %r14d,%edi
call   10e0 <accept@plt>
mov    %eax,%r15d
lea    0xd77(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %r15d,%edi
call   10d0 <create_file@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    12aa <main+0xca>
lea    0xd5f(%rip),%rdi
call   10b0 <error@plt>
lea    0xf(%rsp),%rsi
mov    %r15d,%edi
mov    $0x5,%edx
call   1080 <read@plt>
movb   $0x0,0x14(%rsp)
test   %eax,%eax
je     12f5 <main+0x115>
lea    0xf(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x5,%edx
call   1080 <read@plt>
movb   $0x0,0x14(%rsp)
test   %eax,%eax
jne    12d0 <main+0xf0>
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [13 x i8] c"ERROR CREATE\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca [5 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %10
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %11
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %13
%24 = bitcast %struct.sockaddr_in* %6 to i8*
%25 = call i32 @bzero(i8* %24, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i32, i32* @INADDR_ANY
%29 = call i32 @htonl(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%31 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %30, i32 0, i32 0
store i32 %29, i32* %31
%32 = load i32, i32* %11
%33 = call i32 @htons(i32 %32)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %33, i32* %34
%35 = load i32, i32* %13
%36 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%37 = call i32 @bind(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = load i32, i32* %13
%39 = call i32 @listen(i32 %38, i32 5)
store i32 12, i32* %14
%40 = load i32, i32* %13
%41 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%42 = call i32 @accept(i32 %40, %struct.sockaddr* %41, i32* %14)
store i32 %42, i32* %12
%43 = load i32, i32* %12
%44 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %12
%46 = call i32 @create_file(i32 %45)
store i32 %46, i32* %15
%47 = load i32, i32* %15
%48 = icmp ne i32 %47, 0
br i1 %48, label %51, label %49
49: ; preds = %2
%50 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %49, %2
br label %52
52: ; preds = %60, %51
%53 = load i32, i32* %12
%54 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%55 = call i32 @read(i32 %53, i8* %54, i32 5)
store i32 %55, i32* %16
%56 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 5
store i8 0, i8* %56
%57 = load i32, i32* %16
%58 = icmp ne i32 %57, 0
br i1 %58, label %60, label %59
59: ; preds = %52
br label %65
60: ; preds = %52
%61 = load i32, i32* %15
%62 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%63 = load i32, i32* %16
%64 = call i32 @write(i32 %61, i8* %62, i32 %63)
br label %52
65: ; preds = %59
%66 = load i32, i32* %13
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* %15
%69 = call i32 @close(i32 %68)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [13 x i8] c"ERROR CREATE\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [5 x i8]
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = call i32 @bzero(i8* %7, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = tail call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 %12)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast i32* %6 to i8*
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @bind(i32 %15, %struct.sockaddr* %25, i32 12)
%27 = call i32 @listen(i32 %15, i32 5)
store i32 12, i32* %6
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = call i32 @accept(i32 %15, %struct.sockaddr* %28, i32* %6)
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = call i32 @create_file(i32 %29)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %2
%34 = call i32 @error(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %2
%36 = call i32 @read(i32 %29, i8* %9, i32 5)
%37 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 5
store i8 0, i8* %37
%38 = icmp eq i32 %36, 0
br i1 %38, label %44, label %39
39: ; preds = %35, %39
%40 = phi i32 [ %42, %39 ], [ %36, %35 ]
%41 = call i32 @write(i32 %31, i8* %9, i32 %40)
%42 = call i32 @read(i32 %29, i8* %9, i32 5)
store i8 0, i8* %37
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %39
44: ; preds = %39, %35
%45 = call i32 @close(i32 %15)
%46 = call i32 @close(i32 %31)
ret i32 0
}