#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 /*<<< orphan*/ exit(int);
scalar_t__ get_sha1_hex(char *, unsigned char *);
int mkstemp(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
void *read_sha1_file(unsigned char *, char *, unsigned long *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ usage(char *);
unsigned long write(int, void *, unsigned long);
int main(int argc, char **argv) {
  unsigned char sha1[20];
  char type[20];
  void *buf;
  unsigned long size;
  char template[] = "temp_git_file_XXXXXX";
  int fd;
  if (argc != 2 || get_sha1_hex(argv[1], sha1))
    usage("cat-file: cat-file <sha1>");
  buf = read_sha1_file(sha1, type, &size);
  if (!buf)
    exit(1);
  fd = mkstemp(template);
  if (fd < 0)
    usage("unable to create tempfile");
  if (write(fd, buf, size) != size)
    strcpy(type, "bad");
  printf("%s: %s\n", template, type);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe60(%rip),%rax
mov    %rax,-0x80(%rbp)
mov    0xe5d(%rip),%rax
mov    %rax,-0x78(%rbp)
mov    0xe5b(%rip),%eax
mov    %eax,-0x70(%rbp)
mov    0xe56(%rip),%al
mov    %al,-0x6c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11e6 <main+0x66>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <get_sha1_hex@plt>
cmp    $0x0,%rax
je     11f2 <main+0x72>
lea    0xe28(%rip),%rdi
call   1090 <usage@plt>
lea    -0x30(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x60(%rbp),%rdx
call   1070 <read_sha1_file@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    121c <main+0x9c>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    -0x80(%rbp),%rdi
call   1080 <mkstemp@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    1244 <main+0xc4>
lea    0xdf0(%rip),%rdi
call   1090 <usage@plt>
mov    -0x84(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    -0x60(%rbp),%rdx
call   1040 <write@plt>
cmp    -0x60(%rbp),%rax
je     1271 <main+0xf1>
lea    -0x50(%rbp),%rdi
lea    0xddd(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x80(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    0xdcd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
movaps 0xe73(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movabs $0x5858585858585f,%rax
mov    %rax,0x1d(%rsp)
cmp    $0x2,%edi
jne    11b9 <main+0x39>
mov    0x8(%rsi),%rdi
lea    0x50(%rsp),%rsi
call   1060 <get_sha1_hex@plt>
test   %rax,%rax
je     11c5 <main+0x45>
lea    0xe55(%rip),%rdi
call   1090 <usage@plt>
lea    0x50(%rsp),%rdi
lea    0x30(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1070 <read_sha1_file@plt>
test   %rax,%rax
je     1243 <main+0xc3>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
call   1080 <mkstemp@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11fd <main+0x7d>
lea    0xe37(%rip),%rdi
call   1090 <usage@plt>
mov    0x8(%rsp),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
cmp    0x8(%rsp),%rax
je     1224 <main+0xa4>
lea    0xe2f(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1030 <strcpy@plt>
lea    0xe22(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x30(%rsp),%rdx
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.template = [21 x i8] c"temp_git_file_XXXXXX\00"
@.str = [26 x i8] c"cat-file: cat-file <sha1>\00"
@.str.1 = [26 x i8] c"unable to create tempfile\00"
@.str.2 = [4 x i8] c"bad\00"
@.str.3 = [8 x i8] c"%s: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [20 x i8]
%7 = alloca [20 x i8]
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [21 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [21 x i8]* %10 to i8*
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %22, label %15
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%20 = call i64 @get_sha1_hex(i8* %18, i8* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15, %2
%23 = call i32 @usage(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %15
%25 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%26 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%27 = call i8* @read_sha1_file(i8* %25, i8* %26, i64* %9)
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = icmp ne i8* %28, null
br i1 %29, label %32, label %30
30: ; preds = %24
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %24
%33 = getelementptr inbounds [21 x i8], [21 x i8]* %10, i64 0, i64 0
%34 = call i32 @mkstemp(i8* %33)
store i32 %34, i32* %11
%35 = load i32, i32* %11
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @usage(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %32
%40 = load i32, i32* %11
%41 = load i8*, i8** %8
%42 = load i64, i64* %9
%43 = call i64 @write(i32 %40, i8* %41, i64 %42)
%44 = load i64, i64* %9
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %49
46: ; preds = %39
%47 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%48 = call i32 @strcpy(i8* %47, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %46, %39
%50 = getelementptr inbounds [21 x i8], [21 x i8]* %10, i64 0, i64 0
%51 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%52 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %50, i8* %51)
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@__const.main.template = [21 x i8] c"temp_git_file_XXXXXX\00"
@.str = [26 x i8] c"cat-file: cat-file <sha1>\00"
@.str.1 = [26 x i8] c"unable to create tempfile\00"
@.str.2 = [4 x i8] c"bad\00"
@.str.3 = [8 x i8] c"%s: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [20 x i8]
%4 = alloca [20 x i8]
%5 = alloca i64
%6 = alloca [21 x i8]
%7 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%9 = bitcast i64* %5 to i8*
%10 = getelementptr inbounds [21 x i8], [21 x i8]* %6, i64 0, i64 0
%11 = icmp eq i32 %0, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @get_sha1_hex(i8* %14, i8* %7)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %12, %2
%18 = call i32 @usage(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = call i8* @read_sha1_file(i8* %7, i8* %8, i64* %5)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %19
%25 = call i32 @mkstemp(i8* %10)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @usage(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = load i64, i64* %5
%31 = call i64 @write(i32 %25, i8* %20, i64 %30)
%32 = load i64, i64* %5
%33 = icmp eq i64 %31, %32
br i1 %33, label %36, label %34
34: ; preds = %29
%35 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %10, i8* %8)
ret i32 0
}