#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_EXCL;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int errno;
int getpid();
int open(char const *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int adc_server_write_pid(const char *pidpath) {
  int fd;
  int ret_val;
  char pidstr[20];
  fd = open(pidpath, O_EXCL | O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    do {
    } while (0);
    return errno * (-1);
  }
  snprintf(pidstr, 20, "%d", getpid());
  ret_val = write(fd, pidstr, strlen(pidstr));
  if (ret_val < 0) {
    do {
    } while (0);
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<adc_server_write_pid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e78(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    0x2e3d(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11cb <adc_server_write_pid+0x6b>
jmp    11b4 <adc_server_write_pid+0x54>
jmp    11b9 <adc_server_write_pid+0x59>
mov    0x2e00(%rip),%rax
imul   $0xffffffff,(%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <adc_server_write_pid+0xf0>
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%ecx
mov    $0x14,%esi
lea    0xe14(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1241 <adc_server_write_pid+0xe1>
jmp    1228 <adc_server_write_pid+0xc8>
jmp    122d <adc_server_write_pid+0xcd>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1250 <adc_server_write_pid+0xf0>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adc_server_write_pid>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e5b(%rip),%rax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e61(%rip),%rax
or     (%rax),%esi
mov    0x2e30(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1080 <open@plt>
test   %eax,%eax
js     11e0 <adc_server_write_pid+0x80>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe54(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x14,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1070 <close@plt>
sar    $0x1f,%ebp
jmp    11eb <adc_server_write_pid+0x8b>
mov    0x2dd9(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@errno = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @adc_server_write_pid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [20 x i8]
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_EXCL
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @O_RDWR
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %7, i32 %12, i32 %15)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %1
br label %20
20: ; preds = %19
br label %21
21: ; preds = %20
%22 = load i32, i32* @errno
%23 = mul nsw i32 %22, -1
store i32 %23, i32* %2
br label %43
24: ; preds = %1
%25 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%26 = call i32 (...) @getpid()
%27 = call i32 @snprintf(i8* %25, i32 20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %4
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%30 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @write(i32 %28, i8* %29, i32 %31)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %24
br label %36
36: ; preds = %35
br label %37
37: ; preds = %36
%38 = load i32, i32* %4
%39 = call i32 @close(i32 %38)
store i32 -1, i32* %2
br label %43
40: ; preds = %24
%41 = load i32, i32* %4
%42 = call i32 @close(i32 %41)
store i32 0, i32* %2
br label %43
43: ; preds = %40, %37, %21
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@errno = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @adc_server_write_pid(i8* nocapture %0) {
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_EXCL
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_RDWR
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %8, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
br label %24
17: ; preds = %1
%18 = tail call i32 (...) @getpid()
%19 = call i32 @snprintf(i8* %3, i32 20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = call i32 @strlen(i8* %3)
%21 = call i32 @write(i32 %12, i8* %3, i32 %20)
%22 = call i32 @close(i32 %12)
%23 = ashr i32 %21, 31
br label %24
24: ; preds = %17, %14
%25 = phi i32 [ %16, %14 ], [ %23, %17 ]
ret i32 %25
}