#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*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int write(int /*<<< orphan*/, char *, int);
int main() {
  int r = write(0, "hello", 5);
  printf("%d\n", r);
  char buf[5];
  r = read(0, buf, 3);
  printf("===%d\n", r);
  if (r == 0) {
    printf("===%d\n", r);
    buf[r] = 0;
    printf("%s\n", buf);
  }
  if (r > 0) {
    buf[r] = 0;
    printf("%s\n", buf);
  }
  if (r == -1) {
    printf("error:%m\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
lea    0xeb8(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xd(%rbp),%rsi
xor    %edi,%edi
mov    $0x3,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    11c0 <main+0x90>
mov    -0x8(%rbp),%esi
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movslq -0x8(%rbp),%rax
movb   $0x0,-0xd(%rbp,%rax,1)
lea    -0xd(%rbp),%rsi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jle    11e5 <main+0xb5>
movslq -0x8(%rbp),%rax
movb   $0x0,-0xd(%rbp,%rax,1)
lea    -0xd(%rbp),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0xffffffff,-0x8(%rbp)
jne    11fd <main+0xcd>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb4(%rip),%rsi
xor    %edi,%edi
mov    $0x5,%edx
call   1040 <write@plt>
lea    0xeaa(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xb(%rsp),%rsi
xor    %edi,%edi
mov    $0x3,%edx
call   1060 <read@plt>
mov    %eax,%ebx
lea    0xe84(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
je     119a <main+0x5a>
jle    11c1 <main+0x81>
mov    %ebx,%eax
movb   $0x0,0xb(%rsp,%rax,1)
jmp    11af <main+0x6f>
lea    0xe65(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movb   $0x0,0xb(%rsp)
lea    0xb(%rsp),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
cmp    $0xffffffff,%ebx
jne    11b9 <main+0x79>
lea    0xe40(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11b9 <main+0x79>

LLVM IR (-O0):
@.str = [6 x i8] c"hello\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [7 x i8] c"===%d\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [10 x i8] c"error:%m\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [5 x i8]
store i32 0, i32* %1
%4 = call i32 @write(i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%8 = call i32 @read(i32 0, i8* %7, i32 3)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 %17
store i8 0, i8* %18
%19 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %13, %0
%22 = load i32, i32* %2
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i32, i32* %2
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %24, %21
%31 = load i32, i32* %2
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"hello\00"
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [7 x i8] c"===%d\0A\00"
@.str.4 = [10 x i8] c"error:%m\0A\00"
define i32 @main() {
%1 = alloca [5 x i8]
%2 = tail call i32 @write(i32 0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%5 = call i32 @read(i32 0, i8* %4, i32 3)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%7 = icmp eq i32 %5, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 0)
store i8 0, i8* %4
%10 = call i32 @puts(i8* %4)
br label %21
11: ; preds = %0
%12 = icmp sgt i32 %5, 0
br i1 %12, label %13, label %17
13: ; preds = %11
%14 = zext i32 %5 to i64
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @puts(i8* %4)
br label %21
17: ; preds = %11
%18 = icmp eq i32 %5, -1
br i1 %18, label %19, label %21
19: ; preds = %17
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0))
br label %21
21: ; preds = %13, %8, %19, %17
ret i32 0
}