#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 */
typedef int mode_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int ipc_open(char *fname) {
  int fd;
  mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  fd = open(fname, O_RDWR);
  if (fd == -1) {
    printf("\n*** error opening system data file\r\n");
    perror(fname);
    printf("*** terminating program\n\n");
    exit(1);
  }
  printf("file descriptor %i returned from open\n", fd);
  return fd;
}


x86 assembly (clang, -O0):
<ipc_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    0x2e84(%rip),%rcx
or     (%rcx),%eax
mov    0x2e6b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e5a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11c1 <ipc_open+0x81>
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <perror@plt>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%esi
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_open>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1184 <ipc_open+0x34>
mov    %eax,%ebp
lea    0xe8e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1060 <perror@plt>
lea    0xead(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@O_RDWR = global i32 0
@.str = [38 x i8] c"\0A*** error opening system data file\0D\0A\00"
@.str.1 = [26 x i8] c"*** terminating program\0A\0A\00"
@.str.2 = [39 x i8] c"file descriptor %i returned from open\0A\00"
define i32 @ipc_open(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
%5 = load i32, i32* @S_IRUSR
%6 = load i32, i32* @S_IWUSR
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRGRP
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IROTH
%11 = or i32 %9, %10
store i32 %11, i32* %4
%12 = load i8*, i8** %2
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %1
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0))
%19 = load i8*, i8** %2
%20 = call i32 @perror(i8* %19)
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %1
%24 = load i32, i32* %3
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@O_RDWR = global i32 0
@.str.2 = [39 x i8] c"file descriptor %i returned from open\0A\00"
@str = [37 x i8] c"\0A*** error opening system data file\0D\00"
@str.3 = [25 x i8] c"*** terminating program\0A\00"
define i32 @ipc_open(i8* nocapture %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* %0, i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %10
5: ; preds = %1
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @perror(i8* %0)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.3, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %3)
ret i32 %3
}