#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, ...);
int main() {
  int r = write(0, "hello\n", 6);
  printf("%d\n", r);
  write(1, "world\n", 4);
  write(2, "mizu\n", 8);
  int a = 39;
  write(1, &a, 4);
  char buf[32];
  r = read(0, buf, 30);
  printf("------%d\n", r);
  if (r > 0) {
    buf[r] = 0;
    printf("::%s\n", buf);
  } else if (r == 0) {
    printf("ctrl+d");
  } else if (r == -1) {
    printf("ERROR");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
lea    0xeb8(%rip),%rsi
mov    $0x6,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
lea    0xe93(%rip),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <write@plt>
mov    $0x2,%edi
lea    0xe82(%rip),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1030 <write@plt>
movl   $0x27,-0xc(%rbp)
mov    $0x1,%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1030 <write@plt>
lea    -0x30(%rbp),%rsi
xor    %edi,%edi
mov    $0x1e,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jle    1202 <main+0xd2>
movslq -0x8(%rbp),%rax
movb   $0x0,-0x30(%rbp,%rax,1)
lea    -0x30(%rbp),%rsi
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1241 <main+0x111>
cmpl   $0x0,-0x8(%rbp)
jne    121f <main+0xef>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123c <main+0x10c>
cmpl   $0xffffffff,-0x8(%rbp)
jne    1237 <main+0x107>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    123c <main+0x10c>
jmp    1241 <main+0x111>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0xec4(%rip),%rsi
xor    %edi,%edi
mov    $0x6,%edx
xor    %eax,%eax
call   1030 <write@plt>
lea    0xec9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea6(%rip),%rsi
mov    $0x1,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1030 <write@plt>
lea    0xe95(%rip),%rsi
mov    $0x2,%edi
mov    $0x8,%edx
xor    %eax,%eax
call   1030 <write@plt>
movl   $0x27,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    $0x1,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1030 <write@plt>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
mov    $0x1e,%edx
call   1050 <read@plt>
mov    %eax,%ebx
lea    0xe52(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
jle    11eb <main+0xbb>
mov    %ebx,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe41(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    120b <main+0xdb>
cmp    $0xffffffff,%ebx
je     11fd <main+0xcd>
test   %ebx,%ebx
jne    120b <main+0xdb>
lea    0xe29(%rip),%rdi
jmp    1204 <main+0xd4>
lea    0xe27(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret

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

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