#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*/ SEEK_CUR;
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, int);
int read(int, ...);
int find_user_note(int fd, int user_id) {
  int note_uid = -1;
  unsigned char byte;
  int length;
  while (note_uid != user_id) {
    if (read(fd, &note_uid, 4) != 4)
      return -1;
    if (read(fd, &byte, 1) != 1)
      return -1;
    byte = length = 0;
    while (byte != '\n') {
      if (read(fd, &byte, 1) != 1)
        return -1;
      length++;
    }
  }
  lseek(fd, length * -1, SEEK_CUR);
  printf("[DEBUG] found a %d byte note for user id %d\n", length, note_uid);
  return length;
}


x86 assembly (clang, -O0):
<find_user_note>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11f4 <find_user_note+0xc4>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <read@plt>
cmp    $0x4,%eax
je     1179 <find_user_note+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <find_user_note+0xf1>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1050 <read@plt>
cmp    $0x1,%eax
je     11a1 <find_user_note+0x71>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <find_user_note+0xf1>
movl   $0x0,-0x18(%rbp)
movb   $0x0,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
cmp    $0xa,%eax
je     11ef <find_user_note+0xbf>
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
mov    $0x0,%al
call   1050 <read@plt>
cmp    $0x1,%eax
je     11e1 <find_user_note+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1221 <find_user_note+0xf1>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ac <find_user_note+0x7c>
jmp    1145 <find_user_note+0x15>
mov    -0x8(%rbp),%edi
imul   $0xffffffff,-0x18(%rbp),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xdea(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_user_note>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
movl   $0xffffffff,0x4(%rsp)
ff
cmp    $0xffffffff,%esi
je     11b9 <find_user_note+0x89>
mov    %esi,%r15d
lea    0x4(%rsp),%r14
lea    0x3(%rsp),%r13
mov    $0xffffffff,%r12d
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <read@plt>
cmp    $0x4,%eax
jne    11e2 <find_user_note+0xb2>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1050 <read@plt>
cmp    $0x1,%eax
jne    11e2 <find_user_note+0xb2>
movb   $0x0,0x3(%rsp)
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x1,%edx
xor    %eax,%eax
call   1050 <read@plt>
cmp    $0x1,%eax
jne    11e2 <find_user_note+0xb2>
add    $0x1,%ebp
cmpb   $0xa,0x3(%rsp)
jne    1190 <find_user_note+0x60>
cmp    %r15d,0x4(%rsp)
jne    115d <find_user_note+0x2d>
jmp    11b9 <find_user_note+0x89>
mov    %ebp,%esi
neg    %esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1040 <lseek@plt>
mov    0x4(%rsp),%edx
lea    0xe28(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %ebp,%r12d
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
@.str = [45 x i8] c"[DEBUG] found a %d byte note for user id %d\0A\00"
define i32 @find_user_note(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 -1, i32* %6
br label %9
9: ; preds = %36, %2
%10 = load i32, i32* %6
%11 = load i32, i32* %5
%12 = icmp ne i32 %10, %11
br i1 %12, label %13, label %37
13: ; preds = %9
%14 = load i32, i32* %4
%15 = call i32 (i32, ...) @read(i32 %14, i32* %6, i32 4)
%16 = icmp ne i32 %15, 4
br i1 %16, label %17, label %18
17: ; preds = %13
store i32 -1, i32* %3
br label %47
18: ; preds = %13
%19 = load i32, i32* %4
%20 = call i32 (i32, ...) @read(i32 %19, i8* %7, i32 1)
%21 = icmp ne i32 %20, 1
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -1, i32* %3
br label %47
23: ; preds = %18
store i32 0, i32* %8
store i8 0, i8* %7
br label %24
24: ; preds = %33, %23
%25 = load i8, i8* %7
%26 = zext i8 %25 to i32
%27 = icmp ne i32 %26, 10
br i1 %27, label %28, label %36
28: ; preds = %24
%29 = load i32, i32* %4
%30 = call i32 (i32, ...) @read(i32 %29, i8* %7, i32 1)
%31 = icmp ne i32 %30, 1
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 -1, i32* %3
br label %47
33: ; preds = %28
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %24
36: ; preds = %24
br label %9
37: ; preds = %9
%38 = load i32, i32* %4
%39 = load i32, i32* %8
%40 = mul nsw i32 %39, -1
%41 = load i32, i32* @SEEK_CUR
%42 = call i32 @lseek(i32 %38, i32 %40, i32 %41)
%43 = load i32, i32* %8
%44 = load i32, i32* %6
%45 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %43, i32 %44)
%46 = load i32, i32* %8
store i32 %46, i32* %3
br label %47
47: ; preds = %37, %32, %22, %17
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@.str = [45 x i8] c"[DEBUG] found a %d byte note for user id %d\0A\00"
define i32 @find_user_note(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8
%5 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
%6 = icmp eq i32 %1, -1
br i1 %6, label %25, label %10
7: ; preds = %21
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, %1
br i1 %9, label %25, label %10
10: ; preds = %2, %7
%11 = call i32 (i32, ...) @read(i32 %0, i32* %3, i32 4)
%12 = icmp eq i32 %11, 4
br i1 %12, label %13, label %32
13: ; preds = %10
%14 = call i32 (i32, ...) @read(i32 %0, i8* %4, i32 1)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %32
16: ; preds = %13
store i8 0, i8* %4
br label %17
17: ; preds = %16, %21
%18 = phi i32 [ 0, %16 ], [ %22, %21 ]
%19 = call i32 (i32, ...) @read(i32 %0, i8* %4, i32 1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = add nuw nsw i32 %18, 1
%23 = load i8, i8* %4
%24 = icmp eq i8 %23, 10
br i1 %24, label %7, label %17
25: ; preds = %7, %2
%26 = phi i32 [ undef, %2 ], [ %22, %7 ]
%27 = sub nsw i32 0, %26
%28 = load i32, i32* @SEEK_CUR
%29 = call i32 @lseek(i32 %0, i32 %27, i32 %28)
%30 = load i32, i32* %3
%31 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %26, i32 %30)
br label %32
32: ; preds = %13, %10, %17, %25
%33 = phi i32 [ %26, %25 ], [ -1, %17 ], [ -1, %10 ], [ -1, %13 ]
ret i32 %33
}