#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 /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main() {
  int fd;
  int ret;
  fd = open("test.log", O_WRONLY | O_CREAT, 0640);
  if (fd < 0) {
    perror("open");
    return -1;
  }
  char *buff = "abcdefghij";
  ret = write(fd, buff, strlen(buff));
  if (ret < 0) {
    printf("write");
    close(fd);
    return -1;
  }
  printf("test:\n%s\n", buff);
  int fd1 = open("test1.log", O_WRONLY | O_CREAT, 0640);
  if (fd1 < 0) {
    perror("open");
    return -1;
  }
  int ret1;
  ret1 = write(fd1, buff, strlen(buff));
  if (ret1 < 0) {
    perror("write");
    close(fd1);
    return -1;
  }
  printf("test1:\n%s\n", buff);
  close(fd);
  close(fd1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
lea    0xe78(%rip),%rdi
mov    $0x1a0,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b7 <main+0x57>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12eb <main+0x18b>
lea    0xe50(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1216 <main+0xb6>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12eb <main+0x18b>
mov    -0x18(%rbp),%rsi
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    0x2dc0(%rip),%rax
or     (%rax),%esi
lea    0xde8(%rip),%rdi
mov    $0x1a0,%edx
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1270 <main+0x110>
lea    0xdaa(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12eb <main+0x18b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12c2 <main+0x162>
lea    0xd70(%rip),%rdi
call   1080 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12eb <main+0x18b>
mov    -0x18(%rbp),%rsi
lea    0xd66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e62(%rip),%r14
mov    0x2e83(%rip),%r15
mov    (%r15),%esi
or     (%r14),%esi
lea    0xe7e(%rip),%rdi
mov    $0x1a0,%edx
call   1070 <open@plt>
test   %eax,%eax
js     1257 <main+0xf7>
mov    %eax,%ebx
lea    0xe71(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1230 <main+0xd0>
lea    0xe63(%rip),%rdi
lea    0xe4b(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%r15),%esi
or     (%r14),%esi
lea    0xe52(%rip),%rdi
mov    $0x1a0,%edx
call   1070 <open@plt>
test   %eax,%eax
js     1257 <main+0xf7>
mov    %eax,%r14d
lea    0xe1f(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1265 <main+0x105>
lea    0xe24(%rip),%rdi
lea    0xdf8(%rip),%rsi
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
jmp    124a <main+0xea>
lea    0xde2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdab(%rip),%rdi
call   1080 <perror@plt>
jmp    1245 <main+0xe5>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
mov    %r14d,%edi
jmp    1240 <main+0xe0>

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 = [11 x i8] c"abcdefghij\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [10 x i8] c"test:\0A%s\0A\00"
@.str.5 = [10 x i8] c"test1.log\00"
@.str.6 = [11 x i8] c"test1:\0A%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9, i32 416)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = 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 %57
15: ; preds = %0
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8** %4
%16 = load i32, i32* %2
%17 = load i8*, i8** %4
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @write(i32 %16, i8* %17, i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %15
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %1
br label %57
27: ; preds = %15
%28 = load i8*, i8** %4
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %28)
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %32, i32 416)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %27
%37 = 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 %57
38: ; preds = %27
%39 = load i32, i32* %5
%40 = load i8*, i8** %4
%41 = load i8*, i8** %4
%42 = call i32 @strlen(i8* %41)
%43 = call i32 @write(i32 %39, i8* %40, i32 %42)
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %38
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* %5
%49 = call i32 @close(i32 %48)
store i32 -1, i32* %1
br label %57
50: ; preds = %38
%51 = load i8*, i8** %4
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %51)
%53 = load i32, i32* %2
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* %5
%56 = call i32 @close(i32 %55)
store i32 0, i32* %1
br label %57
57: ; preds = %50, %46, %36, %23, %13
%58 = load i32, i32* %1
ret i32 %58
}

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.2 = [11 x i8] c"abcdefghij\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [10 x i8] c"test:\0A%s\0A\00"
@.str.5 = [10 x i8] c"test1.log\00"
@.str.6 = [11 x i8] c"test1:\0A%s\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_WRONLY
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3, i32 416)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %35
8: ; preds = %0
%9 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @write(i32 %4, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%14 = tail call i32 @close(i32 %4)
br label %35
15: ; preds = %8
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* @O_WRONLY
%18 = load i32, i32* @O_CREAT
%19 = or i32 %18, %17
%20 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i32 %19, i32 416)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %35
24: ; preds = %15
%25 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @write(i32 %20, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%30 = tail call i32 @close(i32 %20)
br label %35
31: ; preds = %24
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @close(i32 %4)
%34 = tail call i32 @close(i32 %20)
br label %35
35: ; preds = %12, %28, %31, %22, %6
%36 = phi i32 [ -1, %6 ], [ -1, %12 ], [ -1, %22 ], [ -1, %28 ], [ 0, %31 ]
ret i32 %36
}