#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_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *, ...);
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
void foo(char *filename, char *data) {
  printf("creating %s\n", filename);
  int fd;
  if ((fd = open(filename, O_CREAT | O_RDWR)) == -1) {
    printf("open %s failed %s\n", filename, strerror(errno));
    while (1)
      ;
  }
  if (write(fd, data, strlen(data)) != strlen(data)) {
    printf("write failed %s\n", strerror(errno));
    while (1)
      ;
  }
  close(fd);
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11d9 <foo+0x79>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11d4 <foo+0x74>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     1240 <foo+0xe0>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    123b <foo+0xdb>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe8f(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e76(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11cc <foo+0x6c>
mov    %eax,%ebp
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbx
jne    11f2 <foo+0x92>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <close@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe2c(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xchg   %ax,%ax
jmp    11f0 <foo+0x90>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe19(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
jmp    1220 <foo+0xc0>

LLVM IR (-O0):
@.str = [13 x i8] c"creating %s\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [19 x i8] c"open %s failed %s\0A\00"
@errno = global i32 0
@.str.2 = [17 x i8] c"write failed %s\0A\00"
define void @foo(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %3
%7 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11)
store i32 %12, i32* %5
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i8*, i8** %3
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %17)
br label %19
19: ; preds = %14, %19
br label %19
20: ; preds = %2
%21 = load i32, i32* %5
%22 = load i8*, i8** %4
%23 = load i8*, i8** %4
%24 = call i64 @strlen(i8* %23)
%25 = call i64 @write(i32 %21, i8* %22, i64 %24)
%26 = load i8*, i8** %4
%27 = call i64 @strlen(i8* %26)
%28 = icmp ne i64 %25, %27
br i1 %28, label %29, label %34
29: ; preds = %20
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %29, %33
br label %33
34: ; preds = %20
%35 = load i32, i32* %5
%36 = call i32 @close(i32 %35)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"creating %s\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [19 x i8] c"open %s failed %s\0A\00"
@errno = global i32 0
@.str.2 = [17 x i8] c"write failed %s\0A\00"
define void @foo(i8* %0, i8* nocapture %1) {
%3 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_RDWR
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %11)
br label %13
13: ; preds = %13, %9
br label %13
14: ; preds = %2
%15 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%16 = tail call i64 @write(i32 %7, i8* %1, i64 %15)
%17 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%18 = icmp eq i64 %16, %17
br i1 %18, label %24, label %19
19: ; preds = %14
%20 = load i32, i32* @errno
%21 = tail call i8* @strerror(i32 %20)
%22 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %23, %19
br label %23
24: ; preds = %14
%25 = tail call i32 @close(i32 %7)
ret void
}