#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
int creat(char *, int);
int open(char *, int);
scalar_t__ stat(char *, struct stat *);
int do_touch(char *file, unsigned int flags) {
  struct stat st;
  int fd;
  if (stat(file, &st) < 0) {
    if (!(flags & 0x01)) {
      if ((fd = creat(file, 0666)) < 0)
        return -1;
      close(fd);
    }
    return 0;
  }
  if ((fd = open(file, 2)) < 0)
    return -1;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<do_touch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11af <do_touch+0x6f>
mov    -0x14(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    11a3 <do_touch+0x63>
mov    -0x10(%rbp),%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    119b <do_touch+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <do_touch+0xa4>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e4 <do_touch+0xa4>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1050 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11d5 <do_touch+0x95>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <do_touch+0xa4>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_touch>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
js     1171 <do_touch+0x31>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1050 <open@plt>
test   %eax,%eax
js     118a <do_touch+0x4a>
mov    %eax,%edi
call   1030 <close@plt>
xor    %eax,%eax
jmp    118f <do_touch+0x4f>
xor    %eax,%eax
test   $0x1,%bpl
jne    118f <do_touch+0x4f>
mov    %rbx,%rdi
mov    $0x1b6,%esi
call   1060 <creat@plt>
test   %eax,%eax
jns    1166 <do_touch+0x26>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @do_touch(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = call i64 @stat(i8* %8, %struct.stat* %6)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %2
%12 = load i32, i32* %5
%13 = and i32 %12, 1
%14 = icmp ne i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = call i32 @creat(i8* %16, i32 438)
store i32 %17, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %3
br label %32
20: ; preds = %15
%21 = load i32, i32* %7
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %20, %11
store i32 0, i32* %3
br label %32
24: ; preds = %2
%25 = load i8*, i8** %4
%26 = call i32 @open(i8* %25, i32 2)
store i32 %26, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %3
br label %32
29: ; preds = %24
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
store i32 0, i32* %3
br label %32
32: ; preds = %29, %28, %23, %19
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @do_touch(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = and i32 %1, 1
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %7
%11 = call i32 @creat(i8* %0, i32 438)
%12 = icmp slt i32 %11, 0
br i1 %12, label %19, label %16
13: ; preds = %2
%14 = call i32 @open(i8* %0, i32 2)
%15 = icmp slt i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13, %10
%17 = phi i32 [ %11, %10 ], [ %14, %13 ]
%18 = call i32 @close(i32 %17)
br label %19
19: ; preds = %16, %13, %7, %10
%20 = phi i32 [ -1, %10 ], [ 0, %7 ], [ -1, %13 ], [ 0, %16 ]
ret i32 %20
}