#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
unsigned long long strtoull(char *, int /*<<< orphan*/ *, int);
int write(int, char *, int);
unsigned long long sendSync(int fd) {
  char buf[4096], *p;
  ssize_t nread;
  if (write(fd, "SYNC\r\n", 6) != 6) {
    fprintf(stderr, "Error writing to master\n");
    exit(1);
  }
  p = buf;
  while (1) {
    nread = read(fd, p, 1);
    if (nread <= 0) {
      fprintf(stderr, "Error reading bulk length while SYNCing\n");
      exit(1);
    }
    if (*p == '\n' && p != buf)
      break;
    if (*p != '\n')
      p++;
  }
  *p = '\0';
  if (buf[0] == '-') {
    printf("SYNC with master failed: %s\n", buf);
    exit(1);
  }
  return strtoull(buf + 1, NULL, 10);
}


x86 assembly (clang, -O0):
<sendSync>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    0xe88(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
cmp    $0x6,%eax
je     11aa <sendSync+0x4a>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1018(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x1018(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
jg     1200 <sendSync+0xa0>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x1018(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    122f <sendSync+0xcf>
mov    -0x1018(%rbp),%rax
lea    -0x1010(%rbp),%rcx
cmp    %rcx,%rax
je     122f <sendSync+0xcf>
jmp    1259 <sendSync+0xf9>
mov    -0x1018(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     1254 <sendSync+0xf4>
mov    -0x1018(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1018(%rbp)
jmp    11b8 <sendSync+0x58>
mov    -0x1018(%rbp),%rax
movb   $0x0,(%rax)
movsbl -0x1010(%rbp),%eax
cmp    $0x2d,%eax
jne    1290 <sendSync+0x130>
lea    -0x1010(%rbp),%rsi
lea    0xdc8(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x1010(%rbp),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtoull@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendSync>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %edi,%ebp
lea    0xe8c(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
cmp    $0x6,%eax
jne    121f <sendSync+0xbf>
mov    %rsp,%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    1200 <sendSync+0xa0>
mov    %r14,%rbx
jmp    11ce <sendSync+0x6e>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %ecx,%ecx
cmp    $0xa,%al
setne  %cl
add    %rcx,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
jle    1200 <sendSync+0xa0>
movzbl (%rbx),%eax
cmp    %r14,%rbx
je     11b0 <sendSync+0x50>
cmp    $0xa,%al
jne    11b0 <sendSync+0x50>
movb   $0x0,(%rbx)
cmpb   $0x2d,(%rsp)
je     1231 <sendSync+0xd1>
lea    0x1(%rsp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtoull@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe10(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
jmp    1210 <sendSync+0xb0>
lea    0xe11(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"SYNC\0D\0A\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Error writing to master\0A\00"
@.str.2 = [41 x i8] c"Error reading bulk length while SYNCing\0A\00"
@.str.3 = [29 x i8] c"SYNC with master failed: %s\0A\00"
define i64 @sendSync(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca i8*
%5 = alloca i64
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @write(i32 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%8 = icmp ne i32 %7, 6
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
store i8* %14, i8** %4
br label %15
15: ; preds = %13, %43
%16 = load i32, i32* %2
%17 = load i8*, i8** %4
%18 = call i64 @read(i32 %16, i8* %17, i32 1)
store i64 %18, i64* %5
%19 = load i64, i64* %5
%20 = icmp sle i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
%26 = load i8*, i8** %4
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i8*, i8** %4
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%33 = icmp ne i8* %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30
br label %44
35: ; preds = %30, %25
%36 = load i8*, i8** %4
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp ne i32 %38, 10
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %4
br label %43
43: ; preds = %40, %35
br label %15
44: ; preds = %34
%45 = load i8*, i8** %4
store i8 0, i8* %45
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 45
br i1 %49, label %50, label %54
50: ; preds = %44
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%52 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %44
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%56 = getelementptr inbounds i8, i8* %55, i64 1
%57 = call i64 @strtoull(i8* %56, i32* null, i32 10)
ret i64 %57
}

LLVM IR (-O2):
@.str = [7 x i8] c"SYNC\0D\0A\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Error writing to master\0A\00"
@.str.2 = [41 x i8] c"Error reading bulk length while SYNCing\0A\00"
@.str.3 = [29 x i8] c"SYNC with master failed: %s\0A\00"
define i64 @sendSync(i32 %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @write(i32 %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%5 = icmp eq i32 %4, 6
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = call i64 @read(i32 %0, i8* %3, i32 1)
%8 = icmp slt i64 %7, 1
br i1 %8, label %13, label %17
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %23, %6
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %6, %23
%18 = phi i8* [ %26, %23 ], [ %3, %6 ]
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 10
%21 = icmp ne i8* %18, %3
%22 = and i1 %21, %20
br i1 %22, label %29, label %23
23: ; preds = %17
%24 = xor i1 %20, true
%25 = zext i1 %24 to i64
%26 = getelementptr i8, i8* %18, i64 %25
%27 = call i64 @read(i32 %0, i8* %26, i32 1)
%28 = icmp slt i64 %27, 1
br i1 %28, label %13, label %17
29: ; preds = %17
store i8 0, i8* %18
%30 = load i8, i8* %3
%31 = icmp eq i8 %30, 45
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 1
%37 = call i64 @strtoull(i8* nocapture %36, i32* null, i32 10)
ret i64 %37
}