#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 O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int write(int, char *, int);
int main() {
  int fd;
  fd = open("test.log", O_WRONLY | O_CREAT, 0640);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  char buff1[1024] = {0};
  printf("Input a string:\n");
  int ret;
  ret = read(0, buff1, 1024);
  if (ret < 0) {
    perror("read");
    close(fd);
    return -1;
  }
  printf("ret = %d\n", ret);
  ret = write(1, buff1, ret - 1);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
lea    0xe65(%rip),%rdi
mov    $0x1a0,%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ca <main+0x5a>
lea    0xe50(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1274 <main+0x104>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x410(%rbp),%rsi
xor    %edi,%edi
mov    $0x400,%edx
call   1070 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    1231 <main+0xc1>
lea    0xe07(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1274 <main+0x104>
mov    -0x414(%rbp),%esi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x410(%rbp),%rsi
mov    -0x414(%rbp),%edx
sub    $0x1,%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0x414(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2e3e(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5c(%rip),%rdi
mov    $0x1a0,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1222 <main+0xa2>
mov    %eax,%ebx
mov    %rsp,%rbp
xor    %r14d,%r14d
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
js     1236 <main+0xb6>
mov    %eax,%ebp
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%ebp
mov    %rsp,%rsi
mov    $0x1,%edi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1213 <main+0x93>
lea    0xdd1(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    120c <main+0x8c>

LLVM IR (-O0):
@.str = [9 x i8] c"test.log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [17 x i8] c"Input a string:\0A\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [10 x i8] c"ret = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i32 416)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %33
13: ; preds = %0
%14 = bitcast [1024 x i8]* %3 to i8*
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%17 = call i32 @read(i32 0, i8* %16, i32 1024)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* %2
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %1
br label %33
24: ; preds = %13
%25 = load i32, i32* %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%28 = load i32, i32* %4
%29 = sub nsw i32 %28, 1
%30 = call i32 @write(i32 1, i8* %27, i32 %29)
store i32 %30, i32* %4
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
store i32 0, i32* %1
br label %33
33: ; preds = %24, %20, %11
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.log\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.3 = [5 x i8] c"read\00"
@.str.4 = [10 x i8] c"ret = %d\0A\00"
@str = [16 x i8] c"Input a string:\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4, i32 416)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %23
9: ; preds = %0
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%12 = call i32 @read(i32 0, i8* %10, i32 1024)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %20
16: ; preds = %9
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %12)
%18 = add nsw i32 %12, -1
%19 = call i32 @write(i32 1, i8* %10, i32 %18)
br label %20
20: ; preds = %16, %14
%21 = phi i32 [ -1, %14 ], [ 0, %16 ]
%22 = tail call i32 @close(i32 %5)
br label %23
23: ; preds = %20, %7
%24 = phi i32 [ -1, %7 ], [ %21, %20 ]
ret i32 %24
}