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

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int write(int, char *, int);
int serve_new_conn(int sk) {
  int rd, wr;
  char buf[1024];
  printf("New connection\n");
  while (1) {
    rd = read(sk, buf, sizeof(buf));
    if (!rd)
      break;
    if (rd < 0) {
      perror("Can't read socket");
      return 1;
    }
    wr = 0;
    while (wr < rd) {
      int w;
      w = write(sk, buf + wr, rd - wr);
      if (w <= 0) {
        perror("Can't write socket");
        return 1;
      }
      wr += w;
    }
  }
  printf("Done\n");
  return 0;
}


x86 assembly (clang, -O0):
<serve_new_conn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x8(%rbp)
lea    0xeab(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1180 <serve_new_conn+0x40>
jmp    1212 <serve_new_conn+0xd2>
cmpl   $0x0,-0xc(%rbp)
jge    11a2 <serve_new_conn+0x62>
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1225 <serve_new_conn+0xe5>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    120d <serve_new_conn+0xcd>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
sub    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jg     11fc <serve_new_conn+0xbc>
lea    0xe37(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1225 <serve_new_conn+0xe5>
mov    -0x414(%rbp),%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11a9 <serve_new_conn+0x69>
jmp    115a <serve_new_conn+0x1a>
lea    0xe1c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serve_new_conn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edi,%r15d
lea    0xece(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
je     11c3 <serve_new_conn+0x83>
mov    %eax,%ebp
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nop
test   %ebp,%ebp
js     11f2 <serve_new_conn+0xb2>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    %ebp,%edx
sub    %eax,%edx
mov    %r15d,%edi
call   1040 <write@plt>
test   %eax,%eax
jle    11d3 <serve_new_conn+0x93>
add    %eax,%ebx
cmp    %ebx,%ebp
jg     1190 <serve_new_conn+0x50>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1180 <serve_new_conn+0x40>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    11e4 <serve_new_conn+0xa4>
lea    0xe38(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe07(%rip),%rdi
jmp    11da <serve_new_conn+0x9a>

LLVM IR (-O0):
@.str = [16 x i8] c"New connection\0A\00"
@.str.1 = [18 x i8] c"Can't read socket\00"
@.str.2 = [19 x i8] c"Can't write socket\00"
@.str.3 = [6 x i8] c"Done\0A\00"
define i32 @serve_new_conn(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
store i32 %0, i32* %3
%8 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %1, %44
%10 = load i32, i32* %3
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 1024)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %9
br label %45
16: ; preds = %9
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %47
21: ; preds = %16
store i32 0, i32* %5
br label %22
22: ; preds = %40, %21
%23 = load i32, i32* %5
%24 = load i32, i32* %4
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %44
26: ; preds = %22
%27 = load i32, i32* %3
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i32, i32* %4
%33 = load i32, i32* %5
%34 = sub nsw i32 %32, %33
%35 = call i32 @write(i32 %27, i8* %31, i32 %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp sle i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %26
%39 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %47
40: ; preds = %26
%41 = load i32, i32* %7
%42 = load i32, i32* %5
%43 = add nsw i32 %42, %41
store i32 %43, i32* %5
br label %22
44: ; preds = %22
br label %9
45: ; preds = %15
%46 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %47
47: ; preds = %45, %38, %19
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"Can't read socket\00"
@.str.2 = [19 x i8] c"Can't write socket\00"
@str = [15 x i8] c"New connection\00"
@str.4 = [5 x i8] c"Done\00"
define i32 @serve_new_conn(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%5 = call i32 @read(i32 %0, i8* %3, i32 1024)
%6 = icmp eq i32 %5, 0
br i1 %6, label %27, label %10
7: ; preds = %15
%8 = call i32 @read(i32 %0, i8* %3, i32 1024)
%9 = icmp eq i32 %8, 0
br i1 %9, label %27, label %10
10: ; preds = %1, %7
%11 = phi i32 [ %8, %7 ], [ %5, %1 ]
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %29
15: ; preds = %17
%16 = icmp sgt i32 %11, %24
br i1 %16, label %17, label %7
17: ; preds = %10, %15
%18 = phi i32 [ %24, %15 ], [ 0, %10 ]
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %19
%21 = sub nsw i32 %11, %18
%22 = call i32 @write(i32 %0, i8* %20, i32 %21)
%23 = icmp slt i32 %22, 1
%24 = add nsw i32 %22, %18
br i1 %23, label %25, label %15
25: ; preds = %17
%26 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %29
27: ; preds = %7, %1
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.4, i64 0, i64 0))
br label %29
29: ; preds = %25, %27, %13
%30 = phi i32 [ 1, %13 ], [ 0, %27 ], [ 1, %25 ]
ret i32 %30
}