#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 */

/* Variables and functions */
scalar_t__ ECONNRESET;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int tcp_main(int client_fd) {
  char buf[512];
  int n = read(client_fd, buf, 512);
  if (n < 0) {
    if (errno == ECONNRESET)
      return 0;
    perror("read error");
    exit(EXIT_FAILURE);
  }
  if (n == 0)
    return 0;
  buf[n] = '\0';
  printf("%s", buf);
  return 1;
}


x86 assembly (clang, -O0):
<tcp_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %eax,-0x214(%rbp)
cmpl   $0x0,-0x214(%rbp)
jge    11b5 <tcp_main+0x75>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
jne    119b <tcp_main+0x5b>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <tcp_main+0xb7>
lea    0xe5e(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x214(%rbp)
jne    11ce <tcp_main+0x8e>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <tcp_main+0xb7>
movslq -0x214(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
lea    -0x210(%rbp),%rsi
lea    0xe20(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_main>:
sub    $0x208,%rsp
mov    %rsp,%rsi
mov    $0x200,%edx
call   1040 <read@plt>
test   %eax,%eax
js     1176 <tcp_main+0x36>
je     1196 <tcp_main+0x56>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xea4(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
mov    $0x1,%eax
jmp    118e <tcp_main+0x4e>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rcx
mov    0x2e59(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
jne    11a0 <tcp_main+0x60>
add    $0x208,%rsp
ret
xor    %eax,%eax
add    $0x208,%rsp
ret
lea    0xe59(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@ECONNRESET = global i64 0
@.str = [11 x i8] c"read error\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i32 @tcp_main(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%8 = call i32 @read(i32 %6, i8* %7, i32 512)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @ECONNRESET
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 0, i32* %2
br label %30
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %1
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 0, i32* %2
br label %30
24: ; preds = %20
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%29 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %28)
store i32 1, i32* %2
br label %30
30: ; preds = %24, %23, %15
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@errno = global i64 0
@ECONNRESET = global i64 0
@.str = [11 x i8] c"read error\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [3 x i8] c"%s\00"
define i32 @tcp_main(i32 %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 512)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ECONNRESET
%9 = icmp eq i64 %7, %8
br i1 %9, label %20, label %10
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
%15 = icmp eq i32 %4, 0
br i1 %15, label %20, label %16
16: ; preds = %14
%17 = zext i32 %4 to i64
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %17
store i8 0, i8* %18
%19 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %20
20: ; preds = %14, %6, %16
%21 = phi i32 [ 1, %16 ], [ 0, %6 ], [ 0, %14 ]
ret i32 %21
}