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

/* Variables and functions */
int /*<<< orphan*/ fail_printf(char *, int, int);
int read(int, int *, int);
int /*<<< orphan*/ sysf_printf(char *, char *);
int sync_wait(int fd, int seq) {
  int rc;
  int sync = -1;
  rc = read(fd, &sync, sizeof(sync));
  do {
    if ((rc < 0))
      sysf_printf("Error reading from socket", "");
  } while (0);
  if (!rc)
    return 0;
  if (sync != seq)
    fail_printf("Invalid sync sequence: %d != %d", seq, sync);
  return 0;
}


x86 assembly (clang, -O0):
<sync_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1176 <sync_wait+0x46>
lea    0xe96(%rip),%rdi
lea    0xec8(%rip),%rsi
call   1040 <sysf_printf@plt>
jmp    117b <sync_wait+0x4b>
cmpl   $0x0,-0x10(%rbp)
jne    1191 <sync_wait+0x61>
movl   $0x0,-0x4(%rbp)
jmp    11b6 <sync_wait+0x86>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11af <sync_wait+0x7f>
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe70(%rip),%rdi
call   1050 <fail_printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sync_wait>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
movl   $0xffffffff,0xc(%rsp)
ff
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1156 <sync_wait+0x26>
jne    1169 <sync_wait+0x39>
jmp    117f <sync_wait+0x4f>
lea    0xea3(%rip),%rdi
lea    0xed5(%rip),%rsi
call   1040 <sysf_printf@plt>
mov    0xc(%rsp),%edx
cmp    %ebx,%edx
je     117f <sync_wait+0x4f>
lea    0xea2(%rip),%rdi
mov    %ebx,%esi
call   1050 <fail_printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [26 x i8] c"Error reading from socket\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [32 x i8] c"Invalid sync sequence: %d != %d\00"
define i32 @sync_wait(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 -1, i32* %7
%8 = load i32, i32* %4
%9 = call i32 @read(i32 %8, i32* %7, i32 4)
store i32 %9, i32* %6
br label %10
10: ; preds = %2
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = call i32 @sysf_printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
br label %16
16: ; preds = %15
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %16
store i32 0, i32* %3
br label %29
20: ; preds = %16
%21 = load i32, i32* %7
%22 = load i32, i32* %5
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %28
24: ; preds = %20
%25 = load i32, i32* %5
%26 = load i32, i32* %7
%27 = call i32 @fail_printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %26)
br label %28
28: ; preds = %24, %20
store i32 0, i32* %3
br label %29
29: ; preds = %28, %19
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [26 x i8] c"Error reading from socket\00"
@.str.1 = [1 x i8] zeroinitializer
@.str.2 = [32 x i8] c"Invalid sync sequence: %d != %d\00"
define i32 @sync_wait(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
%5 = call i32 @read(i32 %0, i32* %3, i32 4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @sysf_printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
br label %11
9: ; preds = %2
%10 = icmp eq i32 %5, 0
br i1 %10, label %16, label %11
11: ; preds = %7, %9
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, %1
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @fail_printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %12)
br label %16
16: ; preds = %11, %14, %9
ret i32 0
}