#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*/ close(int);
int creat(char *, int);
int errno;
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  int ret = 0;
  int fd = creat("test.log", 0644);
  if (fd == -1) {
    printf("creat failed, errno = %d\n", errno);
    return -1;
  }
  char buf[32] = {0};
  ret = read(fd, buf, 31);
  printf("read return %d, read : %s, errno = %d\n", ret, buf, errno);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe8c(%rip),%rdi
mov    $0x1a4,%esi
call   1070 <creat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ae <main+0x5e>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ff <main+0xaf>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1f,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    -0x40(%rbp),%rdx
mov    0x2e00(%rip),%rax
mov    (%rax),%ecx
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
mov    $0x1a4,%esi
call   1060 <creat@plt>
cmp    $0xffffffff,%eax
je     11a5 <main+0x65>
mov    %eax,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    %rsp,%r14
mov    %eax,%edi
mov    %r14,%rsi
mov    $0x1f,%edx
call   1050 <read@plt>
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%ecx
lea    0xe95(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11c1 <main+0x81>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe54(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.log\00"
@.str.1 = [26 x i8] c"creat failed, errno = %d\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"read return %d, read : %s, errno = %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%9 = call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 420)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %13)
store i32 -1, i32* %3
br label %26
15: ; preds = %2
%16 = bitcast [32 x i8]* %8 to i8*
%17 = load i32, i32* %7
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%19 = call i32 @read(i32 %17, i8* %18, i32 31)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%22 = load i32, i32* @errno
%23 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %20, i8* %21, i32 %22)
%24 = load i32, i32* %7
%25 = call i32 @close(i32 %24)
store i32 0, i32* %3
br label %26
26: ; preds = %15, %12
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.log\00"
@.str.1 = [26 x i8] c"creat failed, errno = %d\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"read return %d, read : %s, errno = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [32 x i8]
%4 = tail call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 420)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %15
9: ; preds = %2
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%11 = call i32 @read(i32 %4, i8* %10, i32 31)
%12 = load i32, i32* @errno
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %11, i8* %10, i32 %12)
%14 = call i32 @close(i32 %4)
br label %15
15: ; preds = %9, %6
%16 = phi i32 [ -1, %6 ], [ 0, %9 ]
ret i32 %16
}