#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 read(int, char *, int);
int readstring(int fd, char *buf, int n) {
  buf[0] = 0;
  while (n--) {
    if (read(fd, buf, 1) != 1)
      return 0;
    if (*buf++ == 0)
      return 1;
    *buf = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<readstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     1196 <readstring+0x86>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1163 <readstring+0x53>
movl   $0x0,-0x4(%rbp)
jmp    119d <readstring+0x8d>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    118a <readstring+0x7a>
movl   $0x1,-0x4(%rbp)
jmp    119d <readstring+0x8d>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    1129 <readstring+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readstring>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movb   $0x0,(%rsi)
xor    %r14d,%r14d
test   %edx,%edx
je     1161 <readstring+0x51>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1161 <readstring+0x51>
cmpb   $0x0,(%rbx)
je     115b <readstring+0x4b>
add    $0xffffffff,%ebp
movb   $0x0,0x1(%rbx)
add    $0x1,%rbx
test   %ebp,%ebp
jne    1130 <readstring+0x20>
jmp    1161 <readstring+0x51>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readstring(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%8 = load i8*, i8** %6
%9 = getelementptr inbounds i8, i8* %8, i64 0
store i8 0, i8* %9
br label %10
10: ; preds = %27, %3
%11 = load i32, i32* %7
%12 = add nsw i32 %11, -1
store i32 %12, i32* %7
%13 = icmp ne i32 %11, 0
br i1 %13, label %14, label %29
14: ; preds = %10
%15 = load i32, i32* %5
%16 = load i8*, i8** %6
%17 = call i32 @read(i32 %15, i8* %16, i32 1)
%18 = icmp ne i32 %17, 1
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 0, i32* %4
br label %30
20: ; preds = %14
%21 = load i8*, i8** %6
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %6
%23 = load i8, i8* %21
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
store i32 1, i32* %4
br label %30
27: ; preds = %20
%28 = load i8*, i8** %6
store i8 0, i8* %28
br label %10
29: ; preds = %10
store i32 0, i32* %4
br label %30
30: ; preds = %29, %26, %19
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
define i32 @readstring(i32 %0, i8* nocapture %1, i32 %2) {
store i8 0, i8* %1
%4 = icmp eq i32 %2, 0
br i1 %4, label %17, label %5
5: ; preds = %3, %15
%6 = phi i32 [ %8, %15 ], [ %2, %3 ]
%7 = phi i8* [ %12, %15 ], [ %1, %3 ]
%8 = add nsw i32 %6, -1
%9 = tail call i32 @read(i32 %0, i8* %7, i32 1)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %17
11: ; preds = %5
%12 = getelementptr inbounds i8, i8* %7, i64 1
%13 = load i8, i8* %7
%14 = icmp eq i8 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11
store i8 0, i8* %12
%16 = icmp eq i32 %8, 0
br i1 %16, label %17, label %5
17: ; preds = %5, %11, %15, %3
%18 = phi i32 [ 0, %3 ], [ 0, %15 ], [ 1, %11 ], [ 0, %5 ]
ret i32 %18
}