#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 */
int /*<<< orphan*/ EXIT_FAILURE;
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) {
    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    118f <tcp_main+0x4f>
lea    0xe84(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0x214(%rbp)
jne    11a8 <tcp_main+0x68>
movl   $0x0,-0x4(%rbp)
jmp    11d1 <tcp_main+0x91>
movslq -0x214(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
lea    -0x210(%rbp),%rsi
lea    0xe46(%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     1186 <tcp_main+0x46>
je     117c <tcp_main+0x3c>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
lea    0xea4(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
mov    $0x1,%eax
add    $0x208,%rsp
ret
xor    %eax,%eax
add    $0x208,%rsp
ret
lea    0xe73(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.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 %15
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
store i32 0, i32* %2
br label %25
19: ; preds = %15
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %21
store i8 0, i8* %22
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%24 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 1, i32* %2
br label %25
25: ; preds = %19, %18
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@.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 %10
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %1
%11 = icmp eq i32 %4, 0
br i1 %11, label %16, label %12
12: ; preds = %10
%13 = zext i32 %4 to i64
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %13
store i8 0, i8* %14
%15 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %16
16: ; preds = %10, %12
%17 = phi i32 [ 1, %12 ], [ 0, %10 ]
ret i32 %17
}