#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ gets(char *);
int getuid();
int open(char *, int /*<<< orphan*/);
int print_notes(int, int, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main() {
  int userid, printing = 1, fd;
  char searchstring[100];
  setbuf(stdout, NULL);
  setbuf(stdin, NULL);
  setbuf(stderr, NULL);
  printf("Search for: ");
  gets(searchstring);
  userid = getuid();
  fd = open("/var/notes", O_RDONLY);
  if (fd == -1)
    fatal("in main() while opening file for reading");
  printf("Searching for: \"");
  printf(searchstring);
  printf("\":\n");
  while (printing)
    printing = print_notes(fd, userid, searchstring);
  printf("-------[ end of note data ]-------\n");
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0xc(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
lea    0xe2a(%rip),%rdi
call   1060 <printf@plt>
lea    -0x80(%rbp),%rdi
call   1080 <gets@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x8(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    0xe0f(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    121c <main+0x9c>
lea    0xe01(%rip),%rdi
call   10a0 <fatal@plt>
lea    0xe1e(%rip),%rdi
call   1060 <printf@plt>
lea    -0x80(%rbp),%rdi
call   1060 <printf@plt>
lea    0xe1a(%rip),%rdi
call   1060 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
je     125e <main+0xde>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%esi
lea    -0x80(%rbp),%rdx
call   1030 <print_notes@plt>
mov    %eax,-0xc(%rbp)
jmp    123d <main+0xbd>
lea    0xdf1(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <setbuf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <setbuf@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <setbuf@plt>
lea    0xe31(%rip),%rdi
call   1070 <printf@plt>
mov    %rsp,%rdi
call   1090 <gets@plt>
xor    %eax,%eax
call   1050 <getuid@plt>
mov    %eax,%ebx
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0xe18(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    120d <main+0x7d>
lea    0xe10(%rip),%rdi
call   10b0 <fatal@plt>
lea    0xe2d(%rip),%rdi
call   1070 <printf@plt>
mov    %rsp,%r14
mov    %r14,%rdi
call   1070 <printf@plt>
lea    0xe27(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %r14,%rdx
call   1030 <print_notes@plt>
test   %eax,%eax
jne    1230 <main+0xa0>
lea    0xe0e(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@stdin = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"Search for: \00"
@.str.1 = [11 x i8] c"/var/notes\00"
@O_RDONLY = global i32 0
@.str.2 = [41 x i8] c"in main() while opening file for reading\00"
@.str.3 = [17 x i8] c"Searching for: \22\00"
@.str.4 = [4 x i8] c"\22:\0A\00"
@.str.5 = [36 x i8] c"-------[ end of note data ]-------\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [100 x i8]
store i32 0, i32* %1
store i32 1, i32* %3
%6 = load i32, i32* @stdout
%7 = call i32 @setbuf(i32 %6, i32* null)
%8 = load i32, i32* @stdin
%9 = call i32 @setbuf(i32 %8, i32* null)
%10 = load i32, i32* @stderr
%11 = call i32 @setbuf(i32 %10, i32* null)
%12 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%14 = call i32 @gets(i8* %13)
%15 = call i32 (...) @getuid()
store i32 %15, i32* %2
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %0
%23 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%25 = call i32 @printf(i8* %24)
%26 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %30, %22
%28 = load i32, i32* %3
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* %4
%32 = load i32, i32* %2
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%34 = call i32 @print_notes(i32 %31, i32 %32, i8* %33)
store i32 %34, i32* %3
br label %27
35: ; preds = %27
%36 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%37 = load i32, i32* %4
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %1
ret i32 %39
}

LLVM IR (-O2):
@stdout = global i32 0
@stdin = global i32 0
@stderr = global i32 0
@.str = [13 x i8] c"Search for: \00"
@.str.1 = [11 x i8] c"/var/notes\00"
@O_RDONLY = global i32 0
@.str.2 = [41 x i8] c"in main() while opening file for reading\00"
@.str.3 = [17 x i8] c"Searching for: \22\00"
@str = [3 x i8] c"\22:\00"
@str.6 = [35 x i8] c"-------[ end of note data ]-------\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdout
%4 = tail call i32 @setbuf(i32 %3, i32* null)
%5 = load i32, i32* @stdin
%6 = tail call i32 @setbuf(i32 %5, i32* null)
%7 = load i32, i32* @stderr
%8 = tail call i32 @setbuf(i32 %7, i32* null)
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @gets(i8* %2)
%11 = call i32 (...) @getuid()
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %0
%18 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @printf(i8* %2)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %17, %21
%22 = call i32 @print_notes(i32 %13, i32 %11, i8* %2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %21
24: ; preds = %21
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.6, i64 0, i64 0))
%26 = call i32 @close(i32 %13)
ret i32 0
}